Jul 23, 2013

f Comment

Fixing Amazon SES Postfix Send Email Error, 554 Message rejected: Email address is not verified, in SECONDS!

Amazon If you have production access to Amazon SES and you are trying to use SES to send emails with Postfix via PHP mail() function you may come across weird errors such as the email cannot be sent because of the from address is not verified as the following log message shows.

Jul 23 03:36:25 postfix/pickup[28736]: xxx: uid=1000 from=<ubuntu>
Jul 23 03:36:25 postfix/cleanup[30356]: xxx: message-id=<20130723033625.xxx@damA-21-13.compute-7.internal>
Jul 23 03:36:25 postfix/qmgr[28737]: xxx: from=<ubuntu@damA-21-13.compute-7.internal>, size=506, nrcpt=1 (queue active)
Jul 23 03:36:25 postfix/smtp[30358]: xxx: to=<target@email.com>, relay=email-smtp.us-east-1.amazonaws.com[10.20.30.40]:25, delay=0.31, delays=0.07/0.04/0.1/0.1, dsn=5.0.0, <b>status=bounced</b> (host email-smtp.us-east-1.amazonaws.com[10.20.30.40] said: <b>554 Message rejected: Email address is not verified.</b> (in reply to end of DATA command))

On many Unix systems your mail log is located at /var/log/mail.log
You are confused because you did verify the sender's email in the Amazon AWS Console, which in this case is michael@chtoen.com. You don't understand why the log says From address is some bogus email like ubuntu@damA-21-13.compute-7.internal instead.

Your PHP code looks like the following.
<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: michael@chtoen.com' . "\r\n" .
    'Reply-To: michael@chtoen.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
Looks good doesn't it? You set the From header explicitly to michael@chtoen.com but for some reason Amazon SES SMTP interface keeps ignoring the address you set in the From header.

It seems you cannot control the From field of your outgoing email and Amazon SES SMTP interface always complains the From email, ubuntu@damA-21-13.compute-7.internal, is not verified and bounces your email.

In fact before you followed Amazon's official instructions on Integrating Amazon SES with Postfix you could send emails fine and you could set the From header successfully, just not reliably.

The whole point of using Amazon SES is to send emails reliably so that the emails you send do NOT end up in recipients' spam boxes.

How do you fix the error 554 Message rejected: Email address is not verified and avoid your email being bounced again? How do you fix the error that your email's From header is being ignored in PHP mail() function?

Solution
The problem is you need to include Return-Path header and you need to add a flag in PHP mail() function like the following.
<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: michael@chtoen.com' . "\r\n" .
    'Return-Path: michael@chtoen.com' . "\r\n" .
    'Reply-To: michael@chtoen.com' . "\r\n";

mail($to, $subject, $message, $headers, '-f michael@chtoen.com');
?>
After you made the changes restart Postfix by running the following command.

sudo /etc/init.d/postfix restart
Your email should be sent successfully! In the email log you should see the following entries.

Jul 23 06:07:31 postfix/pickup[922]: xxx: uid=1000 from=
Jul 23 06:07:31 postfix/cleanup[1490]: xxx: message-id=<20130723060731.xxx@damA-21-13.compute-7.internal>
Jul 23 06:07:31 postfix/qmgr[923]: xxx: from=, size=499, nrcpt=1 (queue active)
Jul 23 06:07:31 postfix/smtp[1492]: xxx: to=, relay=email-smtp.us-east-1.amazonaws.com[10.20.30.40]:25, delay=0.4, delays=0.08/0.02/0.1/0.2, dsn=2.0.0, status=sent (250 Ok xxx)
Jul 23 06:07:31 postfix/qmgr[923]: xxx: removed

Obviously status=sent indicates the outgoing email has been successfully accepted for delivery.

It is important to note that just because the mail has been accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
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
ADVERTISING WITH US - Direct your advertising requests to Michael