Relay mail via Google SMTP with Postfix

Using Google’s SMTP service to relay your outbound mail is a handy way to be able to send mail from Amazon EC2 instances, or other machines running IP addresses considered to be of dubious quality in the spam fighting world.

It’s also quite valuable in that your outbound emails will appear in your GMail sent messages folder and hence appear inline in conversations when your users reply.

Setup Instructions

These instructions are developed for Centos 5.2 – but no doubt they can be applied to other distributions with minimal modification.

Ensure that you have Postfix:
yum install postfix -y

We need to create the client keys for the TLS connection to Google’s SMTP service – and to do that we first need to become our own certificate authority (CA):

/etc/pki/tls/misc/CA -newca

Follow the prompts and make intelligent responses.

Now, create the client keys/certs (again with intelligent responses, and ensuring you use the same common name and country code):

cd /etc/postfix
openssl genrsa -out postfixclient.key 1024
openssl req -new -key postfixclient.key -out postfixclient.csr
openssl ca -out ./postfixclient.pem -infiles postfixclient.csr

Now you can configure Postfix accordingly. Add these to the top of /etc/postfix/
relayhost = []:587
smtp_connection_cache_destinations =
relay_destination_concurrency_limit = 1
default_destination_concurrency_limit = 5
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_note_starttls_offer = yes
tls_random_source = dev:/dev/urandom
smtp_tls_scert_verifydepth = 5
smtp_tls_enforce_peername = no
smtpd_tls_req_ccert =no
smtpd_tls_ask_ccert = yes
soft_bounce = yes

And store your password in /etc/postfix/sasl_passwd

(Note that if you’re using a regular gmail account instead of Google Apps For Your Domain, you would use above)

Get Postfix to parse the password file:
postmap /etc/postfix/sasl_passwd

Optionally configure Postfix to run on a different port (so as not to clash with your regular SMTP relay):

Open /etc/postfix/ and change the first line to:
10025 inet n - n - - smtpd

Now start Postfix!
service postfix start

And add it to your startup scripts:
ln -s /etc/init.d/postfix /etc/rc3.d/S96postfix

Filed under: Servers — Jules @ 5:06 pm - November 5, 2008 :: Read comments »


  1. [...] assist if you’d like to try something like this, I’ve posted a howto for configuring Postfix to relay via GMail’s SMTP service. Filed under: Servers, Web 2.0, Web Services — Jules @ 3:56 pm :: Relates to: billing, [...]

    Pingback by Are spam filters damaging your cash flow? — November 11, 2008 @ 3:56 pm

  2. Does this kind of setup work for sending from multiple accounts on your EC2 instance, or will every message appear to come from (I tried setting it up and all messages appear to come from the user whose credentials are used to authenticate).

    Comment by kevin lochner — January 10, 2009 @ 3:02 am

Sorry, the comment form is closed at this time.

Never Offline

A blog hosted by James Peterson, director of insights @ Wormly

On a semi-regular basis James will be trying to demonstrate that website infrastructure really is an exciting topic, and that your users really do care about the uptime & speed of your website.