Published Mar 19,2019 by Kailash Singh
Sent mail using SMTP in Codeigniter

In this tutorial, we are going to learn about how to sent email using SMTP in codeigniter. The CodeIgniter email library will be used to send email using SMTP server.

 

Step 1 :- Create View in View Folder (email_send.php)

 

<h2>Sent Email Using SMTP</h2><br>

<form>
    <input type="email" name="from" class="form-control" placeholder="Enter Email" required><br>
    <textarea name="message" class="form-control" placeholder="Enter message here" required></textarea><br>
    <button type="submit" class="btn btn-primary">Send Message</button>
</form>

 

Step 2 :- Create Sent Mail Controller for view  in Controller Folder (Email_send.php) 

 

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

class Email_send extends CI_Controller {

	public function index()
	{
		$this->load->view('email_send');
	}

}

 

Step 3 :- Create Action, Method to send mail

 

The following HTML code below creates an uploader form. This form is having method attribute set to action, post .

<form action="<?php echo base_url(); ?>email_send/send" method="post">

 

Step 4 :- Create Send mail Function in Email Send  Controller 

 

public function send()
{
    $to =  $this->input->post('from');  // User email pass here
    $subject = 'Welcome To CodingMantra';

    $from = 'pass your email ID';              // Pass here your mail id

    $emailContent = '<!DOCTYPE><html><head></head><body><table width="600px" style="border:1px solid #cccccc;margin: auto;border-spacing:0;"><tr><td style="background:#000000;padding-left:3%"><img src="http://codingmantra.co.in/assets/logo/logo.png" width="300px" vspace=10 /></td></tr>';
    $emailContent .='<tr><td style="height:20px"></td></tr>';


    $emailContent .= $this->input->post('message');  //   Post message available here


    $emailContent .='<tr><td style="height:20px"></td></tr>';
    $emailContent .= "<tr><td style='background:#000000;color: #999999;padding: 2%;text-align: center;font-size: 13px;'><p style='margin-top:1px;'><a href='http://codingmantra.co.in/' target='_blank' style='text-decoration:none;color: #60d2ff;'>www.codingmantra.co.in</a></p></td></tr></table></body></html>";
                


    $config['protocol']    = 'smtp';
    $config['smtp_host']    = 'ssl://smtp.gmail.com';
    $config['smtp_port']    = '465';
    $config['smtp_timeout'] = '60';

    $config['smtp_user']    = 'Gmail ID Enter Here';    //Important
    $config['smtp_pass']    = 'Gmail ID Password pass here';  //Important

    $config['charset']    = 'utf-8';
    $config['newline']    = "\r\n";
    $config['mailtype'] = 'html'; // or html
    $config['validation'] = TRUE; // bool whether to validate email or not 

     

    $this->email->initialize($config);
    $this->email->set_mailtype("html");
    $this->email->from($from);
    $this->email->to($to);
    $this->email->subject($subject);
    $this->email->message($emailContent);
    $this->email->send();

    $this->session->set_flashdata('msg',"Mail has been sent successfully");
    $this->session->set_flashdata('msg_class','alert-success');
    return redirect('email_send');
}

 

Step 5 :- Create Success Mail Message in Email View 

 

<h2>Sent Email Using SMTP</h2><br>

  //Success Flash message available here ..............
  
  <?php if($error = $this->session->flashdata('msg')){ ?>
       <p style="color: green;"><strong>Success!</strong> <?php echo  $error; ?><p>
  <?php } ?>

  //End Flash message available here ..............


<form action="<?php echo base_url(); ?>email_send/send" method="post">
   <input type="email" name="from" class="form-control" placeholder="Enter Email" required><br>
   <textarea name="message" class="form-control" placeholder="Enter message here" required></textarea><br>
   <button type="submit" class="btn btn-primary">Send Message</button>
</form>

 

Step 6 :- Result 

 

 

Hope this will help our developers.

Because we believe Mantra means CodingMantra

9 Comments

Ranjeet said:

How will i get these data.... $config['smtp_port'] = '465'; $config['smtp_timeout'] = '60'; $config['smtp_user'] = 'Gmail ID Enter Here'; //Important $config['smtp_pass'] = 'Gmail ID Password pass here'; //Important' Data....

  • Mar 20, 2019

CodingMantra said:

Don't worry ranjeet. Just paste same code. But enter your gmail id and password in the code................... $config['smtp_user'] = 'Gmail ID Enter Here'; //Important........................... $config['smtp_pass'] = 'Gmail ID Password pass here';

  • Mar 21, 2019

Ranjeet said:

thanks its working.

  • Mar 27, 2019

nayan said:

A message to success the mail arrives but the mail is not received

  • Nov 04, 2019

CodingMantra said:

Hi Nayan, Please check your code properly. If you have got the error again then share your code on codingmantratech@gmail.com

  • Nov 05, 2019

deepak ladekar said:

sir please email sending wala code mujhe share kre deepakladekar36@gmail.com

  • Dec 12, 2019

jamir hossain said:

A message to success the mail arrives but the mail is not received

  • Dec 13, 2019

rakesh said:

hi coding mantra... this code works fine for me, thanks and i want this to insert in mysql db while email sending... im new to codeigniter any help?

  • Dec 14, 2019

ashwini said:

i pest all code as it is. but not work plz help me sir

  • Mar 19, 2020

Leave a Reply