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
|
|
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
|
|
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
|
|
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.