How to Send Email in CodeIgniter via SMTP Server

On 11/14/2017

Codeigniter comes with many built-in helpers and libraries for robust development. Of which its email library is a little gem that you can configure and send mails on fly. Here is a simple example to send email in codeigniter using smtp protocol. We'll see how to send mail with attachments through gmail, but you can use any third party smtp servers. Just get the smtp settings from the service provider and use it instead of gmail settings and you are done.

To send mail in codeigniter, you have to configure your email preferences like mail protocol, smtp authorization, html or plain text mail etc. These email preferences can be defined either globally or at the time of sending mails (like inside controllers).

Also Read:

Configure Email Settings

Here we'll configure our email settings globally in codeigniter. Create a file named 'email.php' inside 'application/config' folder and add the below settings to it. These settings will be loaded and used automatically for sending emails.

<?php
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
    $config['smtp_port'] = '465';
    $config['smtp_user'] = 'user@gmail.com'; //change this
    $config['smtp_pass'] = 'password'; //change this
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;
    $config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
?>

The above is the smtp settings for gmail. The 'smtp_user' is your (gmail) email id and 'smtp_pass' is the email password.

Next we have to load the codeigniter email library and use its methods to send mail.

Sending Email in Codeigniter using Gmail

Use this sendmail() function in a controller file to send email. You can load the 'email' library either inside the function or in the controller constructor.

<?php
//send mail
function sendmail()
{
    $this->load->library('email'); // load email library
    $this->email->from('user@gmail.com', 'sender name');
    $this->email->to('test1@gmail.com');
    $this->email->cc('test2@gmail.com'); 
    $this->email->subject('Your Subject');
    $this->email->message('Your Message');
    $this->email->attach('/path/to/file1.png'); // attach file
    $this->email->attach('/path/to/file2.pdf');
    if ($this->email->send())
        echo "Mail Sent!";
    else
        echo "There is error in sending mail!";
}
?>

All the above email methods like from(), to() are pretty self explanatory and I'll leave it to you. The method $this->email->send() will return a boolean value based upon if email is sent or not.

If you want to send mail in bulk, use comma separated email-id's in $this->email->to() method like this.

$this->email->to('test1@gmail.com, test2@gmail.com, test3@gmail.com');

Also Read:

That's all about sending email in codeigniter via smtp. I hope you like this tutorial. Please don't forget to share in social networks.

Last Modified: 14-Nov-2017

9 comments:

  1. nice share, but can i using my own email like abc@mydomain.com in this way?

    ReplyDelete
    Replies
    1. Yep! You can but make to get the proper 'smtp_host', 'smtp_port', 'smtp_user' & 'smtp_pass' from your hosting provider and configure the settings accordingly :)

      Cheers.

      Delete
  2. error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

    ReplyDelete
  3. Hello I have a issue, when I am using smtp and sending mail it's not usingh return path i have set, by default it will take from email in return path. any idea for this

    ReplyDelete
  4. Message: fsockopen(): unable to connect to XYZ.co.in:25 (Connection refused)

    ReplyDelete
  5. please tell where i create sendmail() function.please

    ReplyDelete
    Replies
    1. hi...in this method i m using codeigniter smtp setting.but don't send mail to gmail....why???p/z help me..

      Delete

Contact Form

Name

Email *

Message *