Email with customized HTML template using Codeigniter - Mostlikers

09 March, 2016

Email with customized HTML template using Codeigniter

It's a new topic related to Codeigniter. In this post, I will explain about, how to send an email with a customized HTML template using Codeigniter. You have to set some configuration to send an email in codeigniter library function.

Email with customized HTML template using Codeigniter


Live Demo              Download


Codeigniter Email Library:

Codeigniter has inbuilt library that supports the email functionalities and simplifies the process of sending email. This library supports the following features:
  • Multiple Protocols: Mail, Sendmail, and SMTP.
  • Multiple recipients.
  • CC and BCCs.
  • HTML or Plaintext email.
  • Attachments.
  • Word wrapping.
  • Priorities.
  • BCC Batch Mode, enabling large email lists to be broken into small BCC batches.
  • Email Debugging tools.


Send Email:

Sending an email in Codeigniter is very simple and here is an example.
Note: The below code will be in one of your controllers.

$this->load->library('email');
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com'); 
$this->email->cc('another@another-example.com'); 
$this->email->bcc('them@their-example.com'); 
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger();

Now let's consider that you have more than one controller and you have to send an email from multiple controllers in that case instead of calling the email library in all the controllers, let us create a common model to fetch the email library. Below is the code to create a common model in Codeigniter.

<?php 
class Welcome extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('email');
    }
}

The common model code with the email code would be as below,

<?php 
class Common_model extends CI_Model {
        
    public function send_mail($from,$to,$subject,$message,$cc,='')
    {
        $this->load->library('email'); 
        $this->email->set_mailtype("html"); 
        $this->email->from($from,'Mostlikers');
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);  
        $this->email->send();       
        return$this->email->print_debugger(); 
    }
}

You can access this model in any controller no need write email code in your controller load this model to the controller and just call the emaifunction. Path to where you have to create the common model is application/models/common_model.php

HTML Email Template

Create HTML email template to send an email through codeigniter. For demonstration purpose, the template path will be application/views/template.php 

<html><body>
<h2 style"color:red;font-weight:bold;">
Codeigniter Send Email with HTML Template/h2>
<table>
    <tr style="color:green;font-weight:bold;">
        <td>Author :<t/d>
        <td>Karthick</td>
    </tr>
    <tr style="color:green;font-weight:bold;">
        <td>Author :<t/d>
        <td>Arun</td>
    </tr>
</table>
</body>
</html>

Editing the controller

To send an email we have to create an email function in the controller load the common model in the controller as we are calling the email library in common model. the code is as follows.
<?php 
class Welcome extends CI_Controller {
    public function __construct()
    {
       parent::__construct(); 
       $this->load->model('common_model'); 
    }

    public function mail_template()
    {
        $from    = "info@mostlikers.com";
        $to      = "mostlikers@gmail.com";
        $subject = "Demo mail testing"; 
        $msg = $this->load -> view('template',True);
        $mail = $this -> common_model->send_mail($from,$to,$subject,$msg);
    }
}

Code Explained:

  • $from holds the sender email id.
  • $to holds the receiver email id.
  • $subject holds the subject and purpose of the email
  • $msg holds the content of the email. This is where you load the customized HTML template.
  • $mail calls the email library in the common model and passes the value of from, to, subject and the email content.










Related Topics

2 comments:

  1. I tried what has been shown above " $msg = $this->load -> view('template',True);" and found that it was not loading properly as it would give a message: Message: Object of class CI_Loader could not be converted to string
    Filename: libraries/Email.php. I solved the problem by adding a third parameter. "$this->load->view("template","",TRUE);" and it worked perfectly. Thank you

    ReplyDelete
  2. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
    Mason Soiza

    ReplyDelete