RE: How to send an email using HTML template?

I want to send an email with beautiful HTML template.

dip Team Leader Asked on March 4, 2017 in PHP.
Add Comment
1 Answers

Please follow the steps:
{1} Download class.phpmailer.php file from here
{ii}Create a html file name template.html
{iii}Create a php file name emailsend.php
{iv}Finally upload the file in root directory and test.

Note: Please change from and to email id in emailsend.php file before uploading to server.

 

Code for template.html

 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="utf-8">
 <title>Example 2</title>
 <style type="text/css">
 @font-face {
 font-family: SourceSansPro;
 src: url(SourceSansPro-Regular.ttf);
 }
.clearfix:after {
 content: "";
 display: table;
 clear: both;
 }
a {
 color: #0087C3;
 text-decoration: none;
 }
body {
 position: relative;
 width: 21cm;
 margin: 0 auto;
 color: #555555;
 background: #FFFFFF;
 font-family: Arial, sans-serif;
 font-size: 14px;
 font-family: SourceSansPro;
 }
header {
 padding: 10px 0;
 margin-bottom: 20px;
 border-bottom: 1px solid #AAAAAA;
 }
#logo {
 float: left;
 margin-top: 8px;
 }
#logo img {
 height: 70px;
 }
#company {
 float: right;
 text-align: right;
 }
#details {
 margin-bottom: 50px;
 }
#client {
 padding-left: 6px;
 border-left: 6px solid #0087C3;
 float: left;
 }
#client .to {
 color: #777777;
 }
h2.name {
 font-size: 1.4em;
 font-weight: normal;
 margin: 0;
 }
#invoice {
 float: right;
 text-align: right;
 }
#invoice h1 {
 color: #0087C3;
 font-size: 2.4em;
 line-height: 1em;
 font-weight: normal;
 margin: 0  0 10px 0;
 }
#invoice .date {
 font-size: 1.1em;
 color: #777777;
 }
table {
 width: 100%;
 border-collapse: collapse;
 border-spacing: 0;
 margin-bottom: 20px;
 }
table th,
 table td {
 padding: 20px;
 background: #EEEEEE;
 text-align: center;
 border-bottom: 1px solid #FFFFFF;
 }
table th {
 white-space: nowrap;
 font-weight: normal;
 }
table td {
 text-align: right;
 }
table td h3{
 color: #57B223;
 font-size: 1.2em;
 font-weight: normal;
 margin: 0 0 0.2em 0;
 }
table .no {
 color: #FFFFFF;
 font-size: 1.6em;
 background: #57B223;
 }
table .desc {
 text-align: left;
 }
table .unit {
 background: #DDDDDD;
 }
table .qty {
 }
table .total {
 background: #57B223;
 color: #FFFFFF;
 }
table td.unit,
 table td.qty,
 table td.total {
 font-size: 1.2em;
 }
table tbody tr:last-child td {
 border: none;
 }
table tfoot td {
 padding: 10px 20px;
 background: #FFFFFF;
 border-bottom: none;
 font-size: 1.2em;
 white-space: nowrap;
 border-top: 1px solid #AAAAAA;
 }
table tfoot tr:first-child td {
 border-top: none;
 }
table tfoot tr:last-child td {
 color: #57B223;
 font-size: 1.4em;
 border-top: 1px solid #57B223;
}
table tfoot tr td:first-child {
 border: none;
 }
#thanks{
 font-size: 1.2em;
 margin-bottom: 50px;
 }
#notices{
 padding-left: 6px;
 border-left: 6px solid #0087C3;
 }
#notices .notice {
 font-size: 1.2em;
 }
footer {
 color: #777777;
 width: 100%;
 height: 30px;
 position: absolute;
 bottom: 0;
 border-top: 1px solid #AAAAAA;
 padding: 8px 0;
 text-align: center;
 }
</style>
 </head>
 <body>
 <header class="clearfix">
 <div id="logo">
 <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcT9cZKgjB2fkCdjqx1HTeastE3M5-OUE_TxlyJ-r-SYZGzJjENL">
 </div>
 <div id="company">
 <h2 class="name">YOUR COMPANY NAME</h2>
 <div>Street,city,state,country</div>
 <div>(+91) 9876543210 </div>
 <div><a href="mailto:support@shubhimpex.net">info@domain.com</a></div>
 </div>
 </div>
 </header>
 <main>
 <div id="details" class="clearfix">
 <div id="client">
 <div class="to">Thank you for registering with us.</div>
</div>
</div>
 <table border="0" cellspacing="0" cellpadding="0">
 <thead>
 <tr>
<th class="desc">USERNAME</th>
 <th class="unit">EMAIL</th>
 <th class="qty">PASSWORD</th>
</tr>
 </thead>
 <tbody>
 <tr>
<td class="desc">#username#</td>
 <td class="unit">#useremail#</td>
 <td class="qty">#password#</td>
</tr>
</tbody>
</table>
 <div id="thanks"></div>
 <div id="notices">
 <div></div>
 <div class="notice">©2017 YOUR COMPANY NAME</div>
 </div>
 </main>
 <br/>
</body>
 </html>
 

 

Code for mailsend.php

 

 <?php
 # Include PHP Mailer Class
 require("class.phpmailer.php");
 ?>
 <?php
 # Function to send email
function sendEmail ($fromName, $fromEmail, $toEmail, $subject, $emailBody) {
 $mail = new PHPMailer();
 $mail->FromName = $fromName;
 $mail->From = $fromEmail;
 $mail->AddAddress("$toEmail");
$mail->Subject = $subject;
 $mail->Body = $emailBody;
 $mail->isHTML(true);
 $mail->WordWrap = 150;
if(!$mail->Send()) {
 return false;
 } else {
 return true;
 }
 }
# Function to Read a file
 # and store all data into a variable
function readTemplateFile($FileName) {
 $fp = fopen($FileName,"r") or exit("Unable to open File ".$FileName);
 $str = "";
 while(!feof($fp)) {
 $str .= fread($fp,1024);
 }
 return $str;
 }
 ?>
 <?php
# Finally send email
//Data to be sent (Ideally fetched from Database)
 $NameOfUser = "XYZ";
 $Username = "abcdef";
 $password = "123456";
 $UserEmail = "receiver.email@somedomain.com";
//Send email to user containing username and password
 //Read Template File
 $emailBody = readTemplateFile("template.html");
//Replace all the variables in template file
 $emailBody = str_replace("#name#",$NameOfUser,$emailBody);
 $emailBody = str_replace("#username#",$Username,$emailBody);
 $emailBody = str_replace("#password#",$password,$emailBody);
$emailBody = str_replace("#useremail#",$UserEmail,$emailBody);
//Send email
 $emailStatus = sendEmail ("Sender Name", "some.email@yourdomain.com", $UserEmail, "Email Subject", $emailBody);
//If email function return false
 if ($emailStatus != 1) {
 echo "An error occurred while sending email. Please try again later.";
 } else {
 echo "Email sent successfully.";
 }
 ?>
 
Team Leader Answered on March 6, 2017.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.