Send Mail Using Gmail SMTP Server Using Codeigniter Framework


Today we are going to see how we can use gmail SMTP server for sending emails from your web application using Codeigniter Framework. You can write following code into your controller.

  • Load Email library into your controller by calling $this->load->library(’email’, $email_config), here $email_config is the array of email configurations and their values.
  • If you want to send email using gmail smtp server then use smtp host value ‘ssl://‘.  username & password values will be your email username and password.
  • Use port number 465 for gmail smtp server, it will different for other smtp servers.

Now you almost done email configuration. And now you can use send() function for sending emails.

$email_config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://', // Gmail smtp server
'smtp_port' => '465',                       // Gmail port number
'smtp_user' => '',
'smtp_pass' => 'password',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n"

$this->load->library(’email’, $email_config);

$this->email->from(‘’, ‘invoice’);



$this->email->send() – Used for sending email using protocol which is defined in ‘protocol’ value.

$this->email->from(‘’, ‘invoice’) – Used for setting up from email address into your email, first parameter is sender email id and second parameter is sender name.

$this->email->to(‘’) – Used for setting up to address where email will go.

$this->email->subject(‘Invoice’) – As name explained, it is used for setting up mail subject.

$this->email->message(‘Test’) – Used for setting up mail content.

We will do more walkthrough with CodeIgniter Framework in futher posts.

Tags:, ,