Configuring Postfix for relaying on Debian using Gmail

This article explains how Postfix can be installed and configured to route emails to an external SMTP server, in particular, using Gmail.

Configuration

Run the script below as root.

#!/bin/bash

apt-get install postfix sasl2-bin bsd-mailx -y

##################################
# Choose:
#   * 'Satelite system'
#   * leave relayhost blank
##################################

cd /etc/postfix

cp -p main.cf main.cf.ORIGINAL

cat << EOD >> main.cf
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_mechanism_filter = plain, login
smtp_sasl_security_options = noanonymous
EOD

# Substitute server, username and password below by your own settings
SERVER=smtp.gmail.com
USERNAME=[email protected]
PASSWORD=your.password

cat << EOD > sasl_passwd
${SERVER} ${USERNAME}:${PASSWORD}
EOD

chmod 400 sasl_passwd

postmap /etc/postfix/sasl_passwd

/etc/init.d/postfix restart

Testing your configuration

As a regular user, try something like this:

#!/bin/bash

echo 'It works!' | mailx -s test [email protected]

References


If you found this article useful, it will be much appreciated if you create a link to this article somewhere in your website.