Follow this Blog!

Jul 23, 2013

f Comment

Step-by-Step Guide on Sending Emails PROGRAMMATICALLY and RELIABLY with Amazon SES, Postfix, Zoho!

Amazon If you own a domain and would like to send email with Amazon SES, Zoho, Postfix reliably and programmatically in PHP, you've come to the right place.

How does programmatically sending emails work?

1. First, you run an SMTP server such as Postfix on local port 25.

2. You call PHP's email sending function such as mail() to put the email together and send the email's content to the SMTP server following the standard SMTP protocols.

That's it! However, your emails are likely to end up in the recipient's spam or junk folder. If you don't want that to happen, follow the following the steps.

3. Sign up for Amazon SES and validate the ownership of your email address.

4. Edit the SMTP server's configuration file, which is the main.cf file in the /etc/postfix folder, so that it uses the Amazon host as the relay host, which many major email servers such as Yahoo and Gmail accept as a trusted host.

That's it. Now whenever you send an email from a PHP program, Amazon will handle the email on your behalf and make sure th email ends up in the Inbox folder of the recipient's email box.

Read on to see each step in detail.

What is Zoho
Zoho is equivalent to Google Business App service. Ever since Google Business App has stopped their free service many have turned to Zoho for being able to receive and send emails from their own custom domain. For example suppose you own http://www.mensfashionforless.com/ and would like to create an email address such as michael@mensfashionforless.com and be able to receive and send emails from michael@mensfashionforless.com. Zoho helps you do exactly that for free at the time of the writing.

From here on out let's suppose you'd like to send emails from admin@yourDomain.com.

In a nutshell Zoho makes it possible for you to receive and send emails from an email address with your own domain in it.
What is Postfix
Postfix is a free and open-source mail transfer agent (MTA) that routes and delivers electronic mail, intended as an alternative to the widely used Sendmail MTA. Read How to Send Emails on a Unix machine with PHP? if you aren't familiar with MTA.

With a programming language such as PHP, Postfix helps you send emails programmatically. Without them you can only use Zoho's web user interface to send emails.
Just being able to send email programmatically is not enough: You need to be able to send emails reliably. Otherwise most likely your emails end up in the recipient's spam or junk folder. That's where Amazon SES comes in.

What is Amazon SES
http://aws.amazon.com/ses/ says "Amazon Simple Email Service (Amazon SES) is a highly scalable and cost-effective bulk and transactional email-sending service for businesses and developers."

In layman's terms Amazon SES helps you send emails reliably making sure your outgoing emails do not end up in the spam folder of the recipient's email box. Amazon SES endorses to third party email service providers such as Gmail, Yahoo, Hotmail that you are the rightful owner of admin@yourDomain.com and therefore should trust emails sent from admin@yourDomain.com.

From here on out let's suppose you'd like to send emails from admin@yourDomain.com.

Now that we understand what Amazon SES, Postfix, Zoho, PHP are and what role each plays I'll walk you through step-by-step how to send emails programmatically and reliably from your own domain.

Step 1: Sign up for a free account with Zoho
Go to Zoho.com and sign up for a free account. Once you are done you need to change the MX records properly so that emails sent to @yourDomain.com will be routed to Zoho's email server accessible by your Zoho web mail account at https://mail.zoho.com/. Just follow the prompts on Zoho.

Step 2: Add the mail app in Zoho
Log into your Zoho's administrator panel and add the mail app by following Control Panel > Applications > Mail Suite Apps > Add > Mail so that your users will have access to the email functions.

Once you are done you'll be able to manage your emails at @yourDomain.com by logging into the web account at https://mail.zoho.com/.
Questions?

Step 3: Sign up for Amazon SES service
I assume you already have an account with Amazon AWS. Log in and sign up for Amazon SES service.

Step 4: Request production access to SES and do other necessary things
Log into your Amazon AWS account and request production access to SES. You may need to wait a couple of days before you are approved. Once you are approved validate a sender email you intend to use at https://console.aws.amazon.com/ses/home?#verified-senders-email:. If you'd like to send emails from admin@yourDomain.com you'll need to prove to Amazon SES that you are the owner of admin@yourDomain.com.

Basically Amazon SES endorses to third party email service providers such as Gmail, Yahoo, Hotmail that you are the rightful owner of admin@yourDomain.com and therefore should trust emails sent from admin@yourDomain.com.
Finally create SMTP credentials. You'll end up at https://console.aws.amazon.com/ses/home#smtp-settings with the following message.

To send email using SMTP, you will need to know the following:

Server Name: email-smtp.us-east-1.amazonaws.com
Port: 25, 465 or 587
Use Transport Layer Security (TLS): Yes
Authentication: Your SMTP credentials - see below.

So far so good? Questions?

Step 5: Integrate Amazon SES with Postfix
I assume your server already has Postfix installed. If not read How to Send Emails on a Unix machine with PHP?

Follow Amazon's instructions on http://docs.aws.amazon.com/ses/latest/DeveloperGuide/postfix.html to integrate SES with Postfix.

The purpose of this step is to configure your local SMTP server so that it relays outgoing emails to Amazon SES SMTP server which takes over until the emails arrive at the destination. Without this step your emails will not be reliably sent.
Step 6: Write PHP code to send a test email!
You can use PHP's mail() function to send a test email to yourself and make sure it doesn't end up in the spam folder.

If you run into errors while sending an email Fixing Amazon SES Postfix Send Email Error in SECONDS! will help you.

Now you can programmatically send emails from admin@yourDomain.com and receive emails by logging into the web UI at https://mail.zoho.com/.

If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael