6 Ways To Create Email Templates In PHP

Need to create an email template for your PHP project? Well, an email template is essentially just a string – You either do “string replace” or “insert variable”… Which most beginners should already know. Let Master Coffee walk you through some examples – Let’s go!

 

CODE DOWNLOAD

I have released this under the MIT license, feel free to use it in your own project – Personal or commercial. Some form of credits will be nice though. 🙂

 

 

VIDEO TUTORIAL

 

METHOD 1) STRING REPLACE

1A) HTML TEMPLATE

1-template.html
<html><body>
  <p>Hi {NAME},</p>
  <p>Thank you for purchasing <strong>{PRODUCT}</strong>.</p>
</body></html>

This is just a “regular HTML email template”, all the variables are enclosed in curly brackets – {VARIABLE}

 

 

1B) PHP STRING REPLACE

1-replace.php
<?php
// (PART A) EMAIL SETTINGS
$to = "[email protected]";
$name = "Job Doe";
$product = "Boo Bar";
$subject = "Template Demo";
$headers = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);

// (PART B) EMAIL TEMPLATE
$message = file_get_contents("1-template.html");
foreach ([
  "{NAME}" => $name,
  "{PRODUCT}" => $product
] as $k=>$v) { $message = str_replace($k, $v, $message); }

// (PART C) SEND EMAIL
echo mail($to, $subject, $message, $headers) ? "SENT" : "ERROR" ;

The only two essential parts are:

  • $message = file_get_contents("1-template.html") Read the HTML template as a string.
  • $message = str_replace("{VARIABLE}", "VALUE", $message); Replace the varaiables.

 

 

METHOD 2) SHORT ECHO TAG

2A) HTML TEMPLATE

2-template.php
<html><body>
  <p>Hi <?=$name?>,</p>
  <p>Thank you for purchasing <strong><?=$product?></strong>.</p>
</body></html>

Take note, this is a PHP file. We are “inserting variables” here using the PHP short echo tag – <?=$VARIABLE?>

 

2B) PHP OUTPUT BUFFER MAGIC

2-short.php
<?php
// (PART A) EMAIL SETTINGS
$to = "[email protected]";
$name = "Joe Doe";
$product = "Eoo Bar";
$subject = "Template Demo";
$headers = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);

// (PART B) EMAIL TEMPLATE
ob_start();
require "2-template.php";
$message = ob_get_contents();
ob_end_clean();

// (PART C) SEND EMAIL
echo mail($to, $subject, $message, $headers) ? "SENT" : "ERROR" ;
  • If we do a “normal” $VAR = "ABC" and require "2-template.php", PHP will directly output the HTML template.
  • To “capture” the parsed HTML template in a string:
    • We define the variables first – $VAR = "ABC"
    • Start output buffering – ob_start()
    • Load the template – require "2-template.php"
    • Put the buffer into a string – $message = ob_get_contents()
    • Stop buffering – ob_end_clean()
    • Then, send $message accordingly.

 

 

METHOD 3) IN-STRING VARIABLE

3-variable.php
<?php
// (PART A) EMAIL SETTINGS
$to = "[email protected]";
$name = "Joh Doe";
$product = "Hoo Bar";
$subject = "Template Demo";
$headers = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);

// (PART B) EMAIL TEMPLATE
$message = "<html><body>
  <p>Hi {$name},</p>
  <p>Thank you for purchasing <strong>{$product}</strong>.</p>
</body></html>";

// (PART C) SEND EMAIL
echo mail($to, $subject, $message, $headers) ? "SENT" : "ERROR" ;

Yes, this is just the “normal insert variables into a string”. For the beginners – For this to work properly, you must use double quotes to define the string.

 

 

METHOD 4) HEREDOC

4-heredoc.php
<?php
// (PART A) EMAIL SETTINGS
$to = "[email protected]";
$name = "Joi Doe";
$product = "Ioo Bar";
$subject = "Template Demo";
$headers = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);

// (PART B) EMAIL TEMPLATE
$message = <<<MAIL
<html><body>
  <p>Hi {$name},</p>
  <p>Thank you for purchasing <strong>{$product}</strong>.</p>
</body></html>
MAIL;

// (PART C) SEND EMAIL
echo mail($to, $subject, $message, $headers) ? "SENT" : "ERROR" ;

This is another way to define a string in PHP –

  • <<<MAIL Whatever after this line is a string.
  • MAIL; Marks the end of the string.
  • We can insert variables into the string as usual using {$VARIABLE}.

 

 

METHOD 5) PRINT STRING

5-print.php
<?php
// (PART A) EMAIL SETTINGS
$to = "[email protected]";
$name = "Jon Doe";
$product = "Noo Bar";
$subject = "Template Demo";
$headers = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);

// (PART B) EMAIL TEMPLATE
$message = sprintf("<html><body>
  <p>Hi %s,</p>
  <p>Thank you for purchasing <strong>%s</strong>.</p>
</body></html>",
  $name, $product
);

// (PART C) SEND EMAIL
echo mail($to, $subject, $message, $headers) ? "SENT" : "ERROR" ;

Yet another way to insert variables into a string, using the sprintf() function.

  • The sprintf() function takes in 2 or more parameters.
  • The first parameter is the string, replace the variables with %s.
  • The following parameters are variables to replace %s.

 

 

METHOD 6) STRING CONCAT

6-concat.php
<?php
// (PART A) EMAIL SETTINGS
$to = "[email protected]";
$name = "Joy Doe";
$product = "Yoo Bar";
$subject = "Template Demo";
$headers = implode("\r\n", [
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8"
]);

// (PART B) EMAIL TEMPLATE
$message = "<html><body>
  <p>Hi ".$name.",</p>
  <p>Thank you for purchasing <strong>".$product."</strong>.</p>
</body></html>";

// (PART C) SEND EMAIL
echo mail($to, $subject, $message, $headers) ? "SENT" : "ERROR" ;

Finally, a method that Master Coffee doesn’t quite like. By using string concat – "STRING" . $VARIABLE . "STRING" . $VARIABLE ...

 

 

THE END – WHICH IS THE “BEST METHOD”?

That’s all for this short tutorial and sharing. For those who are wondering “which is the best method based on the performance”, I did a quick test and recorded the time taken to run each method (in microseconds):

  • String replace – 0.0085361003875732
  • Short echo tag – 0.0094518661499023
  • In-string variable – 0.0074009895324707
  • Heredoc – 0.0085310935974121
  • String print – 0.0082659721374512
  • String concat – 0.0096440315246582

Yep, there is not much difference, unless you are processing millions of entries. So just go with whichever method you are most comfortable with – Master Coffee prefers the “traditional” string replace, and heredoc for the clarity/simplicity.

 

CHEAT SHEET

Ways To Create Email Template in PHP (click to enlarge)