CodeIgniter, Tutorials

How to send email using codeigniter email library

30 January 2019

Simply create a new file called the config/email.php. Add the $config array in that file.
Email functionality deployed on any Web Hosting for PHP is a simple matter of using the email library. All you have to do is to set a few variables and the email is setup.

1)First need to define email credentials in email library

email.php [application/config/email.php]

$config['useragent'] = 'CodeIgniter';
$config['protocol'] = 'smtp';                           //mail, sendmail, or smtp
//$config['mailpath'] = '/usr/sbin/sendmail';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';     //SMTP Server Address.
$config['smtp_user'] = 'YOUREMAILHERE@gmail.com';
$config['smtp_pass'] = 'YOURPASSWORDHERE';
$config['smtp_port'] = 465; 
$config['wordwrap'] = TRUE;                            //TRUE or FALSE
$config['mailtype'] = 'html';                          //text or html
$config['charset'] = 'utf-8';                          //utf-8, iso-8859-1, etc
$config['newline'] = "\r\n";                           //“\r\n” or “\n” or “\r”

2)Now send email from any controller or model using email library

$this->load->library('email'); // Email library
$this->email->from('EMAIL', 'NAME');
$this->email->subject('Test email from CI and Gmail');
$this->email->message('This is a test.');
      echo 'Email sent.';

This example assumes you are sending the email from one of your controllers. Using this code, No need to define email settings every time when email send. Just define email settings one time in library and you can use multiple time in the system.

Leave a Comments