CodeIgniter Send Emails Via SMTP Server

In CodeIgniter, if you want to send the emails with third party SMTP server, you can use the following code snippet.

You need to load the SMTP library and pass sender email to from() function, receiver email in to() function, subject in subject() function and email message in message() function. Also you need to pass SMTP configuration like host name, user email, email password.

$this->load->library('email');  
$this->email->from('from@example.com', 'from Name');
$this->email->to('to@example.com');
$this->email->subject('Email Subject');
$this->email->message('Email message content');

After writing above code, you need to call send() function like below:

$this->email->send(); 

Example :

<?php

$this->load->library('email');  

$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'your email';
$config['smtp_pass'] = 'email password';
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
 
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

$this->email->from('from@example.com', 'from Name');
$this->email->to('to@example.com');
$this->email->subject('Email Subject');
$this->email->message('Email message content');

$this->email->send(); 

?>

Hope this helps!

Leave a Reply