Sending an email is not only simple, but you can configure it on the fly or set your preferences in a config file. CodeIgniter provides an Email library for sending.


Step 1: Create the controller file

application/controllers/Email_controller.php


Email_controller.php

<?php

class Email_controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('form');
    }

    public function index() {

        $this->load->helper('form');
        $this->load->view('email_form');
    }

    public function send_mail() {
        
        
        $from_email = "test@rathorji.in";
        $to_email = $this->input->post('email');

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

        $this->email->from($from_email, 'Your Name');
        $this->email->to($to_email);
        $this->email->subject('Email Test');
        $this->email->message('Testing the email class.');

        //Send mail 
        if ($this->email->send())
            $this->session->set_flashdata("email_sent", "Email sent successfully.");
        else
            $this->session->set_flashdata("email_sent", "Error in sending Email.");
        $this->load->view('email_form');
    }

}

?>

Step 2: Create the view file

Create view  email_form.php and make the form to entering email and one button to send mail


application/views/ email_form.php

<!DOCTYPE html> 
<html lang = "en"> 
    <head> 
        <meta charset = "utf-8"> 
        <title>CodeIgniter Email Example</title> 
    </head>
    <body> 
        <?php
        echo $this->session->flashdata('email_sent');
        echo form_open('/Email_controller/send_mail');
        ?> 

        <input type = "email" name = "email" required /> 
        <input type = "submit" value = "SEND MAIL"> 

        <?php
        echo form_close();
        ?> 
    </body>
</html>

Code is 100% tested it will work for you 

Output:




Make sure you have changed the default controller in application/config/routes.php 

$route['default_controller'] = 'Email_controller';