Postfix relay using Gmail on CentOS

Copied from: http://blog.earth-works.com/2013/05/14/postfix-relay-using-gmail-on-centos/
by Steve Talley, In case it goes away. (too valuable to risk losing). All credit to Steve Talley.

Many ISPs block sending email over port 25. This means that you won't be able to send mail from your Linux server through simple commands like mailx or logwatch. A good way to get around this limitation is to set up a relay through a Gmail account.

The following instructions were tested on CentOS 6.3, 6.4 and 6.5 using Postfix.

Note: You must install certain modules for SASL authentication to work. On CentOS, you can check that these are installed by running the following command: yum install cyrus-sasl-plain

Create the file /etc/postfix/sasl_passwd in your favorite text editor, and fill in the following on one line:

smtp.gmail.com    GmailUsername:GmailPassword

Next, we'll need to hash that file so that it's more secure. First, make sure that /etc/postfix is owned by the postfix user, or you'll get an error message stating “postmap: fatal: open database /etc/postfix/sasl_passwd.db: Permission denied”:

sudo chown postfix /etc/postfix
postmap hash:/etc/postfix/sasl_passwd

The files /etc/postfix/sasl_passwd and /etc/postfix/sasl_passwd.db will both still exist on your computer even after hashing. Please make sure that both are owned by user and group root. Set the file permissions to 640 for both files.

Edit the /etc/postfix/main.cf configuration file, and add the following lines at the end:

#Set the relayhost to the Gmail SMTP server
relayhost = smtp.gmail.com:587

#Set the required TLS options
smtp_tls_security_level = secure
smtp_tls_mandatory_protocols = TLSv1
smtp_tls_mandatory_ciphers = high
smtp_tls_secure_cert_match = nexthop

#Check that this path exists -- these are the certificates used by TLS
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt

#Set the sasl options
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous

Restart postfix with this command: service postfix restart

Other notes:

The CentOS minimal install does not include the mail command – run yum install mail to install.

Send a test message by using the format:
echo “Test email” | mail -s “This is a test” youremail@domain.com

Make sure to chown postfix /etc/postfix or you will get a fatal error when using postmap.

More reading:

Set postfix to only use ipv4 to fix ipv6 routing issues with gmail:

http://linuxmoz.com/google-mail-ipv6-network-is-unreachable-postfix-ipv4-only-fix/