Grokking Technology

Configuring postfix to relay through Gmail


You have created a server somewhere and

Here is one simple solution - configure postfix to relay through gmail


You need to have the following available:

If you have all the above, read on…

Most of the content below was based on this article : although badly titled (it does not use a tunnel), it details most of what you need to do. My article mostly simplifies.


First, locate your postfix configuration folder (/etc/postfx) and edit the main configuration file; add the following content to the bottom of this file


myhostname =
relayhost = []:587
smtp_connection_cache_destinations = []:587
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/gmail
smtp_sasl_security_options = noanonymous
smtp_sender_dependent_authentication = yes
soft_bounce = yes
default_destination_concurrency_limit = 1

Item descriptions

An explanation of some of these items follows

Defines the hostname of your local server; you need to make sure that this domain is quite distinct from the domain you want to send emails to or the domain used on your Gmail account. Any email sent to this domain will be considered local and will not be relayed to Gmail
Specifies where mail should be sent for onward delivery i.e. relayed through; is the fixed address for sending mail through gmail while 587 is the port to use with TLS
(optional) the same as the relayhost setting; this setting can improve performance
tells postfix to use TLS if it is available. Gmail insists on either TLS or SSL; TLS does not require you to get an SSL certificate
we will create an additional file to contain our authentication details to Gmail; this defines the name of the file - use nay name you want, here we use gmail

For all the other settings, consult Postfix configuration online.

Authentication file

Now create the file gmail (as defined by smtp_sasl_password_map) and edit it to contain something like this:

# Login information for the default relayhost.

This file contains our username and password to authenticate with Gmail.

Once you have created this file as required, and everytime it changes, you need to action it with

postmap gmail


Once your configuration is complete, you can restart postfix and mail generated locally should be relayed through your Gmail account.

Test using a command-line tool and troubleshoot by checking for errors either in /var/log/mail.log or journalctl -r -u postfix

Page created on Sun 25 Apr 2021 by Andy Ferguson
Tag list: postfix mail smtp