Sent mail using SMTP in Codeigniter

Kailash Singh

Sent mail using SMTP in Codeigniter

Published Mar 19,2019 by Kailash Singh

12 Comments     3973 Views    


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 Elevenstech';

    $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://elevenstechwebtutorials.com/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://elevenstechwebtutorials.com/' target='_blank' style='text-decoration:none;color: #60d2ff;'>www.elevenstechwebtutorials.com</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.


12 Comments


Ranjeet Mar 20, 2019

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....

Elevenstech Mar 21, 2019

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';

Ranjeet Mar 27, 2019

thanks its working.

nayan Nov 04, 2019

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

Elevenstech Nov 05, 2019

Hi Nayan, Please check your code properly. If you have got the error again then share your code on [email protected]

deepak ladekar Dec 12, 2019

sir please email sending wala code mujhe share kre [email protected]

Elevenstech Dec 13, 2019

Ok i will share with you.

rakesh Dec 14, 2019

hi Elevenstech... 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?

Elevenstech Dec 15, 2019

Ok i will help you. mail me on this : [email protected]

Vipul Patel Dec 18, 2020

Thank You so much.. Very easy sir.. Working All code ... working code..

Sent mail using SMTP in Codeigniter

Elevenstech Dec 18, 2020

thank you so much