Gmail as relayhost for Postfix

      Comments Off on Gmail as relayhost for Postfix

I just installed a new Ubuntu 13.04 server and thought I’d look into using gmail as relayhost for Postfix instead of Exim for a change. Postfix is the default MTA (mail transfer agent) on Ubuntu, so let’s set it up already…

Installing packages:

sudo apt-get install postfix ca-certificates libsasl2-2 libsasl2-modules mailutils

If you have not installed postfix before, you’ll be asked some questions during installation. Select ‘Internet Site’ for the first question, and as FQDN you can just enter the name of your server or whatever you like.

Configure postfix by editing /etc/postfix/main.cf

sudo pico /etc/postfix/main.cf

add the following lines at the end of the file:

relayhost = smtp.gmail.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

Create /etc/postfix/sas_passwd file, which contains your gmail user and password:

sudo pico /etc/postfix/sasl_passwd

Add the following line, replacing johndoe and mypassword with your gmail user name & password:

[smtp.gmail.com]:587 johndoe@gmail.com:mypassword

(note: in some cases you can not use the brackets which will make postfix do an mxlookup. If it works with them, just leave it like that)

Generate /etc/postfix/sasl_passwd.db file using postmap:

sudo postmap /etc/postfix/sasl_passwd

Set up CA certificate for postfix:

sudo cp /etc/ssl/certs/Thawte_Premium_Server_CA.pem /etc/postfix/cacert.pem

Secure the files by setting file permissions:

sudo chmod 400 /etc/postfix/sasl_passwd

Reload postfix to effectuate configuration changes:

sudo service postfix reload

Futher:

Testing the configuration:

echo Test | mail -s "test" target@gmail.com

Replacing the target with the email address you’d like to send mail to. This should send a message to you (might take a min), and it should also show up in your sent mail box if your sending from the same account as you’re receiving on.

Making sure mail sent to root goes to your email:

To make sure mail to ‘root’ is actually forwarded to your mail address, edit the /etc/aliases file:

sudo pico /etc/aliases

modify the email again, and add the following line:

root: johndoe@gmail.com

Enjoy your new postfix installation!

update: minor fixes done

Source: old site