Sending dynamic HTML email using php

Updated: 08-Mar-2022 / Tags: PHP Tutorials / Views: 4030 - Author: George

Introduction

Hello everyone, i am sure that all of you have seen beautiful emails arriving in your inboxes after a purchase or a subscription, and have wondered how did they do that.
Those emails are plain HTML and CSS and are not difficult to do. It is the same thing as designing a web page. Every email client such as outlook, thunderbird and others, can read and structure html and css.

Let me quick say what we will do.
We will create a PHP page and we are gonna name that page "mail-template.php", in which we design the mail. We give the file a ".php" extension because we want to include some variables in the email which we will set in the php script file (script.php) .

When we are done with the email we create a php file named "script.php", and in this file we are gonna use the php mail() function and we will send the "mail-template.php" as the mail's body to the sender we like.

Live Demo

If you click on the "View Demo" button you will see the email's design.

Files we need

  • We need to create two files.
    We need a "mail-template.php" file where we write our email html code.
  • And a "script.php" file where we write our php code.

The mail-template file

This is the "mail-template.php" file, as you can see its a regular web page. This is what we are going to send via email. I didn't include all the content because this would created a very long page i don't want that here. I want to explain the big picture not the details. If you download the files you will get everything.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>HTML email template</title>
	<style>
		<!-- The css code goes here -->
	</style>
</head>
<body>
	<div class="page">
		<h1>Thank you for your purchase <?php echo @$name ?> </h1>

		<!-- More content here -->

		<footer>
			<a href="https://google.com">Hyperlink</a> |
			<a href="https://digitalfox-tutoroals.com">Hyperlink</a> |
			<a href="">Hyperlink</a> |
			<a href="">Hyperlink</a>
		</footer>
	</div>
</body>
</html>
  • In line 7 we have the css code. When we send HTML structured mails we have to include the css code in the same page, so the email clients can read and apply the rules.
  • In line 12, we have our greetings header and the thing that differs is the php $name variable. The $name variable here acts as a placeholder, and we are going to set it's value in the php file.

The php code

In the script.php file, we are going to set up the information we need to send the e-mail.

In line 2 we defining where to send the email. And in line 3 we are defining the e-mail's subject.

<?php
	$to = "someone@mail.com";
	$subject = "Thank you for your purchase";

	// In php 7.2 and newer versions we can use an array to set the headers.
	$headers = array(
	 	'MIME-Version' => '1.0',
	 	'Content-type' => 'text/html;charset=UTF-8',
	 	'From' => 'dennis@mail.com',
	 	'Reply-To' => 'dennis@mail.com'
	);

	// Setting the value in the $name variable.
	$name = "Dennis";

	// Starting output buffer.
	ob_start();
	include("mail-template.php");
	$message = ob_get_contents();
	ob_end_clean();

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

	if(!$sent){
		echo "Error: Message not sent. Please try again";
	}else{
		echo "Message was sent successfully";
	}
  • In line 6 we defining an array named $headers which we will send with the mail.
  • In line 7 the first header we set in the MIME header and it's version.

    What is MIME (Multipurpose Internet Mail Extensions). MIME let's the users exchange different kinds of data over mail. We can send audio, images, video, and programs when the MIME header is set.

  • In line 8 we have the MIME header's Content-Type. With the content-type we tell the mail clients and the browsers, how to treat the data that we are sending. In our case we tell the mail clients to display our content as HTML, and we also set the character encoding to UTF-8 charset=UTF-8.
  • In line 9 we set the headers From: information, and in line 10 we set the Reply-To information.
    Those informations are mandatory.
  • In line 14 we setting the value to the $name variable.

    $name = "Dennis";

    The $name variable will display the name in the h1 tag, which is in the mail-template.

    <h1> Thank you for your purchase Dennis </h1>

  • In line 17-20 we have this code block, in which we start an output buffer with the ob_start() function in line 17. That means that we have access to the code, (in this case our html email template), and thus we can assign values in any variables we may have in the template. And we have, the $name variable.

    In line 18 we include the mail-template, so we can add the content to the output buffer.

    In line 19 we are storing everything from the file ("mail-template.php") to a $message variable which we will use in the mail() function.

    And in line 20 we clear (erase) the output buffer.

    ob_start();
    include("mail-template.php");
    $message = ob_get_contents();
    ob_end_clean();
    		
  • Next in line 22 we use the mail() function to send the email. The mail() function takes four arguments ('where to', 'the subject', 'the mail-template.php', 'the headers'). Also we assign the returned value of the mail() function to the $sent variable.

    In line 24 we check the value that the mail() function returns, (true or false).
    If the function returns true we display a success message, else we display an error.

    $sent = mail($to, $subject, $message, $headers);
    
    if(!$sent){
      echo "Error: Message not sent. Please try again";
    }else{
      echo "Message was sent successfully";
    }
    // End of script
    		

Summary

  • In this article we saw:
  • What is MIME (Multipurpose Internet Mail Extensions). MIME let's the users exchange different kinds of data over mail. We can send audio, images, video, and programs when the MIME header is set.
  • How to buffer a whole HTML page with the output control function ( ob_start(), ob_get_contents(), ob_end_clean() ).
  • We saw that we can have variables in the HTML template ($name) . And that we can assign values to those variables in the php file.
  • And that we can assign the buffered data to the $message variable, and send them as an email with the mail() function.

Last Words

Thanks for reading guys, and i hope you find this article helpful.

Source code

You can download the source code and use it in any way you like.

Times downloaded: 626

Buy me a coffee

If you like to say thanks, you can buy me a coffee.

Buy me a coffee with paypal