I previously wrote a tutorial on creating a simple PHP Contact form; this is Part 2 of 2 where I go over some more advanced techniques.
Issue: Who is the email from?
So far we can send an email, but it comes “from” whatever the default configuration was set up as. By this, I mean that the e-mail may come from someRandomUser@yourDomain.com. While this is functional, we can change who the email comes “from”. This serves two purposes:
- Less of a chance it will end up in spam/junk
- It’s more professional if you use this code to email your users (and not just yourself)
The PHP mail() function accepts an optional parameter called “headers”. Use this code, of course changing the appropriate content for your domain:
Looks much better!
When you open the email, it will have the “return address” with what you put in. It will still indicate the original server with via but that’s okay.
This email will probably still end up in your spam/junk folder. To address, that…
Use Amazon SES to prevent emails going to Spam/Junk Folder
Using a reputable company to send your emails can help ensure the message gets delivered to the user’s inbox. It doesn’t have to an Amazon service (I’ve also used SendGrid in the past). For this tutorial, we’ll use Amazon Simple Email Service (“SES”).
Setting up SES
After signing into the Amazon Web Service (“AWS”) Console, go to the Amazon Simple Email Service area.
Go to SES Home >>Identify Management >> Email Addresses >> Verify a New Email Address. Check your email, and then click on the link inside of the new message to confirm. Back in the AWS console, it should confirm that you can now use this email.
Here, we are setting up a single email address, which is pretty easy. If you will be using several different email addresses, you could instead use Identity Management >> Domain . I wrote about it here, but it does take slightly longer.
Setting up IAM
We need to create a “user” that will be sending the emails. This can be done in Amazon Web Services >> Identity and Access Management (“IAM”).
In IAM, go to the Users section then click the button for “Add User”. Enter in a username and check the box for “Programmatic access”. Then click the “Next: Permissions” button to proceed.
Add the user to the “SES” group. Search for it, and check off the box. Then click the “Next: Tags” button.
You can skip the “Tags” section, so just go to the next screen by clicking the “Next: Review” button.
Confirm you’ve made the selections mentioned and then click the “Create user” button.
On the final screen, you’ll see an Access key ID and a Secret access key (that you’ll have to click on a link to show it). Click the link and make note of these two values.
Let’s write some code
Use this code, changing your two Amazon keys:
PHP Code explanation
- Line 3 “includes” the code we need to send emails via Amazon SES
- Lines 5-6 are the Amazon Access/Secret keys, that are unique to you (from the IAM setup)
- Lines 8–10 store variables from the form
- Lines 12–13 set up some variables we’ll need for the email sent to us
- Lines 15–16 initializes using the Amazon SES code
- Lines 18–22 sends the email
The only other note to mention is on Line 21, the message content (“BodyHTML”) is listed twice. The first part (with the strip_tags() ) is for when the email client/app only supports text (thus it strips any HTML tags). The second instance of it is for email client/apps that do support HTML, and nl2br() converts the carriage return (blank rows between paragraphs).
Try it out! Now that your email is being sent this way (in this case, Amazon), it will have less of a chance of going to spam/junk
Optional: Additional advanced code
Some improvements you could potentially add:
- Utilize Ajax such that the user isn’t brought to a separate page
- If you keep the two-page approach as described here, ensure that when the user hits “Refresh” that the email doesn’t get sent to you twice
My PHP book has a chapter on Ajax (source code here). I also explain the “refresh” issue in my book (specifically, page 95). Source code can be found here for that but the full explanation is detailed in the book :)
Like this style of writing? 👍👍 Are you creating a PHP web application and already know what a “variable” is and the concept of an “if” statement? Check out my book: Web Development for Intermediate Programmers — with PHP