<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tech Mug &#187; Newsletter</title>
	<atom:link href="http://www.techmug.com/tag/newsletter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techmug.com</link>
	<description></description>
	<lastBuildDate>Tue, 31 Jan 2012 12:15:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Send Emails / Newsletters using HTML templates and PHP</title>
		<link>http://www.techmug.com/send-emails-newsletters-using-html-templates-and-php/</link>
		<comments>http://www.techmug.com/send-emails-newsletters-using-html-templates-and-php/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 17:44:44 +0000</pubDate>
		<dc:creator>Fraz Ahmed</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Newsletter]]></category>
		<category><![CDATA[phpmailer]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.techmug.com/?p=23</guid>
		<description><![CDATA[HTML emails look attractive, provide easy calls of action. And importantly creates your brand image in the eyes of your clients. But it becomes very difficult to manage your HTML emails if you want to change the look and feel of your email design. Or you may be interested in changing the content of your [...]]]></description>
			<content:encoded><![CDATA[<p><strong>HTML emails </strong>look attractive, provide easy calls of action. And importantly creates your brand image in the eyes of your clients. But it becomes very difficult to manage your HTML emails if you want to change the look and feel of your email design. Or you may be interested in changing the content of your HTML email without playing with your code.</p>
<p>In this tutorial we will be<strong> sending HTML emails</strong> using PHP. This tutorial will teach you to keep your HTML presentation separate from your PHP code.</p>
<p><span id="more-23"></span></p>
<p style="text-align: center;"><a href="http://www.techmug.com/demo/html-email/html-email.zip"><img class="alignnone size-full wp-image-337" title="Download Source Code" src="http://www.techmug.com/wp-content/uploads/2010/10/btn-source-code.jpg" alt="Download Source Code" width="140" height="90" /></a></p>
<h2><strong>How it works?</strong></h2>
<p>Basically there are only three steps to send your branded HTML emails.</p>
<ol>
<li>Read HTML template file from your specified location.</li>
<li>Replace variables with your data, if any.</li>
<li>Finally send email.</li>
</ol>
<h2><strong>What we need?</strong></h2>
<p>Before we move to define the above steps, its worth to mention the requirements of this tutorial. The first thing we need to send emails is PHPMailer class. To learn more about PHPMailer class please visit the official website <a title="PHPMailer" href="http://phpmailer.worxware.com/" target="_blank">here</a>. And the last other thing is your HTML template file.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "2820701010";
google_ad_width = 336;
google_ad_height = 280;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<h2><strong>Step by step learning</strong></h2>
<p>Here are the steps you need to take in order to develop your own email sending program.</p>
<p><strong>First</strong> of all you need to include the PHPMailer class in your code. To do so you use below code:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
#####################################
# Include PHP Mailer Class
#####################################
require(&quot;class.phpmailer.php&quot;);
?&gt;
</pre>
<p>Before moving further I would like to introduce two PHP functions we will use in our code. First one is for sending emails and second is for reading your <strong>HTML template</strong> file. First function (sendEmail) accepts five parameters and return false if email is not sent and returns true if email is sent. Parameters are self explanatory in below code:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
#####################################
# Function to send email
#####################################
function sendEmail ($fromName, $fromEmail, $toEmail, $subject, $emailBody) {
$mail = new PHPMailer();
$mail-&gt;FromName = $fromName;
$mail-&gt;From = $fromEmail;
$mail-&gt;AddAddress(&quot;$toEmail&quot;);

$mail-&gt;Subject = $subject;
$mail-&gt;Body = $emailBody;
$mail-&gt;isHTML(true);
$mail-&gt;WordWrap = 150;

if(!$mail-&gt;Send()) {
return false;
} else {
return true;
}
}
?&gt;
</pre>
<p>The other function (readTemplateFile) reads your HTML template file and returns all the stuff found in that file. This function accepts only one parameter which is the location of your HTML file. Below is function:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
#####################################
# Function to Read a file
# and store all data into a variable
#####################################
function readTemplateFile($FileName) {
$fp = fopen($FileName,&quot;r&quot;) or exit(&quot;Unable to open File &quot;.$FileName);
$str = &quot;&quot;;
while(!feof($fp)) {
$str .= fread($fp,1024);
}
return $str;
}
?&gt;
</pre>
<p><strong>Finally</strong> you need to call and pass parameters to above function to complete the task of sending HTML email. Yeah! really we almost done it. Ok we are using a template which send username and password of a user. Please see HTML template file in downloaded file. We break our last and final step in three pieces:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Data to be sent (Ideally fetched from Database)
$NameOfUser = &quot;XYZ&quot;;
$Username = &quot;abcdef&quot;;
$password = &quot;123456&quot;;
$UserEmail = &quot;receiver.email@somedomain.com&quot;;
?&gt;
</pre>
<p>First of all we put data into some variables. These variables will be used later in our code. Ideally this data will be fetched from database in real world.</p>
<p>Now we put our HTML template data into other variable:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Send email to user containing username and password
//Read Template File
$emailBody = readTemplateFile(&quot;template.html&quot;);
?&gt;
</pre>
<p>So we have all our file data in a variable ($emailBody). And we may change anything in this variable. We will use PHP&#8217;s str_replace function to replace the data:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Replace all the variables in template file
$emailBody = str_replace(&quot;#name#&quot;,$NameOfUser,$emailBody);
$emailBody = str_replace(&quot;#username#&quot;,$Username,$emailBody);
$emailBody = str_replace(&quot;#password#&quot;,$password,$emailBody);
?&gt;
</pre>
<p>To understand the above code we take first line of code. It simply replace #name# with the value of $NameOfuser variable in $emailBody variable. And now our new replaced content is stored in $emailBody variable, so now we can use this content to be passed in our email sending function below:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//Send email
$emailStatus = sendEmail (&quot;Sender Name&quot;, &quot;some.email@yourdomain.com&quot;, $UserEmail, &quot;Email Subject&quot;, $emailBody);
?&gt;
</pre>
<p>If email is not sent successfully, we will display error message else we will show success message:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
//If email function return false
if ($emailStatus != 1) {
echo &quot;An error occured while sending email. Please try again later.&quot;;
} else {
echo &quot;Email with account details were sent successfully.&quot;;
}
?&gt;
</pre>
<p>That&#8217;s all! Now you have fully functional code to send your branded emails. If in future you want to change the design of your email, you just have to replace HTML template file. There is no need of changing your PHP code. Ideally you can use this code to send:</p>
<ul>
<li>Newsletters</li>
<li>Registration Details</li>
<li>Forgot Password email</li>
<li>And for anything you want</li>
</ul>
<p>Besides the benefits of using above code there is one shortcoming too of this code. If your user&#8217;s email client does not support <em>HTML email</em>, he/she will not be able to see your fancy deigned email. But there are ways to prevent this shortcoming.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-7217075623453662";
google_ad_slot = "4831550702";
google_ad_width = 300;
google_ad_height = 250;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<center><font-size="9px">Advertisement</font></center></p>
<p>Hope so you enjoyed this tutorial. Please feel free to ask or comment for this tutorial. And Don’t forget to share the post on your favorite website. It may also help someone somewhere <img src='http://www.techmug.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Send your <strong>newsletters using HTML templates and PHP</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.techmug.com/send-emails-newsletters-using-html-templates-and-php/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>

