Send Emails from CodeIgniter on Localhost

Today, We want to share with you codeigniter send email from localhost. Email is very important in online any web applications. When a member signs up, i might want to send them an email to verify their email address as well as allow the member to confirm subscription. I also use email to reset forgotten passwords, send invoice as well as receipts to customers, etc. PHP CodeIgniter makes it super easy for us to send emails from our web application using a variety of options.

CodeIgniter has a built-in email library that i can work with when sending emails.

In this tutorial, you will learn simple step by step

  • Email Configuration
  • Email View
  • Email Controller
  • Email Routes

How to Send Email using CodeIgniter?

Today We was working with PHP with codeigniter as well as there is email feature but that need to work on localhost but We stuck at that point. And then some time We got the best solution as well as today I will share that PHP code with you. First of all We tell you PHP codeigniter is great MVC framework as well as We like it very much.

First you required you make an account on and after making an account you will get username and password for smtp settings and you required to add that config or settings in your CI web site.

Here is working CI code and you need to add this your PHP codeigniter’s config.php file:


$config['email'] = Array(
'protocol' => 'smtp',
'smtp_host' => '',
'smtp_port' => 2525,
'smtp_user' => 'your_user_name',
'smtp_pass' => 'your_user_pass',
'crlf' => "\r\n",
'newline' => “\r\n”

In this first step I will download the latest version of Codeigniter, first of all simple you can Go to this link Download Codeigniter.

Create Contact Form And Send Email using PHP

CodeIgniter Email Configuration

here simple Create a file email.php in the directory application/config

<?php defined('BASEPATH') OR exit('No direct script access allowed');

$config = array(
'protocol' => 'smtp', // 'mail', 'sendmail', or 'smtp'
'smtp_host' => '',
'smtp_port' => 465,
'smtp_user' => '',
'smtp_pass' => '12345!',
'smtp_crypto' => 'ssl', //can be 'ssl' or 'tls' for example
'mailtype' => 'text', //plaintext 'text' mails or 'html'
'smtp_timeout' => '4', //in seconds
'charset' => 'iso-8859-1',
'wordwrap' => TRUE

Basic Configurations

$config['base_url'] = 'http://localhost/demo';

CodeIgniter Email View


<!DOCTYPE html>
<title>CodeIgniter Send Email -</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<h3>Use the form below to send email</h3>
<form method="post" action="<?=base_url('email')?>" enctype="multipart/form-data">
<input type="email" id="to" name="to" placeholder="Receiver Email">
<input type="text" id="subject" name="subject" placeholder="Enter Your Subject">
<textarea rows="6" id="message" name="message" placeholder="Type your message here"></textarea>
<input type="submit" value="Send Email" />

CodeIgniter Email Controller



defined('BASEPATH') OR exit('No direct script access allowed');

class SendEmailCtrl extends CI_Controller {

public function __construct() {
parent:: __construct();


public function index() {

function send() {

$from = $this->config->item('smtp_user');
$to = $this->input->post('to');
$subject = $this->input->post('subject');
$message = $this->input->post('message');


if ($this->email->send()) {
echo 'Your Email has successfully been sent.';
} else {

Email Routes call to methods

Add the bellow routes to application/config/routes.php

$route['send-email'] = 'your email controller';
$route['email'] = 'your email controller/send';
PHP Mail Attachment – Php Send Email File-Images Attachments

how to send email from localhost with php CodeIgniter?

Here you can check the PHP source code for send email from localhost server simple step.

function sendMail()
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://',
'smtp_port' => 465,
'smtp_user' => '', // Update it to yours
'smtp_pass' => 'yourname@s245sd', // Update it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE

$message = '';
$this->load->library('email', $config);
$this->email->from(''); // Update it to yours
$this->email->to('');// Update it to yours
$this->email->subject('Send email by using codeigniter library via localhost For Testing');
echo 'Good Luck Your Email sent.';


I hope you get an idea about send link in email codeigniter.

#codeigniter #email 

Send Emails from CodeIgniter on Localhost
1.55 GEEK