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/ /etc/postfix/

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

# TLS parameters
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 = []:587
mynetworks = [::ffff:]/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_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

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


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

Leave a Reply

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