Technobabble
Grokking Technology

Configuring postfix to relay through Gmail

Problem

You have created a server somewhere and

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

Pre-requistes

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.

Configuration

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

/etc/postfix/main.cf

myhostname = emne.afccommercial.co.uk
relayhost = [smtp.gmail.com]:587
smtp_connection_cache_destinations = [smtp.gmail.com]: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

myhostname
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
relayhost
Specifies where mail should be sent for onward delivery i.e. relayed through; smtp.gmail.com is the fixed address for sending mail through gmail while 587 is the port to use with TLS
smtp_connection_cache_destinations
(optional) the same as the relayhost setting; this setting can improve performance
smtp_use_tls
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
smtp_sasl_password_mapss
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:

/etc/postfix/gmail

sysadmin@afc.co.uk        drew@afc.co.uk:mypassword
# Login information for the default relayhost.
[smtp.gmail.com]:587      drew@afc.co.uk:mypassword

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

Conclusion

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