How to send email using HTML templates in Codeigniter
Date Posted:24-05-2017
In this post we will explain how to send email using HTML templates in Codeigniter
How to send email using HTML templates in Codeigniter
Step 1: Create HTML file with suits to your template.
Path : /application/views/emails/test-email.php
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Codeigniter mail templates</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head> <body> <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"> This is mail demo of How to send email using HTML templates in Codeigniter </p> </div> </body> </html>
Step 2: Create new Controller name as sendmails.php,Add method to sendmails.php
Path : /application/controllers/sendmails.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Sendmails extends CI_Controller { public function htmlmail(){ $userEmail='email@domain.com' $subject='Test' $config = Array( 'mailtype' => 'html', 'charset' => 'utf-8', 'priority' => '1' ); $this->load->library('email', $config); $this->email->set_newline("\r\n"); $this->email->from('your mail id', 'Pheonix solutions'); $data = array( 'userName'=> 'Pheonix solutions' ); $this->email->to($userEmail); // replace it with receiver mail id $this->email->subject($subject); // replace it with relevant subject $body = $this->load->view('emails/test-email.php',$data,TRUE); $this->email->message($body); $this->email->send(); } } ?>
Step 3: Call the controller method in as per your requirement.
http://[domain name]/Sendmails/htmlmail
Thank you! it’s worked well.
but no styling.
I used a simple Bootstrap page template. but not styles or color applied.
anyway, thank you
you need to style it inline