How to create email templates and send emails using PHP

How to create email templates and send emails using PHP : sending HTML mail through PHP mail()

Creating an email template

To create a template some points need to be noted:

  • Template is create using table format of HTML. This is the best way to keep the email layout responsive.
  • Styles need to be inline.

<!------ template code ------------------------->
<html>
<head>
   <title>Welcome to Scanfcode</title>
</head>
<body style='padding:0;'>
 <table style='width:100%;background-color:#fff;border:solid 1px #ccc;text-align:center;'>
    <tr style='background-color:#11609e;'>
       <td colspan="4">
          <span style='color:#f2f2f2;padding:1px 10px;'>Thanks for signing up</span>
          <span style='color:#f2f2f2;float:right;padding:1px 10px'>Go to website?
          <a href='http://scanfcode.com/' style='color:#e6e6e6;'>Click here</a></span>
       </td>
    </tr>
    <tr>
       <td colspan="4" style='width:100%;padding:20px;border:solid 1px #ccc;border-width:1px 0px;'>
          <img src="scanfcode.png" alt="How to create email templates and send emails using PHP" />
       </td>    
   </tr>
   <tr>
      <td colspan="4" style='color:#11609e;margin:10px;font-size:30px'>
        <b>Welcome to Scanfcode</b>
      </td>
   </tr>
   <tr>
      <td  colspan="4" style='padding:20px;'>
          <hr>
          <h3 style='font-weight:normal'> Learn Coding</h3>
      </td>
   </tr>
   <tr style="background-color:#11609e;color:#fff;text-transform:uppercase;font-size:120%;">
       <td style='padding:20px 0px;border-right:solid 1px #e6e6e6;'>C/c++</td> 
       <td style='border-right:solid 1px #e6e6e6;'>HTML</td>
       <td style='border-right:solid 1px #e6e6e6;'>CSS</td>
       <td>Java</td>
   </tr>
    <tr style="background-color:#11609e;color:#fff;text-transform:uppercase;font-size:22px;">
         <td style='padding:20px 0px;border-right:solid 1px #e6e6e6;'><hr>JAVAscript</td> 
         <td style='border-right:solid 1px #e6e6e6;'><hr>php</td>
         <td style='border-right:solid 1px #e6e6e6;'><hr>android</td>
         <td><hr>mysql</td>
     </tr>
     <tr>
          <td colspan="4"><hr>
                <button style="height:40px;width:160px;border-width:0px;background-color:orange;">
                <a href='http://scanfcode.com' style='color:#fff;'>Get Started</a></button><hr>
          </td>
     </tr>
     <tr>
        <td colspan="4">
          <p style='color:#333;margin:80px 40px;line-height:30px;'>
Scanfcode.com “CODE WANTS TO BE SIMPLE”  is an initiative  to help the upcoming programmers with the code. Scanfcode focuses on providing the most efficient code or snippets as the code wants to be simple. We will help programmers build up concepts in different programming languages that include
          </p>
       </td>
    </tr>
    <tr style='background-color:#000;'>
       <td colspan="4">
         <p style='color:#fff;font-size:14px;'>Copyright © 2016-2017 Scanfcode.com. All Rights Reserved.</p>
         <br>
         <p style='font-size:12px;color:#f2f2f2;padding:20px'>Scanfcode is a technical blog which tends to spread knowledge in field of programming languages like C, C++, HTML, CSS, Java, JavaScript, PHP, Android, MySQL. Code snippets at Scanfcode are original and help one build fast. Made in India.
         </p>
          <br>
          <a href='http://scanfcode.com/privacy-policy/' style='color:orange;padding:10px;'>Privacy Policy</a>
      </td>
    </tr>
  </table>
</body>
</html>

Template code output:

Thanks for signing upGo to website?Click here
How to create email templates and send emails using PHP
Welcome to Scanfcode

Learn Coding

C/c++HTMLCSSJava

JAVAscript

php

android

mysql


Scanfcode.com “CODE WANTS TO BE SIMPLE” is an initiative to help the upcoming programmers with the code. Scanfcode focuses on providing the most efficient code or snippets as the code wants to be simple. We will help programmers build up concepts in different programming languages that include

Copyright © 2016-2017 Scanfcode.com. All Rights Reserved.

Scanfcode is a technical blog which tends to spread knowledge in field of programming languages like C, C++, HTML, CSS, Java, JavaScript, PHP, Android, MySQL. Code snippets at Scanfcode are original and help one build fast. Made in India.

Privacy Policy

How to create email templates and send emails using PHP

Combining Template with PHP mail function

Composing mail

$to ="scanfocode@scanfcode.com"; //can be any user email id (input from user also)
$subject = "WELCOME TO SCANFCODE"; //subject as it appears in the mail
$message = "<!--  code for the template comes here---> 
       (from <html> to </html> complete)";


Sending mail


// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <alerts@scanfcode.com>' . "\r\n";
$headers .= 'Cc: alerts@scanfcode.com' . "\r\n";

mail($to,$subject,$message,$headers);

Leave a Reply

Your email address will not be published. Required fields are marked *