Home >>Codeigniter Tutorial >How to Send Email using CodeIgniter

How to Send Email using CodeIgniter

How to Send Email using CodeIgniter

Sending email in CodeIgniter is much easier and you may set preferences in accordance with your needs. CodeIgniter supplies an Email library to sending email in program. Codeigniter supported inbuilt Mail library which simplifies the email sending process.

To send mail in Codeigniter we need to do a little bit of configuration, need to load email library.

Codeigniter supports multiple Protocols, Mail, send mail, and SMTP, multiple recipients, CC and BCCs, HTML or Plain text email, Attachments, Word wrapping and many more.

As you know email is quite important in web applications. When a user signs up, we may want to send them an email to verify their email address and allow the user to confirm subscription. We also use email to reset forgotten passwords, send invoice and receipts to clients, etc. CodeIgniter makes it super simple for us to send emails from our application utilizing a variety of options.

CodeIgniter includes an integrated email library that called codeigniter email library that we're able to work together when sending mails. In this tutorial, we will demonstrate the mostly used email attributes like text mail, HTML email, and email with an attachment.

CodeIgniter SMTP Email Configuration

Create a smtp configuration file email.php inside application/config

$config = array(
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.example.com', 
    'smtp_port' => 465,
    'smtp_user' => 'phptpoint@example.com
    'smtp_pass' => '123456',
    'smtp_crypto' => 'ssl',
    'mailtype' => 'text',
    'smtp_timeout' => '4', 
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE

Note : for sending emails to work, you should provide valid configuration parameters. Dummy parameters will not be able to send emails.

Create a View

Create a view file inside views/email.php

Add given below code inside the file.

<!DOCTYPE html>
        <title>CodeIgniter  Email Send using SMTP</title>
            <h3>Use the form below to send email</h3>
            <form method="post" >
                <input type="email" name="to" placeholder="Enter Receiver Email">
                <input type="text" name="subject" placeholder="Enter Subject">
                <textarea rows="6" name="message" placeholder="Enter your message here"></textarea>
                <input type="submit" value="Send Email" />

CodeIgniter Controller

Create a Controller file inside controller/EmailController .php

Add given below code inside EmailController.php file.

class EmailController 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 'Email has been sent successfully';

No Sidebar ads