I really like the simplicity of AWS’s Lightsail service for small personal projects. After spending much of my work day in larger, more complex AWS environments, it’s nice to just click a few buttons in Lightsail and be up and running in no time. The WordPress instance blueprint packaged by Bitnami is rock solid and has some nice plugins already included.
When it comes to email notifications, I like to relay those from my WordPress instance to AWS SES where I already have my domain verified and configured for sending emails. Bitnami includes a WordPress plugin for this but it requires a paid version for integrating with AWS SES. Fortunately, it’s fairly easy to relay emails (without a plugin) to AWS SES with the help of Postfix.
Guide
Prerequisites
In order to relay emails from your WordPress instance to AWS SES using Postfix, you’ll need the following first:
- SSH access to your AWS Lightsail WordPress Bitnami instance
- A verified domain in AWS SES
- AWS SES SMTP credentials
- Optional: Moved out of the AWS SES sandbox. While in the sandbox, you can only send emails to verified identities and are limited to ~200 emails per day.
Install and Configure Postfix
- SSH into your WordPress instance
- Install Postfix:
sudo apt install postfix
- When prompted, select “Satellite system”:
- For System mail name, specify your root domain name (I chose joshwayne.org for my instance). You can use the defaults for the rest of the options.
- Run the following command. Make sure to specify the correct region in the relayhost parameter. My AWS SES configuration is in us-east-1.
sudo postconf -e "relayhost = [email-smtp.us-east-1.amazonaws.com]:587" \ "smtp_sasl_auth_enable = yes" \ "smtp_sasl_security_options = noanonymous" \ "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" \ "smtp_use_tls = yes" \ "smtp_tls_security_level = encrypt" \ "smtp_tls_note_starttls_offer = yes"
- Create a SASL password file:
sudo vim /etc/postfix/sasl_passwd
- Copy and paste the following (all one line) into the file. Make sure to use your region and replace iam_access_key_id and secret_access_key with your AWS SES SMTP user credentials.
[email-smtp.us-east-1.amazonaws.com]:587 iam_access_key_id:secret_access_key
- Create hash DB map:
sudo postmap hash:/etc/postfix/sasl_passwd
- Update permissions:
sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
- Reload Postfix:
sudo postfix reload
Test
You can test the configuration by sending an email with the sendmail utility and/or PHP. If the sendmail test succeeds but not the PHP test, then there is a problem with the PHP configuration. If the PHP test succeeds, then you’re all set.
PHP Test
- SSH into your WordPress instance
- Create a test.php file in your home directory:
vim test.php
- Copy and paste the following code into the file. Replace the sender and recipient values.
<?PHP
$sender = 'testsender@example.org';
$recipient = 'testrecipient@gmail.com';
$subject = "Test PHP Email";
$message = "This is a test message.";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers)) {
echo "Message accepted";
} else {
echo "Error: Message not accepted";
}
?>
- Run the test:
php test.php
Sendmail Test
- SSH into your WordPress instance
- Type the following command (press enter after each line). Replace the sender and recipient email addresses. Don’t forget the period on the last line.
/usr/sbin/sendmail -f testsender@example.com testrecipient@gmail.com
To: testrecipient@gmail.com
From: Test Sender <testsender@example.com>
Subject: Test Sendmail Email
This is a test message.
.
Conclusion
In this tutorial, we installed Postfix and configured it to relay our WordPress emails to AWS SES without using any WordPress plugins.