PHP Contact Form Tutorial : Part 2 of 2

Image for post
Image for post

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:

  1. Less of a chance it will end up in spam/junk

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!

Image for post
Image for post
Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

Add the user to the “SES” group. Search for it, and check off the box. Then click the “Next: Tags” button.

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

Let’s write some code

Grab the code from here and create a separate file called “amazon_ses.php

Use this code, changing your two Amazon keys:

PHP Code explanation

  • Line 3 “includes” the code we need to send emails via Amazon SES

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

Image for post
Image for post
This went right to my inbox; not to Spam/Junk!

Optional: Additional advanced code

Some improvements you could potentially add:

  1. Utilize Ajax such that the user isn’t brought to a separate page

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store