AWSLINUX

Sending Emails with Postfix and Amazon SES

As an alternative of installing a complete email server, you can send emails from your Amazon EC2 instance using postfix and Amazon SES service like this :

  • Create an AWS IAM SES user and attach AmazonSesSendingAccess policy to this user
  • Install needed packages (in my case Debian jessie instance) :
 
sudo apt-get install postfix \
     libsasl2-modules libsasl2-2 \
     libsasl2-dev bsd-mailx
  • Edit /etc/postfix/main.cf file like this :

relayhost = email-smtp.eu-west-1.amazonaws.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl-passwords
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_note_starttls_offer = yes
smtp_sasl_mechanism_filter = PLAIN LOGIN

Don’t forget to change the relayhost if needed (depending on your zone).

  • Then create /etc/postfix/sasl-passwords with this contents :

email-smtp.eu-west-1.amazonaws.com AWS_SES_USER_Access_Key_ID:AWS_SES_USER_Secret_Access_Key

Don’t forget to change the relayhost if needed (depending on your zone).

  • Run the follwing command :

postmap hash:/etc/postfix/sasl-passwords

  • Restart postfix service:

service postfix restart

Before sending you need to verify the email address that you will use to send emails in the SES section Email Addresses, and if you want to be able to send emails to addresses that are not verified in your AWS account contact AWS support and ask them to let you leave the sandbox mode to production mode.

  • Test all this with sendmail :

sendmail -f tarek@cloudinit.fr hello@cloudinit.fr

From: tarek@cloudinit.fr

Subject: Test

This email was sent through Amazon SES!

That’s it.

Leave a Reply

Your email address will not be published. Required fields are marked *