How to use email template in cakephp

Cakephp

Posted on 30 Dec 15 by Vickey
  1462



By using email template we can change our email content very easily.Because we have all templete in a differente directory and if we need to change any email content we edit only the templete file and there is no need to search content in our controllers.So lets start work with email templates step by step

Step1: Create a directory 'emails' under '/app/webroot/' and here create your first templete  'welcome.html'

Step2: Add your email content in this html file 

<html>
<head><title>Welcome to {SITE_TITLE}!</title></head>
<body>
<div style="max-width: 800px; margin: 0; padding: 30px 0;">
<table width="80%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="5%"></td>
<td align="left" width="95%" style="font: 13px/18px Arial, Helvetica, sans-serif;">
<h2 style="font: normal 20px/23px Arial, Helvetica, sans-serif; margin: 0; padding: 0 0 18px; color: black;">
Hi {USER}, 
<br>Welcome to {SITE_NAME}!</h2>
Thanks for joining {SITE_NAME}.<br />
Best wishes<br />
{SITE_NAME}
</td>
</tr>
</table>
</div>
</body>
</html>

You have created your first email template now we will use it in controllers

$templetedata = file_get_contents("Your website base url" . "emails/welcome.html");

here we get all content from templete now we will replace our dynamic data for example name or site url etc

$dynamicValues = array(
	"{USER}" => 'Username',
	"{SITE_NAME}" => 'example.com'
);

$emailmsg = @str_replace(array_keys($dynamicValues), array_values($dynamicValues), $templetedata);

and now send this email message

$this->sendMail($emailmsg);

We have done with email templates if you have any question comments please mail me


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com