3 Ways To Send Email With HTML Template In PHP

Welcome to a tutorial on how to send emails in PHP with HTML templates. So you want to use an HTML file as an email template? Well, there are a few ways to do it – Read on for the examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Click here to download

The example code is released under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP-HTML EMAIL TEMPLATES

All right, let us now get into the examples of creating HTML email templates in PHP.

 

TUTORIAL VIDEO

 

EXAMPLE 1) HTML TEMPLATE WITH STRING REPLACE

1A) HTML TEMPLATE

1a-template.html
<html><body>
  <p>Hi {NAME}!</p>
  <p>Sent at <strong>{TIME}</strong>.</p>
</body></html>

This is probably one of the most common and traditional methods – Not just for PHP, but for “most other programming languages” as well. Just create the HTML template, and put {VARIABLE} placeholders where you want to use variables.

 

1B) PHP STRING REPLACE

1b-replace.php
// (A) SETTINGS
$subject = "DEMO TEMPLATE";
$to = "joe@doe.com";
$name = "Joe";
$date = date("Y-m-d H:i:s");
 
// (B) READ TEMPLATE & STRING REPLACE
$html = file_get_contents("1a-template.html");
$html = str_replace("{NAME}", $name, $html);
$html = str_replace("{TIME}", $date, $html);
 
// (C) SEND!
mail($to, $subject, $html, implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]));

This should be pretty self-explanatory. Read the entire HTML file into a string, then do str_replace() for all {VARIABLE} placeholders.

 

 

EXAMPLE 2) HTML TEMPLATE WITH PHP VARIABLES

2A) HTML TEMPLATE

2a-template.php
<html><body>
  <p>Hi <?=$name?>!</p>
  <p>Sent at <strong><?=$date?></strong>.</p>
</body></html>

Take note, this is a PHP file with the usual short variable tags <?=$VARIABLE?>.

 

2B) OUTPUT BUFFER MAGIC

2b-php-var.php
// (A) SETTINGS
$subject = "DEMO TEMPLATE";
$to = "jon@doe.com";
$name = "Jon";
$date = date("Y-m-d H:i:s");
 
// (B) OUTPUT BUFFER MAGIC
ob_start();
include "2a-template.php";
$html = ob_get_contents();
ob_end_clean();
 
// (C) SEND!
mail($to, $subject, $html, implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]));

What kind of sorcery is this? For the uninitiated, you should know the following:

  • $name = "Jon";
  • $date = date("Y-m-d H:i:s");
  • include "2a-template.php";

Yes, PHP will instantly parse and output the HTML. So we change that behavior with output buffering.

  • ob_start() Start output buffering.
  • include "2a-template.php" Parse the variables and HTML. But it will be stored in the buffer, not directly output.
  • $html = ob_get_contents() Get the HTML content from the buffer, and put it into $html as a string.
  • ob_end_clean() Clear and stop buffering.

TLDR – Use output buffering to build $html, then send it out in an email.

 

 

EXAMPLE 3) ARRAY EXTRACT

3-extract.php
// (A) EXTRACT EMAIL VARIABLES
$email = [
  "subject" => "DEMO TEMPLATE",
  "to" => "joy@doe.com",
  "name" => "Joy",
  "date" => date("Y-m-d H:i:s")
];
extract($email);
 
// (B) OUTPUT BUFFER MAGIC
ob_start();
include "2a-template.php";
$html = ob_get_contents();
ob_end_clean();
 
// (C) SEND!
mail($to, $subject, $html, implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]));

This is the same method as above, but we introduce extract(ARRAY) to make things more convenient. A quick explanation:

  • extract() is a PHP native function. It simply takes in an array and creates variables from it.
  • For example, extract(["name" => "jon", "age" => 123]) will create $name = "jon" and $age = 123.
  • So basically, we are just using extract() to quickly “map” our email template variables.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEATSHEET

PHP Send Email With Template (Click To Enlarge)

 

WHICH IS THE BEST METHOD?

All of the above methods work. However, string replacement can be resource-intensive when it comes to a long email with many variables. So, personal preference – “Wicked output buffer with extract” is the most elegant among the three.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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