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!
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
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
<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
// (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
<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
// (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
// (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.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
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
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA 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
- Mail – PHP Manual
- Bulk Send Email In PHP – Code Boxx
- Fix PHP Mail Not Sending – Code Boxx
INFOGRAPHIC CHEAT SHEET

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!