How to Send Email in CodeIgniter via SMTP Server. Now send email using SMTP Server.this is best Question solution for send mail. 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: How to send Email from localhost in php

Here is step for Send Email in CodeIgniter via SMTP Server

Configure Email Preferences

Here we’ll configure our email preferences 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.

    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://'; //change this
    $config['smtp_port'] = '465';
    $config['smtp_user'] = ''; //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.

Code to Send 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.

//send mail
function sendmail()
    $this->load->library('email'); // load email library
    $this->email->from('', 'sender name');
    $this->email->subject('Your Subject');
    $this->email->message('Your Message');
    $this->email->attach('/path/to/file1.png'); // attach file
    if ($this->email->send())
        echo "Mail Sent!";
        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.


That’s it Now Enjoy send mail using SMTP.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.