CodeIgniter Framework Mail

How to send email using HTML templates in Codeigniter

This post explains how to send email using HTML templates in Codeigniter, earlier post we learned how to send mails using HTML content and using phpmailer and gmail. Now we learn how to send mails in Codeigniter.
By follow the below steps we can implement it.

How to send email using HTML templates in Codeigniter by Anil Kumar Panigrahi

How to send email using HTML templates in Codeigniter by Anil Kumar Panigrahi

Step 1: Create HTML file with suits to your template.

Path : /application/views/emails/anillabs.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Anil Labs - Codeigniter mail templates</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<div>
   <div style="font-size: 26px;font-weight: 700;letter-spacing: -0.02em;line-height: 32px;color: #41637e;font-family: sans-serif;text-align: center" align="center" id="emb-email-header"><img style="border: 0;-ms-interpolation-mode: bicubic;display: block;Margin-left: auto;Margin-right: auto;max-width: 152px" src="http://www.anillabs.com/wp-content/uploads/2013/09/anil-kumar-panigrahi-blog.png" alt="" width="152" height="108"></div>
<p style="Margin-top: 0;color: #565656;font-family: Georgia,serif;font-size: 16px;line-height: 25px;Margin-bottom: 25px">Hey <?php echo $userName;?>,</p>
<p style="Margin-top: 0;color: #565656;font-family: Georgia,serif;font-size: 16px;line-height: 25px;Margin-bottom: 25px"> I am  Anil Kumar Panigrahi , Founder of Anil Labs. and This is mail demo of How to send email using HTML templates in Codeigniter </p>
</div>
</body>
</html>

Step 2: Controller method to send mails

Path : /application/controllers/sendmails.php

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

class Sendmails extends CI_Controller {

    public function htmlmail(){
        $config = Array(       
            'protocol' => 'sendmail',
            'smtp_host' => 'your domain SMTP host',
            'smtp_port' => 25,
            'smtp_user' => 'SMTP Username',
            'smtp_pass' => 'SMTP Password',
            'smtp_timeout' => '4',
            'mailtype'  => 'html',
            'charset'   => 'iso-8859-1'
        );
        $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
   
        $this->email->from('your mail id', 'Anil Labs');
        $data = array(
             'userName'=> 'Anil Kumar Panigrahi'
                 );
        $this->email->to($userEmail);  // replace it with receiver mail id
    $this->email->subject($subject); // replace it with relevant subject
   
        $body = $this->load->view('emails/anillabs.php',$data,TRUE);
    $this->email->message($body);  
        $this->email->send();
    }
       
}

Just replace your username , password and mail-ids and use it as HTML templates for sending mails in Codeigniter

Step 3: Call the controller method in as per your requirement.

http://[domain name]/Sendmails/htmlmail

