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 like these, for example, multifunctional devices, or apps that do not support authentication via Mail Submission and STARTTLS (port 587), e-mails can send via local Postfix MTA without need to log on 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
The Postfix configuration is to be edited by opening the file /etc/postfix/main.cf
mynetworks = 127.0.0.0/8 192.168.1.0/24 myhostname = 22.214.171.124.dynamic.yline.res.cust.isp.net mydestination = $myhostname, localhost.$mydomain, localhost inet_interfaces = all inet_protocols = ipv4 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/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 entry, the following command can be executed in the terminal, as in the following example, or here.
$ curl -s ifconfig.co | xargs host 126.96.36.199.in-addr.arpa domain name pointer 188.8.131.52.dynamic.yline.res.cust.isp.net
Next, a /etc/postfix/sasl_passwd file is created with the following content.
Instead of email@example.com use your own Gmail account, with password the Google password.
Use postmap to create the file sasl_passwd as Berkeley DB.
Hint. Run postmap after each change.
Now Postfix is restarted to activate the configuration.
systemctl restart postfix
Test the Postfix configuration with this line in the terminal.
echo "Here is a text." | mail -v -s "Test subject" -r firstname.lastname@example.org email@example.com
The firstname.lastname@example.org be the valid Gmail address, and if email@example.com, 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 smtp.gmail.com.
tail -25 /var/log/maillog mailq
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.
status=deferred (SASL authentication failed; server smtp.gmail.com[184.108.40.206] said: 534-5.7.9 Application-specific password required. Learn more at?534 5.7.9
Hint. It can happen that after several sending attempts, then rejected (bounced) mails are in the queue, the command
mailq gets them, with
postsuper -d you remove mails from the queue.
postsuper -d ALL