Postfix: Gmail as Relay – Linux Mint/Ubuntu/Debian

Many ISPs block sending email over port 25. This means that you won’t be able to send mail directly from your Linux server.

A good way to get around this limitation is to set up a relay through a Gmail account.

1. Install the Required Packages

Execute the following command to install required packages:

$ sudo apt-get install mailutils

During the installation, a popup box may display. If it does, select your server as Internet Site and for FQDN use something like domain.tld

2. Configure Postfix to use Gmail as Relay

Edit the Postfix configuration file:

$ sudo vi /etc/postfix/main.cf

Add the following lines:

# Relaying Postfix SMTP via GMAIL
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

3. Setup Gmail Credentials

Open (create) the file sasl_passwd:

$ sudo vi /etc/postfix/sasl_passwd

Add the following line (set your Gmail username and password):

[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD

Set the permissions and tell Postfix to use the sasl_passwd file:

$ sudo chmod 400 /etc/postfix/sasl_passwd
$ sudo postmap /etc/postfix/sasl_passwd

4. Validate the Certificate

Add the certificate to Postfix, as follows:

$ cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

5. Test Postfix with Gmail SMTP Realy

Restart Postfix to apply all changes.

$ sudo service postfix restart

Try to send an email.

$ echo "Hello World" | mail -s "Test Message" you@example.com

Check the mail log:

$ tail /var/log/mail.log

If all goes well, you should not see any errors.

Once configured, all emails from your server will be sent via Gmail.

The Gmail’s SMTP Server has a limit of 500 emails per day. So use it wisely!

4 Replies to “Postfix: Gmail as Relay – Linux Mint/Ubuntu/Debian”

  1. Brilliant – I’ve tried several sites, but couldn’t get the install to work.
    You’re instructions were easy to follow and worked first time
    ( except for a minor change to the certificate name – I had to use thawte_Primary_Root_CA.pem )

  2. Если у меня к примеру 5 ящиков на mail.ru
    и мне надо их менять при каждой следующей пересылки письма – подскажите как сделать?

  3. # cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem
    cat: /etc/ssl/certs/Thawte_Premium_Server_CA.pem: No such file or directory

  4. # ll /etc/ssl/certs/ | grep hawte
    lrwxrwxrwx 1 root root 26 вер 11 2019 2e4eed3c.0 -> thawte_Primary_Root_CA.pem
    lrwxrwxrwx 1 root root 31 вер 11 2019 ba89ed3b.0 -> thawte_Primary_Root_CA_-_G3.pem
    lrwxrwxrwx 1 root root 31 вер 11 2019 c089bbbd.0 -> thawte_Primary_Root_CA_-_G2.pem
    lrwxrwxrwx 1 root root 66 вер 11 2019 thawte_Primary_Root_CA_-_G2.pem -> /usr/share/ca-certificates/mozilla/thawte_Primary_Root_CA_-_G2.crt
    lrwxrwxrwx 1 root root 66 вер 11 2019 thawte_Primary_Root_CA_-_G3.pem -> /usr/share/ca-certificates/mozilla/thawte_Primary_Root_CA_-_G3.crt
    lrwxrwxrwx 1 root root 61 вер 11 2019 thawte_Primary_Root_CA.pem -> /usr/share/ca-certificates/mozilla/thawte_Primary_Root_CA.crt

Leave a Reply