Send email using HTML email template in Codeigniter - Manoj Patial

Send email using HTML email template in Codeigniter

SQL Joins Tutorial
May 24, 2018
3 Best WordPress SEO Plugins
January 16, 2019

Using HTML email template for send email in Codeigniter

In this article you’ll learn how to use email template with dynamic variables for send email to user after signup using Codeigniter email library.

Step 1: Create email message template
First of all create a PHP file in /application/views/emails/ folder which contains your email message with HTML code. In this example I am creating a file with name new_user.php.

<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″ />
<title>Codeigniter email template</title>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″ />
</head>
<body>
<div>

<style>*{margin:0px;padding:0px;}</style>
<table bgcolor=”#ececec” style=”border-bottom:1px solid #e0e0e0;width: 100%;margin:0px;”>
<tr>
<td style=”display:block!important;max-width:600px!important;margin:0 auto!important;clear:both!important;”>
<div style=”padding:15px;max-width:600px;margin:0 auto;display:block;”>
<table bgcolor=”#29BAB2″>
<tr>
<td><img src=”http://manojpatial.com/wp-content/uploads/2018/04/mp-logo-big.png” /></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<table style=”width: 100%;”>
<tr>
<td style=”display:block!important;max-width:600px!important;clear:both!important;margin:0 auto;” bgcolor=”#FFFFFF” align=”center”>
<div style=”padding:15px;max-width:600px;display:block;margin:0 auto;”>
<table>
<tr>
<td style=”font-family: Open Sans,arial,sans-serif; font-size:16px;”>
<h3 style=”font-family: Open Sans,arial,sans-serif; font-size:22px;margin-bottom:10px;”>Dear {name}</h3>
<p style=”font-family: Open Sans,arial,sans-serif; font-size: 16px;margin-bottom:20px;”>Thanks for signing up to http://manojpatial.com</p>
<p style=”font-family: Open Sans,arial,sans-serif; font-size: 16px;margin-bottom:20px;”>Please find your login details below</p>
<p style=”margin-bottom: 10px;line-height:1.6;font-family: Open Sans,arial,sans-serif; font-size: 16px;”>
Url: {URL}<br/>
Login Email: {user_name}<br/>
Password: {password}<br/>
</p><br/>
<p style=”margin-bottom: 10px;line-height:1.6;font-family: Open Sans,arial,sans-serif; font-size: 16px;”>
Thanks & Regards,<br/>Manoj Patial<br/></p>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</body>
</html>

Step 2: Controller method for send email to user

In controller method we’ll see how to call template and assign value to variables set in email template.

public function htmlmail(){

		//setup SMTP configurion
		$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); // Load email template
		$this->email->set_newline("\r\n");
		$this->email->from('your mail id', 'Anil Labs');

		$data = array(
			'name'=> 'Manoj Patil',
			'URL'=> 'http://manojpatial.com/login',
			'user_name'=> 'manojpatil',
			'password'=> 'welcome',

        );

		$this->email->to($user_email); // replace it with receiver email id
		$this->email->subject($subject); // replace it with email subject
		$message = $this->load->view('emails/new_user.php',$data,TRUE);

		$this->email->message($message); 
		$this->email->send();

	}

This method can be call when user signup with website.

Manoj Patial
Manoj Patial
I am Manoj Patial, a website developer from India, I have more than 10 years of experience in website development. I developed more than 200+ website using Drupal, WordPress, PHP, Codeigniter, shopify, HubSpot and other CMS/PHP frameworks.

Leave a Reply

avatar
  Subscribe  
Notify of

Pin It on Pinterest