PHP Send Email With HTML Template (Very Simple Examples)

Welcome to a tutorial on how to send emails in PHP with HTML templates. So rather than hard coding an email in PHP, do you want to use an HTML file as a template instead?

To use an HTML file as an email template, simply read it as a string and put it into the PHP mail function:

  • $to = "jon@doe.com";
  • $subject = "Test";
  • $body = file_get_contents("template.html");
  • $head = "MIME-Version: 1.0\r\n";
  • $head .= "Content-type: text/html; charset=utf-8";
  • mail($to, $subject, $body, $head);

That covers the quick idea behind HTML email templates in PHP. But what if we want to introduce variables into the template? Read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Email Template Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

PHP-HTML EMAIL TEMPLATES

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

 

EXAMPLE 1) HTML TEMPLATE WITH STRING REPLACE

1A) HTML TEMPLATE

1a-template.html
<html><body>
  <h1>It Works.</h1>
  <p>Hi {NAME}!</p>
  <p>Sent from an HTML file at <strong>{TIME}</strong>.</p>
</body></html>

This is probably one of the most common and traditional methods on the Internet. Just put {VARIABLE} placeholders in your HTML template where you want to use variables.

 

1B) PHP STRING REPLACE

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

Pretty self-explanatory… Just load the entire HTML file as a string, then do str_replace().

 

 

EXAMPLE 2) HTML TEMPLATE WITH PHP VARIABLES

2A) HTML TEMPLATE

2a-template.php
<html><body>
  <h1>It Works.</h1>
  <p>Hi <?=$name?>!</p>
  <p>Sent from an HTML file 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) EMAIL SETTINGS
$to = "joe@doe.com";
$subject = "SUBJECT";
$head = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);
 
// (B) HTML TEMPLATE
// (B1) VARIABLES
$name = "Jon";
$date = date("Y-m-d H:i:s");
 
// (B2) OUTPUT BUFFER MAGIC
ob_start();
include "2a-template.php";
$html = ob_get_contents();
ob_end_clean();
 
// (C) SEND!
mail($to, $subject, $html, $head);

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

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

But here’s the problem – PHP will instantly parse and output the HTML as-it-is.

  • ob_start() will start output buffering.
  • include "2a-template.php" will parse the variables and HTML, but not directly output now. It will be stored in the buffer instead.
  • $html = ob_get_contents() grabs the HTML content from the buffer.
  • ob_end_clean() clears and stops buffering.

Yes, just a little bit of buffering magic here.

 

 

EXAMPLE 3) WICKED EXTRACT

3-extract.php
// (A) EMAIL SETTINGS
$to = "joy@doe.com";
$subject = "SUBJECT";
$head = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);
 
// (B) FUNKY TEMPLATE FUNCTION
function template ($file, $vars=null) {
  ob_start();
  if ($vars!==null) { extract($vars); }
  include $file;
  $content = ob_get_contents();
  ob_end_clean();
  return $content;
}
$html = template("2a-template.php", [
  "name" => "Joy",
  "date" => date("Y-m-d H:i:s")
]);
 
// (C) SEND!
mail($to, $subject, $html, $head);

This is the same 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. Simple enough?
  • So basically, we are just using extract() to quick “map” our email template variables.

 

 

USEFUL BITS & LINKS

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

 

WHICH IS THE BEST METHOD?

Both the string replace and PHP variables work. But string replacement can be resource-intensive when it comes to a long email with many variables. So personally, I prefer the “wicked output buffer with extract”.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

PHP Send Email with HTML Template (click to enlarge)

 

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.