In this lesson you will learn how to submit a contact form from you website via Amazon's Simple Email Service or SES for short and PHP Mailer. By using SES you are using Amazon's email server to send the details from your contact form via email to the recipient. If you have not done so already you should install composer on your server and through composer you can install PHP Mailer. Or you can have PHP Mailer direct in your folder structure for your website.
Another couple of points to consider is you need to have an AWS account and an email address verified by AWS SES. You will also need to verify your websites domain name so it can be enabled for sending via your website. You will also require an AWS IAM user with an access key ID and secret access key. To get this follow these instructions here
Although PHPMailer is capable of sending out emails it is not uncommon for these emails to end up in your spam folder and that is why we are using the Amazon SES interface to send out the email to the recipient.
Decide on which method below you're going to use.
//Using PHP Mailer via composer
use PHPMailer\PHPMailer\PHPMailer;
require 'path/to/vendor/autoload.php';
//Using PHP Mailer in website's folder structure
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
Next: Once form submit is clicked by the user, check for any errors in the form. In this example we are looking to make sure all required fields are filled in correctly. We will also count the errors and output an error message if total errors are greater then zero.
$errors = array();
if (isset($_POST['submit']))
{
if (!empty($_POST['name']))
{
$name = $_POST['name'];
}
else
{
$errors['name'] = "Name required Try again";
}
if (!empty($_POST['email']))
{
$email = $_POST['email'];
}
else
{
$errors['email'] = "Email required Try again";
}
if (!empty($_POST['phone']))
{
$phone = $_POST['phone'];
}
else
{
$errors['phone'] = "Contact number required Try again";
}
if (empty($_POST['message']))
{
$errors['message'] = "Message required Try again";
}
else
{
$msg = $_POST['message'];
}
$total_errors = count($errors);
In this section here you need to specify the recipient email (Email address to receive form submission), the sender email (Your AWS SES approved email address) your SMTP server (example: email-smtp.eu-west-1.amazonaws.com), your username (Access key ID) and password (Secret access key)
Depending on state of errors we will either output the errors on the page or send the email and display a success message to the sender.
if($total_errors > 0)
{
$not_sent = implode("\n", $errors);
}
else
{
$mail = new PHPMailer(true);
try
{
$recipient = 'YOUR EMAIL ADDRESS TO RECEIVE CONTACT FORM SUBMISSION';
$sender = 'YOUR AWS SES APPROVED EMAIL ADDRESS';
$senderName = $name;
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "SMTP server example";
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 587;
$mail->Username = "SMTP account username";
$mail->Password = "SMTP account password example";
$mail->SMTPSecure = 'tls';
$mail->setFrom($sender, $senderName);
$mail->addAddress($recipient);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Enquiry from YOUR WEBSITE';
//Seperate with html break tag after each variable for email formatting
$mail->Body = "From: $name
Email: $email
Phone: $phone
Message: $msg
IP Address: $ip";
$mail->send();
if($mail)
{
$success = 'Thank You for your message. We will be in touch';
}
}
catch (phpmailerException $e)
{
echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer.
}
catch (Exception $e)
{
echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES.
}
}
Completed block of PHP code
//Using PHP Mailer via composer
use PHPMailer\PHPMailer\PHPMailer;
require 'path/to/vendor/autoload.php';
//Using PHP Mailer in website's folder structure
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$errors = array();
if (isset($_POST['submit']))
{
if (!empty($_POST['name']))
{
$name = $_POST['name'];
}
else
{
$errors['name'] = "Name required Try again";
}
if (!empty($_POST['email']))
{
$email = $_POST['email'];
}
else
{
$errors['email'] = "Email required Try again";
}
if (!empty($_POST['phone']))
{
$phone = $_POST['phone'];
}
else
{
$errors['phone'] = "Contact number required Try again";
}
if (empty($_POST['message']))
{
$errors['message'] = "Message required Try again";
}
else
{
$msg = $_POST['message'];
}
$total_errors = count($errors);
if($total_errors > 0)
{
$not_sent = implode("\n", $errors);
}
else
{
$mail = new PHPMailer(true);
try
{
$recipient = 'YOUR EMAIL ADDRESS TO RECEIVE CONTACT FORM SUBMISSION';
$sender = 'YOUR AWS SES APPROVED EMAIL ADDRESS';
$senderName = $name;
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "SMTP server example";
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 587;
$mail->Username = "SMTP account username";
$mail->Password = "SMTP account password example";
$mail->SMTPSecure = 'tls';
$mail->setFrom($sender, $senderName);
$mail->addAddress($recipient);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Enquiry from YOUR WEBSITE';
//Seperate with html break tag after each variable for email formatting
$mail->Body = "From: $name
Email: $email
Phone: $phone
Message: $msg
IP Address: $ip";
$mail->send();
if($mail)
{
$success = 'Thank You for your message. We will be in touch';
}
}
catch (phpmailerException $e)
{
echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer.
}
catch (Exception $e)
{
echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES.
}
}
}
Place the below php script above or below your html form. Errors or success message will be displayed after user clicks the submit button.
if(isset($success))
{
echo $success;
}
if(isset($not_sent))
{
echo $not_sent;
}
Some JavaScript: Place this block of code between some script tags in the footer or at the bottom of the page of your website. This will prevent the form re-submitting when the page is refreshed.
if (window.history.replaceState )
{
window.history.replaceState( null, null, window.location.href );
}
Simple Boostrap4 html form. This is a fully working example and please check it out. Your message will be sent to an unmanned inbox so please don't get in touch with me using this form.