How to Forward Mails to Gmail using Postfix


Send email using Postfix (MTA) Mail Transfer Agent via Gmail Relayhost

Emails sending with Postfix (MTA) Mail Transfer Agent via Gmail relay host. E-mails are delivered via Gmail account, the clients in the local network use Postfix as a local SMTP gateway. This tutorial shows how to do this.

In situations using like multifunctional devices, or apps that do not support authentication via Mail Submission and STARTTLS (port 587), then e-mails can send via local Postfix MTA without need to authenticate to a mail server.

If CentOS Linux is used, the required packages are installed as root.

$ yum update && yum install -y postfix mailx cyrus-sasl cyrus-sasl-plain

On a Debian and Ubuntu, the package installation is as follows.

$ apt-get update && apt-get install -y postfix mailutils

Now edit Postfix configuration by opening the file /etc/postfix/

mynetworks =
myhostname =
mydestination = $myhostname, localhost.$mydomain, localhost
inet_interfaces = all
inet_protocols = ipv4
relayhost = []: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/pki/tls/certs/ca-bundle.crt

Enter the local networks in mynetworks. As myhostname the PTR resource record that can be resolved on the Internet for determining the PTR record, hit the following command to run in a terminal, or click here.

$ curl -s | xargs host domain name pointer

Next, a /etc/postfix/sasl_passwd file is created with the following content.


Instead of use your own Gmail account, replace password with your account password.

Run postmap to create the file sasl_passwd as Berkeley DB.

postmap /etc/postfix/sasl_passwd

Hint. Run postmap after each change.

Now Postfix is restarted to activate the configuration.

systemctl restart postfix

Verify the Postfix configuration with this line in the terminal.

echo "Here is a text." | mail -v -s "Test subject" -r

Replace with your valid Gmail address, and for enter a valid recipient email.

Note. This Google Account requires disabled settings under Security – Sign in to Google – go to Security Verification and set two factor Off, and access to the Google Account when accessed by less secure apps must be On.

Postfix logging with syslog in the log file /var/log/maillog in which you can check whether the e-mail was sent and accepted by

tail -25 /var/log/maillog

Use mailq to check the queue for any rejected mails.

If you find authentication errors in maillog, the Google Account security settings must be checked.

Note. To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.

status=deferred (SASL authentication failed; server[] said: 534-5.7.9 Application-specific password required. Learn more at?534 5.7.9

Note. after several sending attempts, then rejected (bounced) mails are in the queue remains, use the command mailq to show them, ran postsuper -d to remove deferred mails from queue.

postsuper -d ALL

Wie hilfreich war dieser Beitrag?

Klicke auf die Sterne um zu bewerten!

Durchschnittliche Bewertung 4.3 / 5. Anzahl Bewertungen: 3

Bisher keine Bewertungen! Sei der Erste, der diesen Beitrag bewertet.

3 Gedanken zu „How to Forward Mails to Gmail using Postfix“

  1. I have used this for many years on many linux systems. HOWEVER ALL of them have stopped working as of 1 June 2022!!! I tried to send email to the postfix user group to complain after finding 0 info on this in various postfix sites but my email complaint was rejected multiple times. What are we to do now?

    1. it is probably related to the announcement by Google from May 30, 2022. „Less secure apps & your Google Account“. Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password. Have you tried it with a sasl update?

    2. Use Gmail’s „app password“ feature, creating a secondary password that doesn’t require two-factor authentication.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert