Configure Postfix to Use Gmail as a Mail Relay
If you have a Gmail account, you can configure your Mail Transfer Agent (MTA) to relay outgoing mail through Gmail. This provides you with the reliability and infrastructure of Gmail for sending emails from the command line. In this tutorial, we’ll use Postfix as our MTA, which is a secure and open-source mail transfer agent. We’ll cover instructions for various operating systems.
1. Install Required Software
Debian, Ubuntu:
|
|
Fedora:
|
|
CentOS:
|
|
OpenSUSE:
|
|
Arch Linux:
|
|
FreeBSD:
Compile Postfix from the ports collection with SASL support:
|
|
In the configuration dialogs, select SASL support. Then:
|
|
Install Mailx from the binary package:
|
|
2. Configure Gmail Authentication
Create or modify a password file that Postfix will use to authenticate with Gmail. Replace username
with your Gmail username and password
with your Gmail password. If you’re using a custom Gmail Apps domain, replace gmail.com
with your domain.
Debian, Ubuntu, Fedora, CentOS, OpenSUSE, Arch Linux:
Postfix configuration files are in /etc/postfix
. Create or edit the password file:
|
|
Add the line:
[smtp.gmail.com]:587 [email protected]:password
Save and make the file accessible only by root:
|
|
FreeBSD:
Postfix configuration files are in /usr/local/etc/postfix
. Create or edit the password file:
|
|
Add the line:
[smtp.gmail.com]:587 [email protected]:password
Save and make the file accessible only by root:
|
|
3. Configure Postfix
There are six parameters to set in the Postfix configuration file main.cf
:
relayhost
: Specifies the mail relay host and port number.smtp_use_tls
: Enables (or disables) transport layer security.smtp_sasl_auth_enable
: Enables (or disables) SASL authentication.smtp_sasl_security_options
: Set to empty to ensure no Gmail-incompatible security options are used.smtp_sasl_password_maps
: Specifies the password file to use.smtp_tls_CAfile
: Specifies the list of certificate authorities to use when verifying server identity.
Debian, Ubuntu, Arch Linux:
Edit the main Postfix configuration file:
|
|
Add or modify the following values:
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Save and close the file.
Fedora, CentOS:
Edit the main Postfix configuration file:
|
|
Add or modify the following values:
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
Save and close the file.
OpenSUSE:
Edit the main Postfix configuration file:
|
|
Add or modify the following values:
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/ca-bundle.pem
Save and close the file.
OpenSUSE also requires modifying the Postfix master process configuration file master.cf
:
|
|
Uncomment the line that reads:
#tlsmgr unix - - n 1000? 1 tlsmg
So it reads:
tlsmgr unix - - n 1000? 1 tlsmg
Save and close the file.
FreeBSD:
Edit the main Postfix configuration file:
|
|
Add or modify the following values:
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options =
smtp_sasl_password_maps = hash:/usr/local/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/mail/certs/cacert.pem
Save and close the file.
4. Process Password File
Use postmap
to compile and hash the contents of sasl_passwd
. The results will be stored in your Postfix configuration directory in the file sasl_passwd.db
.
Debian, Ubuntu, Fedora, CentOS, OpenSUSE, Arch Linux:
|
|
FreeBSD:
|
|
5. Restart Postfix
Restart the Postfix service to apply your changes.
Debian, Ubuntu, Fedora, CentOS, OpenSUSE, Arch Linux:
|
|
FreeBSD:
To start the Postfix service for this session:
|
|
To start Postfix automatically at system initialization, edit /etc/rc.conf
:
|
|
Add the line:
postfix_enable=YES
Save and close the file, then run:
|
|
6. Enable “Less Secure Apps” in Gmail
By default, Gmail allows only the most secure sign-ins. To permit relay requests, log in to your Gmail account and turn on “Allow less secure apps.” Review the Google Support document “Allowing less secure apps to access your account” for more information.
7. Send a Test Email
Test your new configuration by sending an email using the mail
command. Run:
|
|