• sixfacedeveloper@gmail.com
  • 989 498 3237
News Photo

Send Email in CodeIgniter With SMTP

These days, almost every PHP framework supports a well developed email management library. CodeIgniter is no different as it has a great email sending class that ensures that CodeIgniter projects could send emails without difficulty. In this article, I will describe how to send emails in a CodeIgniter application using SMTP. I will use the well known and maintained email sending class.

Load CodeIgniter’s Email Class

First, load the CodeIgniter’s email library through the following code snippet:
// loading library of email

Set Email Parameters

The next step is to setup the required fields for the custom email. these fields could be setup through several functions including: from() function takes two parameters – the email address of the sender and the name. to() function takes the email address of the recipient. Next two functions are subject() and message() that round up the requirements for sending emails in CodeIgniter. Here is how these functions are used in the code:

$this->email->from(’email@example.com’, ‘Identification’);
$this->email->subject(‘Send Email Codeigniter’);
$this->email->message(‘The email send using codeigniter library’);

Once these functions are filled, the final step is to send the email by using the send() function.


Create the Controller
Create a controller file Sendingemail.php and save it in the application/controller/. Add the following code to this file:

class Sendingemail extends CI_Controller {
function __construct() {
public function index() {
public function send_mail() {
$from_email = 'email@example.com';
$to_email = $this->input->post('email');
//Load email library
$this->email->from($from_email, 'Identification');
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library');
//Send mail
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
$this->session->set_flashdata("email_sent","You have encountered an error");

Create the View
Create a view file called contact_email_form.php and save it in

echo $this--->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
<input type="email" name="email" required=""><br>
<input type="submit" value="SEND MAIL"><br>
echo form_close();

Make the changes in the routes.php file in application/config/routes.php and add the following line at the end of the file:

$route['email'] = 'Sendingemail';

Access the Email Application
Finally, Hit the following URL to access the application:


Setting SMTP Configuration

As mentioned earlier, CodeIgniter fully supports different email protocols including SMTP through simple configuration options.

As you could see from the following code snippet, selecting the email protocol is the matter of setting a single configuration variable. In this code snippet, I have set the $config[‘protocol’] to  smtp for using SMTP protocol.


$config = array();
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'xxx';
$config['smtp_user'] = 'xxx';
$config['smtp_pass'] = 'xxx';
$config['smtp_port'] = 25;



Adding email functionality to CodeIgniter applications 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. If you need any help in sending email in your CodeIgniter applications, do leave a comment below and I will get back to you.

Share This News


Do you want to get our quality service for your business?