BH0's Blog

Send HTML Email With PHP

Hello, in this blog we will learn how to send an email which is a HTML file using PHP and the emails we will get from a JSON file - because I really like JSON. Please note that this may only work with a Gmail account.

What will we use?

  • PHP - duh
  • Laragon - a local Apache server
  • JSON - a method of storing data
  • HTML - and CSS to produce the email’s appearance

Set up

Firstly, we will need to install and configure Laragon, Laragon will give us access to PHP, Apache web-server, and a “mail sender”.

We will also need three files: “index.php” - the file which will contain our code, “email.html” - the email we wish to send and “mailinglist.json” - the file which our emails come from (yes you can use another source such as a simple array or a database).

Laragon Set up

Navigate to Laragon’s website’s download section: https://laragon.org/download/ and select one of the downloads, I use “Laragon Wamp” but theoretically anything including PHP and Apache should work.

Follow the installation wizard and things should be fairly straight forward.

  • When the download is complete open the Laragon Window and click the “gear icon” - “preferences”.
  • Navigate to the “Mail Sender” configuration area by clicking on the “Mail Sender” tab
  • Enter your Gmail account username + password
  • Feel free to press “Test Sending Mail…” to ensure everything is set up correctly
  • Exit this Window and click “Start All” to run the server
  • Click “Menu” then navigate to “Quick Create” and select “blank”
  • Name the project appropriately - this is the root folder’s name for all your files
  • And everything should be good to go (you may have to give Laragon permission)
  • Now ensure you create 3 (or at least 2) files within your project folder: “index.php”, “email.html” and optionally “mailinglist.json”

The Code

Email

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<h2> Email to YOU </h2>
<p> This is an email sent using PHP </p>
<p> Why isn't this responsive? </p>
<div><b> Because the writer was too lazy to produce / obtain a responsive HTML email</b>
<br>
<ul> Email sent using
<li> Laragon </li>
<li> PHP </li>
<li> Apache </li>
<li> JSON </li>
<li> HTML </li>
</ul>

Feel free to use a responsive HTML email here, to learn how to do so I recommend Devtip’s “How to Code a Responsive HTML Email” - https://youtu.be/XnWIperMy08 , I may make my own tutorial on this - just note that you must use inline styling. I assume you can read HTML.

mailinglist.json

1
2
3
4
{
"emails": [ "example@example.ex", "example@example.ex"]
}

Don’t forget to swap out this file with your desired email addresses. Or you can use your own email-source. I like work with tools which return J/BSON so I used JSON.

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/// Obtain Email Content
$email = file_get_contents("email.html");
/// Will help let the server/s know this is an HTML file
$headers = 'Content-type: text/html; charset=UTF-8' . "\r\n";
//$recipients = array("example@example.ex", "example@example.ex");
// Obtain Emails From JSON file
$mailinglist = json_decode(file_get_contents('mailinglist.json'), true);
$recipients = $mailinglist['emails'];
/// Send Email
foreach ($recipients as $recipient) {
if (mail($recipient, "the subject", $email, $headers)) {
echo ' Your email has been sent successfully.';
} else {
echo 'Failed to send email. ';
}
}
?>
<h2>Attempted to send email sent to <?php for ($i = 0; $i < sizeof($recipients); $i += 1) {
echo ' Email: ' . $recipients[$i];
} ?> </h2>

What this code does:

  • get the email content by reading the file contents
  • The header variable is then used to make it clear to our tools that we the file contents should be HTML thus it will not output the HTML as a text file
  • We then get the emails from the mailinglist file - if you’re unfamilliar with JSON I suggest reading up on it
  • As our emails are stored in an array - we then loop through this array to send an individually email to each email address
  • We then output some sort of message to inform the person sending the emails that everything was a success

Notice the “the subject” parameter - as an extra curriculum task you’re welcome to figure out how to get the header element’s contents of your email and use this as the subject but I didn’t feel that was in the scope of this tutorial.

To run this code open up the Laragon menu:

  • Ensure your server is running
  • Click menu > “www” > select [your project name]
    And it will open in a web browser and display the HTML output and run the PHP code - if you have no errors your email should be sitting within the appropriate inboxes.

And yes, you can continue to automate this much more, for example, making the code run at a specific time and perhaps adjusting it to work with multiple emails.

The possibillities are theoretically endless.