Tuesday, July 15, 2014

Setting up Gmail as an SMTP relay

Lets say you have a Linux server at home. Lets say you want to send/receive mail from it. One way to do this is to run postfix and dovecot on it. Then mess with your domain's MX and DNS records. Eventually hope your domain doesn't get blacklisted because nobody knows you.

Or you could configure postfix on your Linux server to use Gmail as an SMTP relay, as described in this great link:
http://charlesauer.net/tutorials/centos/postfix-as-gmail-relay-centos.php

I got it working on Centos 6.5, so that's another distribution that works.

Some important things to note:
1. If a bad person gets access to port 25 on your server, it could become part of their spam network. Google will cancel your Gmail account if it appears you're spamming people from it, even if its really some loser from across the planet.
Please be sure to lock your server down. You will need to restrict access, and there are several ways to do so: restrict port access on your gateway, use IP Filtering on your server, etc.

2. All your outgoing mail is going to look like it came from the gmail address you used in /etc/postfix/sasl_passwd. Unless you decide to change it :)
Hint: Mailx -r "sender@domain (Fname Lname)" rcpt@domain

I have copy/pasted the important stuff from Charles's site below.

Installing Postfix

Installing Postfix is easy, just run this command as root:
yum install postfix mailx cyrus-sasl-plain
Thanks to Jonathan for pointing that out.

Configuring

Basically, you need to create a password file so that Postfix can authenticate to Gmail's servers. You do this by creating a file named sasl_passwd in /etc/postfix. Replace smtp_user and smtp_passwd with their respective values.
echo "smtp.gmail.com    smtp_user:smtp_passwd" > /etc/postfix/sasl_passwd
You then hash that file so that the password is not stored in clear text. This command will create a file named sasl_passwd.db in the /etc/postfix/ directory.
postmap hash:/etc/postfix/sasl_passwd
After that is done, add these to the bottom of /etc/postfix/main.cf. This is assuming that your root certificates installed from openssl are located in /etc/pki/tls/certs/ca-bundle.crt.
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
# Secure channel TLS with exact nexthop name match.
smtp_tls_security_level = secure
smtp_tls_mandatory_protocols = TLSv1
smtp_tls_mandatory_ciphers = high
smtp_tls_secure_cert_match = nexthop
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
relayhost = smtp.gmail.com:587
After that is done, restart postfix:
service postfix restart
Now test it to make sure it is working. Run this:
mail email@domain
Fill in the subject, put something in the body and then type a . and hit enter.
If all went well, you should get an email at the email address you entered. If you do, you can delete the file that has the password.
rm /etc/postfix/sasl_passwd
If it did not work, check the log to see what happened.
tail /var/log/maillog

Labels: , , ,