Send Mail Using Gmail SMTP Server Using Codeigniter Framework

CodeigniterMail

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://smtp.googlemail.com‘.  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://smtp.googlemail.com', // Gmail smtp server
'smtp_port' => '465',                       // Gmail port number
'smtp_user' => 'someuser@gmail.com',
'smtp_pass' => 'password',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n"
);

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

$this->email->from(‘someuser@gmail.com’, ‘invoice’);
$this->email->to(‘test@test.com’);
$this->email->subject(‘Invoice’);
$this->email->message(‘Test’);

$this->email->send();

 


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

$this->email->from(‘someuser@gmail.com’, ‘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(‘test@test.com’) – 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.