Simple way to send email in codeigniter using SMTP | 2my4edge

01 March 2022

Simple way to send email in codeigniter using SMTP

Here I'm going to tell you about how to send contact emails in codeigniter using SMTP Settings. this is one of the simple way to integrate in Codeignter. We already discussed a bit about Codeigniter in previous tutorials. It has some library file to make this action. like that here also we have email library file. Lets see the code and steps below.

Send email using Codeignter
LIVE DEMO                                       DOWNLOAD

Please refer this article for settings 

BASIC SETTING
application->config->autoload.php file
$autoload['libraries'] = array('database', 'email', 'session', 'upload');

DATABASE
Database Name : 2my4edge
TABLE NAME
1.code_email
CREATE TABLE `code_email` (
  `id` int(11) NOT NULL,
  `name` varchar(250) NOT NULL,
  `email` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

VIEW FILES
1.emailform.php
<div align="center" class="container">
<?php if($this->session->flashdata('flash_job')){?>
      <div class="alert alert-success">      
        <?php echo $this->session->flashdata('flash_job')?>
      </div>
    <?php } ?>
<form method="POST" action="<?php echo site_url('sendmail/sendmail');?>">
<table border="1">
<tr>
<td>Name</td>
<td><input type="text" name="name" required id="name" placeholder="Name"></td>
</tr>
<tr>
<td>Email ID</td>
<td><input type="email" name="emailid" required id="email" placeholder="Your Email id"></td>
</tr>
<tr>
<td colspan="2" align="center"><input style="width: 50%;" type="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>

CONTROLLER FILES
1.Sendmail.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sendmail extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');                  /***** LOADING HELPER TO AVOID PHP ERROR ****/
        $this->load->model('Sendmail_model','send'); /* LOADING MODEL * Welcome_model as welcome */
    }

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



    public function sendmail()
    {

    $input_data=array('name'=>$this->input->post('name'),
                      'email'=>$this->input->post('emailid')
                     );
    $this->send->save_contact_data($input_data);

    $from_email="arun@2my4edge.com";
    $replyemail = "arunkumarpsp1@gmail.com";
    $name="Arunkumar 2my4edge";
    $to_email=$input_data['email'];
    $subject="New Registration from 2my4edge.com";
    $config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtp.gmail.com',
    'smtp_port' => 465,
    'smtp_user' => 'youremail@gmail.com',
    'smtp_pass' => 'xxxyourpasswordxxx',
    'mailtype' => 'text/html',
    'newline' => '\r\n',
    'charset' => 'utf-8'
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->set_mailtype("html");
    $this->email->from($from_email,$name);
    $this->email->to($to_email);
    $this->email->reply_to($replyemail);
    $this->email->subject($subject);
    $message='<table  style="background:white;width:100%;text-align:center;max-width:800px;min-width:450px;">
    <tbody>
    <tr>
    <td style="padding:10px 0px;">
    <a href="http://www.2my4edge.com">
    <img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWvOdIiG12ffCJuB4q3pygAo8JIVmr_4serpR20MRj6PzghHHaIqPdxhbWyLOlvntAvLoz_rQBUzXKQCUeMqN9V6S2jpw8ty6uVpsbJe2iechFfbT2reo9nbu5NaXlJCxJUcOk_IwrdYFx/s1600/new2.png">
    </a>
    </td>
    </tr>
    </tbody>
    </table>
    <table style="background:#F1F1F1;width:100%;padding: 12px; text-align: justify; 
                line-height: 25px;font-family:roboto;max-width:800px;min-width:450px;">
    <tbody>
    <tr>
      <td>
        <p style="color:#68461d;font-size:28px;">
          <i></i><br>
            <small style="color:666666!important;font-size: medium; font-style: normal;">
            Dear <b>'.$input_data['name'].',</b> <br>
            you have tested email from <a href="http://www.2my4edge.com">www.2my4edge.com</a>. <br>
            Your Email Id is <b>'.$input_data['email'].'</b>.</small>
        </p>
       
      </td>
     </tr>
    </tbody>
    </table>';
    $this->email->message($message);
    $this->email->send();

    $this->session->set_flashdata("flash_job",'Thank you for your submission. We will get back to you shorty.');
    redirect('sendmail');
  }



}
Change STML EMAIL and PASSWORD. Give your valid email id and password.

MODEL FILES
1.Sendmail_model.php
<?php
class Sendmail_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }


    public function save_contact_data($inputdata)
    {
      $this->db->insert('code_email', $inputdata); 
    }

}

If you want to save the data in database use this model, else remove the model. and also from controller.

For more details visit this link : Codeignter
I hope this code is really helpful to all codeigniter developer. Thanks and comment your feedback below.

3 comments:

  1. Ritesh Saini6/02/2016 8:59 AM

    Bohot Achchha, Tutorial post kiya hai aapne bhai,

    Ese hi hum logo ko sikhate rehna..

    Shukriya.

    ReplyDelete
  2. JethaLal ChampakLal Gada6/22/2016 6:13 PM

    Bahu Saras, Tutorial 6 ...

    ReplyDelete
  3. Open Internet Explorer and log in to your first Google account. Next, open Google Chrome and log in to your second Google account, and finally, open Firefox to log in to the third account. You can use all Google services for each account, just as if you were logged in to just one. how to create gmail account

    ReplyDelete

^