Configure Postfix To Use Gmail As A Mail Relay
Configure Postfix To Use Gmail As A Mail Relay
If you have a Gmail account, you can configure your MTA to relay outgoing mail
through Gmail. This gives you the benefit of Gmail's reliability and robust
infrastructure, and provides you with a simple means of sending email from the
command line.
In this tutorial, we will use Postfix as our MTA. Postfix is a free, open-source,
actively maintained, and highly secure mail transfer agent.
At each step, we'll cover instructions specific to the following operating systems:
Debian 9
We will also install mailutils/mailx so that we can send a test email. Where
necessary, we will install supplemental SASL libraries.
All commands beginning with # require root privileges.
1. Install Required Software
Debian, Ubuntu:
When prompted for "General type of mail configuration," choose Internet Site.
When prompted for a "Mail name," choose a hostname to be used in mail headers as
the origin of your emails. A fully-qualified domain name is preferred, but using your
machine's simple hostname is OK. Regardless of what you enter here, your return
address will appear to recipients as your Gmail address.
You may be prompted to set the "Root and postmaster mail recipient." Enter root,
or another user who should receive mail subsystem notifications.
For any other prompts, you can choose the default values.
2. Configure Gmail Authentication
Create or modify a password file which will be used by Postfix to establish
authentication with Gmail. In the authentication information below,
replace username with your Gmail username and password with your Gmail
password. If you are using a custom Gmail Apps domain name, you may
replace gmail.com with your Google Apps domain.
The password file will reside in the Postfix configuration directory. The file can be
named whatever you like, but the recommended filename is sasl_passwd.
Debian, Ubuntu, Fedora, CentOS, OpenSUSE, Arch Linux:
Postfix configuration files reside in the directory /etc/postfix. Create or edit the
password file:
nano /etc/postfix/sasl_passwd
3. Configure Postfix
There are six parameters which must be set in the Postfix configuration file main.cf.
The parameters are:
relayhost, which specifies the mail relay host and port number. The host name will
be enclosed in brackets to specify that no MX lookup is required.
smtp_use_tls, which enables (or disables) transport layer security.
smtp_sasl_auth_enable, which enables (or disables) SASL authentication.
smtp_sasl_security_options, which in the following configuration will be set to
empty, to ensure that no Gmail-incompatible security options are used.
smtp_sasl_password_maps, which specifies the password file to use. This file
will be compiled and hashed by postmap in a later step.
smtp_tls_CAfile, which specifies the list of certificate authorities to use when
verifying server identity.
Debian, Ubuntu, Arch Linux:
Edit the main Postfix configuration file:
nano /etc/postfix/main.cf
postmap /etc/postfix/sasl_passwd
5. Restart Postfix
Restart the Postfix service, putting your changes into effect.
Debian, Ubuntu, Fedora, CentOS, OpenSUSE, Arch Linux:
To start Postfix.
6. Enable "Less Secure Apps" In Gmail
By default, only the most secure sign-ins, such as logging in to Gmail on the web,
are allowed for your Gmail account. To permit relay requests, log in to your Gmail
account and turn on Allow less secure apps.
For more information, review the Google Support document "Allowing less secure
apps to access your account."
7. Send A Test Email
Test your new configuration by sending an email using the mail command. Run:
You will be presented with a blank line (or a CC: field, which you can bypass by
pressing Enter). Type the body of your message, pressing Enter for new lines. When
you are finished composing the email, type CTRL-D to send it. To cancel the email,
press CTRL-C twice.
To send a precomposed email, use the command:
less /var/log/mail.log
If you receive authentication errors from Gmail, verify that Allow Less Secure
Apps is turned on in your Gmail account settings, as specified in Step 6.
Verify that the password file sasl_passwd exists and that its contents are
formatted correctly, as specified in Step 2. If you make any changes to the password
file, make sure to repeat Steps 4 and 5 to hash the new password file and restart
Postfix.
If you see any TLS errors, double check the configuration in main.cf as specified in
Step 3. If you make any configuration changes, restart Postfix as specified in Step 5.