32 Comments

  • Reply
    ravi
    June 30, 2015 at 9:51 am

    not working dude

    • Reply
      Kumar
      July 26, 2018 at 8:01 am

      $config = Array(
      ‘protocol’ => ‘smtp’,
      ‘smtp_host’ => ‘yourstmphostname’,
      ‘smtp_port’ => stmpport,
      ‘smtp_user’ => ‘[email protected]’,
      ‘smtp_pass’ => ‘youremailpassword’,
      ‘mailtype’ => ‘html’,
      ‘charset’ => ‘iso-8859-1’,
      ‘wordwrap’ => TRUE
      );

      $email = $this->input->post(’email’);
      $subject = “Hi, “.$this->input->post(‘name’).”. Your Booking Is Success! – “;
      $message = ”

      Booking ID
      :     “.$bid.”

      Name
      :     “.$this->input->post(‘name’).”

      Mobile
      :     “.$this->input->post(‘mobile’).”

      Date
      :     “.$this->input->post(‘date’).”

      Service
      :     “.$this->input->post(‘service’).”

      Message
      :     “.$this->input->post(‘message’).”

      Contact Us For More Information : >> Click Here << “;

      $this->load->library(’email’, $config);
      $this->email->set_newline(“\r\n”);
      $this->email->from(‘[email protected]’);
      $this->email->to($email);
      // $this->email->cc(‘[email protected]’);
      // $this->email->bcc(‘[email protected]’);
      $this->email->subject($subject);
      $this->email->message($message);
      //$this->email->attach(‘C:\Users\xyz\Desktop\images\abc.png’);
      if($this->email->send())
      {
      echo ‘Email send.’;
      }
      else
      {
      show_error($this->email->print_debugger());
      }

  • Reply
    Bhumi Shah
    October 28, 2015 at 4:13 pm

    This was such a great article, Meagan. Thanks for writing it.

  • Reply
    eka wijaya
    October 30, 2015 at 3:20 am

    Thank you very much, This article helped me.

  • Reply
    Ducsi
    November 10, 2015 at 9:07 am

    Thanks for the article! It helped me a lot!

  • Reply
    eddie
    November 24, 2015 at 1:15 am

    Thank you very much, This is useful.

  • Reply
    Sivaji
    January 11, 2016 at 12:02 pm

    Thank for the clean and nice article, I am having a question, I Implemented e-mail template functionality for signup and forgot password pages . I didn’t do the authentication with username and password but it worked fine for me. After few days later the system not sending mail’s to signup user’s and forgot password user, If I disable template attachment(//$this->email->message($template);) mail is sending if i enable e-mail attachment mail is not sending but in script it was showing success message, could you please help me on this thanks in advance. Here is my code

    $config = Array(
    ‘mailtype’ => ‘html’,
    ‘charset’ => ‘utf-8’,
    ‘wordwrap’ => TRUE
    );
    $this->load->library(’email’, $config);
    $this->email->from(‘[email protected]’);
    $this->email->to($_POST[’emailId’]);
    $this->email->subject(‘Reset Password’);
    //$template = $this->load->view(’emailtemplates/forgotpassword’, $data, true);
    $template = $this->load->view(’emailtemplates/mailtemplate’, $data, true);
    $this->email->message($template);
    if ($this->email->send()) {
    $this->load->view(’emailtemplates/passwordalert’);
    } else {
    $this->load->view(’emailtemplates/alertmessage’);
    echo “mail sending failed please try agin”;
    }

  • Reply
    Alberto
    February 16, 2016 at 2:23 pm

    I send emails but got the html code as body, I had to add the headers to get correct content

    $this->email->set_header('MIME-Version', '1.0; charset=utf-8');
    $this->email->set_header('Content-type', 'text/html');

    It was very helpful; thanks, mate.

    • Reply
      john
      February 25, 2016 at 2:35 am

      thank you..

  • Reply
    GTE
    February 22, 2016 at 4:42 am

    Html code displayed in the email. Why?

    • Reply
      Farhan Ghaffar
      March 27, 2017 at 5:04 pm

      Because you should use mailtype as html

  • Reply
    Muhammad
    March 10, 2016 at 12:39 pm

    Hi ! can we send more then one message in email

    • Reply
      Muhammad
      March 14, 2016 at 12:25 pm

      plz help me , i want send both template and message in single mail , If u have any suggestion then plz share with me

  • Reply
    Arun Verma
    March 31, 2016 at 7:22 am

    Great article
    it is very helpful for me
    thanks

  • Reply
    Shojol
    May 12, 2016 at 12:47 pm

    Why the Html code displaying in the email.that make problem.

  • Reply
    Chaudhary Sushil
    July 15, 2016 at 8:05 pm

    Thanks for your great neat and clean example.
    But I want to send a email in on array like this

    $data = array(
    ‘username’ =>$this->input->post(‘username’),
    ‘useremail’ =>$this->input->post(‘useremail’)
    );

    All contact form data send successfully but the email not send. I don’t understand why happens this to me. Is there any validation or rule which I missed.
    If you have any idea about this, so pleas tell me. I am really very tiered about this.

  • Reply
    Shafiul
    October 1, 2016 at 9:04 am

    I got help and sent email by creating html in a variable of PHP and write inline css and send by codeigniter email class. Thanks to Anil.

  • Reply
    juancho
    October 4, 2016 at 5:09 pm

    great and simple article. very usefull

  • Reply
    birtha
    October 12, 2016 at 11:05 am

    thanks bro….

  • Reply
    jibin
    December 5, 2016 at 11:43 am

    not working dued

  • Reply
    Ganesh
    December 30, 2016 at 12:02 pm

    Hello sir,
    Thanks for sharing this article so I have used your article and got a required result.

  • Reply
    nikkolai fernandez
    January 27, 2017 at 12:36 am

    thank you was a good solution 🙂

  • Reply
    Anil
    June 12, 2017 at 6:23 am

    Hi,
    I am trying to send emails from codeigniter. Now i want to send mail such that the from adress woulb be fetched from view file and i will use the same in controller. I would be getting email and name from the view file. I want to set these details (from->name and replyto-> email) in controller. Although CI is providing set_headers, i am not able to do this. Please help me. Thanks in advance.

  • Reply
    sanket
    July 19, 2017 at 12:53 pm

    i have used mailtype as html, HTML tags are being displayed plz help me for solution

  • Reply
    manu
    August 12, 2017 at 10:58 am

    why this use type

  • Reply
    James
    November 8, 2017 at 7:31 am

    Hello,

    Why are you including SMTP values, when the protocol you’re using is sendmail?

  • Reply
    Generate token key in the registration of CodeIgniter application - Anil Labs
    December 7, 2017 at 10:49 am

    […] You can use html templates to send mail, which already explained in previous mail i.e. send email using HTML templates in Codeigniter. […]

  • Reply
    Sneha
    February 1, 2018 at 8:26 am

    I have set all the headers but m not able to see the body(message) in mail i recieved.
    Any suggestion.

  • Reply
    Abdus salam khan
    March 16, 2018 at 7:09 am

    I want send mail on outlook using html template in Codeigniter, html template display in plain text formate on outlook

  • Reply
    Anil Rawat
    May 10, 2018 at 7:29 am

    Not Working

    Here My Code

    $this->load->library(’email’);
    $config = array (
    ‘mailtype’ => ‘html’,
    ‘charset’ => ‘utf-8’,
    ‘priority’ => ‘1’
    );
    $this->email->initialize($config);
    $data = array(
    ‘message’=> ‘Please find below the link ‘,
    );
    $body = $this->load->view(‘template.php’,$data,TRUE);
    $this->email->from(‘abcWeb.com’, ‘System’);
    $this->email->to(‘[email protected]’);
    $this->email->subject(“Greeting”);
    $this->email->message($body);
    $this->email->send();

  • Reply
    Sending HTML template emails using SMTP in NodeJS - Anil Labs
    August 13, 2018 at 6:50 am

    […] the earlier posts, I had explained how to send emails in CodeIgniter. Now I would like to explain how to send HTML template emails using SMTP in […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.