Configure Postfix to relay emails through Gmail STMP Server

Postfix relay through gmail

Postfix relay through gmailTo configure Postfix SMTP Server to relay emails through Gmail SMPT Server on Ubuntu Linux you need an account of Gmail and then you have to follow these simple instructions.

Install Postfix by the following command

sudo apt-get install postfix -y

the installation ask some informations to configure the service: answer No configuration because you will manually configure it after.

When the installation of Postfix is ended, you can start to manually configure it copying the Debian Linux standard example:

sudo cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf

then you have to edit the file /etc/postfix/main.cf (with vim, gedit or other editor) for adding the following rows:

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=no
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
#
myhostname = (your-host-name for example myserver.home.local)
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination =
relayhost = [smtp.gmail.com]:587
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
default_transport = smtp
relay_transport = smtp
inet_protocols = all
#
# SASL Settings
smtp_use_tls=yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem

Create the file /etc/mailname (if it don’t exist) and add a row with the name of your server, the same you used at the myhostname parameter in the file main.cf.

Now you need to create the file /etc/postfix/sasl_passwd with the credentials to connect to GMail SMTP Server:

[smtp.gmail.com]:587   gmail_username@gmail.com:gmail_password

obviously you have to use the credentials of your Gmail account.

So create the .db file from the above file by these commands:

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

At last create the certificate and restart Postfix with the following commands:

sudo cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem
sudo /etc/init.d/postfix restart

Now everything would be OK.

It’s possible to test it sending an e-mail by the command line.

For example:

echo “Hi, this is the body of the e-mail” | mail -s "My Test" -r mymail@gmail.com destination@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *