{"id":7342,"date":"2020-12-02T22:39:45","date_gmt":"2020-12-02T19:39:45","guid":{"rendered":"https:\/\/kifarunix.com\/?p=7342"},"modified":"2024-03-15T09:15:00","modified_gmt":"2024-03-15T06:15:00","slug":"configure-postfix-to-use-gmail-smtp-on-centos-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/configure-postfix-to-use-gmail-smtp-on-centos-8\/","title":{"rendered":"Configure Postfix to Use Gmail SMTP on CentOS 8"},"content":{"rendered":"\n
Welcome to our tutorial on how to configure Postfix to use Gmail SMTP on CentOS 8 to relay mails. Postfix<\/a> is a free and open-source mail transfer agent that routes and delivers electronic mail. Postfix MTA can be configured to relay mails through an external SMTP servers such as Gmail SMTP server for a reliable mail delivery.<\/p>\n\n\n You can install Postfix by installing the SASL (Simple Authentication and Security Layer) provides a mechanism of authenticating users using their username and password<\/em>. The implementation of SASL is provided Cyrus SASL library which can be installed on a CentOS 8 system by running the command below;<\/p>\n\n\n\n Mail command on CentOS 8 is provided by mailx package. <\/p>\n\n\n\n Postfix is now installed with the default configuration. You can view Postfix configuration values using the postconf command;<\/p>\n\n\n\n To make further configuration changes, edit the main Postfix configuration file, Postfix can be configured to deliver mails indirectly via a relay host. A relay host can be defined on a Postfix configuration file using the By default, the value of the According to Postfix configuration<\/a>, different values can be set for the relayhost parameter;<\/p>\n\n\n\n In our case, we are setting Postfix relay to Gmail SMTP servers. Hence, open the Postfix main configuration file;<\/p>\n\n\n\n Under the Intranet\/Internet section, find the, According to Postfix<\/a>:<\/p>\n\n\n\n To enable SASL server authentication, you need to;<\/p>\n\n\n\n These configs can be updated on Postfix configuration file (See the highlighted lines<\/strong>);<\/p>\n\n\n\n Save and exit the configurtion file.<\/p>\n\n\n\n As per our configurations above, the SASL credentials database file is set to You should define the SMTP credentials in the format;<\/p>\n\n\n\n As shown below;<\/p>\n\n\n\n Replace the Note:<\/p>\n\n\n\n The credentials are set in plaintext. To protect access to the file by other users, make the file read+write only for Postfix requires that the This will assign the same ownership and permissions to the database file as set for the sasl_passwd file above.<\/p>\n\n\n\n Run the You can ignore the warning, Start and enable Postfix to run on system boot;<\/p>\n\n\n\n To check the status;<\/p>\n\n\n\n Once you are done with the configuration, you can try to send a test mail to verify that Gmail SMTP relay works fine. You can use mail client or any other for this purpose.<\/p>\n\n\n\n You can tail the logs to check the delivery status;<\/p>\n\n\n\n If you get the error;<\/p>\n\n\n\n You need to login to the account you used for the SASL authentication and enable Less secure app access<\/a>.<\/p>\n\n\n\n NOTE that the use of less secure app access has been deprecated. See how you can use App password as an alternative in the guide below;<\/p>\n\n\n\n Configure Postfix to Use Gmail App Passwords<\/a><\/p>\n\n\n\n After that, retry to sent the test mail and check the logs and hurray, our test mail is delivered, status=sent<\/strong>.<\/p>\n\n\n\n And that is how easy it is to configure Postfix to use Gmail as an SMTP relay host. That marks the end of our guide on how to install and configure Postfix to use Gmail SMTP relay host on CentOS 8.<\/p>\n\n\n\n Read more about Postfix Configuration on;<\/p>\n\n\n\n Postfix Basic Configuration<\/a><\/p>\n\n\n\n Configure Postfix to Use Gmail SMTP on Ubuntu 20.04<\/a><\/p>\n\n\n\n Configure Nagios Email Notification Using Gmail<\/a><\/p>\n\n\n\n Configure Sendmail to Use Gmail Relay on Ubuntu 18.04\/Debian 10\/9<\/a><\/p>\n\n\n\n Encrypt Emails using Enigmail on Thunderbird<\/a><\/p>\n\n\n\n<\/figure><\/div>\n\n\n
Configuring Postfix to Use Gmail SMTP on CentOS 8<\/h2>\n\n\n\n
Install Postfix on CentOS 8<\/h3>\n\n\n\n
postfix<\/code> package itself or via the
mailutils<\/code> package which installs along with it.<\/p>\n\n\n\n
dnf install postfix<\/code><\/pre>\n\n\n\n
Install SASL on CentOS 8 <\/h3>\n\n\n\n
dnf install cyrus-sasl-plain <\/code><\/pre>\n\n\n\n
Install Mail Command on CentOS 8<\/h3>\n\n\n\n
dnf install mailx<\/code><\/pre>\n\n\n\n
Configuring Postfix to Use Gmail SMTP Relay<\/h3>\n\n\n\n
postconf<\/code><\/pre>\n\n\n\n
\/etc\/postfix\/main.cf<\/code> and make any necessary changes as needed.<\/p>\n\n\n\n
Set the Postfix Relay server<\/h4>\n\n\n\n
relayhost<\/strong><\/code> parameter.<\/p>\n\n\n\n
relayhost<\/strong><\/code> parameter is empty<\/strong>. This configures Postfix is to try to deliver mail directly to the Internet, which is usually not desirable.<\/p>\n\n\n\n
\n
vim \/etc\/postfix\/main.cf<\/code><\/pre>\n\n\n\n
relayhost =<\/code>, lines and set its value to Gmail SMTP domain name as shown below;<\/p>\n\n\n\n
...\n#relayhost = $mydomain\n#relayhost = [gateway.my.domain]\n#relayhost = [mailserver.isp.tld]\n#relayhost = uucphost\n#relayhost = [an.ip.add.ress]\nrelayhost = [smtp.gmail.com]:587<\/strong>\n<\/code><\/pre>\n\n\n\n
Configure Postfix SASL Authentication<\/h4>\n\n\n\n
\n
\n
smtp_sasl_auth_enable<\/strong><\/code> to
yes<\/strong><\/code>.
smtp_sasl_auth_enable = yes<\/code>
<\/li>\n\n\n\nsasl_passwd<\/code> as using the
smtp_sasl_password_maps<\/strong><\/code> parameter.
smtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd<\/code>
<\/li>\n\n\n\nsmtp_tls_security_level<\/strong><\/code> parameter.
smtp_tls_security_level = encrypt<\/code>
<\/li>\n\n\n\n\n
smtp_sasl_security_options = noanonymous<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n
...\nrelayhost = [smtp.gmail.com]:587\n...\n#\n...\n# SASL authentication\n# change may to encrypt.\nsmtp_tls_security_level = encrypt\nmeta_directory = \/etc\/postfix\nshlib_directory = \/usr\/lib64\/postfix\n# Added these lines\nsmtp_sasl_auth_enable = yes\nsmtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd\nsmtp_sasl_security_options = noanonymous\n<\/code><\/pre>\n\n\n\n
Set the SMTP SASL Credentials<\/h4>\n\n\n\n
\/etc\/postfix\/sasl_passwd<\/code><\/strong>.<\/p>\n\n\n\n
# destination credentials\n[smtp.domain.name] username:password<\/code><\/pre>\n\n\n\n
vim \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n
[smtp.gmail.com]:587 userid@gmail.com:password<\/code><\/pre>\n\n\n\n
userid@gmail<\/strong><\/code> and
password<\/strong><\/code> with your Gmail account credentials.<\/p>\n\n\n\n
\n
[<\/code>” and “
]<\/code>” in the
relayhost<\/code> destination, you must as well use the same format in the
smtp_sasl_password_maps<\/code> file.<\/li>\n\n\n\n
:submission<\/code>” or “
:587<\/code>“) in the
relayhost<\/code> destination, you must as well use the same form in the
smtp_sasl_password_maps<\/code> file.<\/li>\n<\/ul>\n\n\n\n
Secure the SASL Password File<\/h4>\n\n\n\n
root<\/code>.<\/p>\n\n\n\n
chown root:root \/etc\/postfix\/sasl_passwd\nchmod go-rwx \/etc\/postfix\/sasl_passwd\nchmod 600 \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n
Create SASL Password DB file<\/h4>\n\n\n\n
sasl_passwd<\/code> file to be a database such that it can be read faster. Use
postmap<\/code> command to convert the file into a database,
sasl_passwd.db<\/code>.<\/p>\n\n\n\n
postmap \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n
ls -l \/etc\/postfix\/sasl_passwd*<\/code><\/pre>\n\n\n\n
-rw-------. 1 root root 56 Dec 2 22:02 \/etc\/postfix\/sasl_passwd\n-rw-------. 1 root root 12288 Dec 2 22:03 \/etc\/postfix\/sasl_passwd.db<\/code><\/pre>\n\n\n\n
Check Postfix Configuration<\/h4>\n\n\n\n
postfix check<\/code> command to check the Postfix configuration for any error. Any error should printed on the output.<\/p>\n\n\n\n
postfix check<\/code><\/pre>\n\n\n\n
postfix\/postfix-script: warning: symlink leaves directory: \/etc\/postfix\/.\/makedefs.out<\/code><\/strong>.<\/p>\n\n\n\n
systemctl enable --now postfix<\/code><\/pre>\n\n\n\n
systemctl status postfix<\/code><\/pre>\n\n\n\n
\u25cf postfix.service - Postfix Mail Transport Agent\n Loaded: loaded (\/usr\/lib\/systemd\/system\/postfix.service; disabled; vendor preset: disabled)\n Active: active (running) since Wed 2020-12-02 22:04:20 EAT; 6s ago\n Process: 3653 ExecStart=\/usr\/sbin\/postfix start (code=exited, status=0\/SUCCESS)\n Process: 3651 ExecStartPre=\/usr\/libexec\/postfix\/chroot-update (code=exited, status=0\/SUCCESS)\n Process: 3647 ExecStartPre=\/usr\/libexec\/postfix\/aliasesdb (code=exited, status=0\/SUCCESS)\n Main PID: 3721 (master)\n Tasks: 3 (limit: 5027)\n Memory: 5.6M\n CGroup: \/system.slice\/postfix.service\n \u251c\u25003721 \/usr\/libexec\/postfix\/master -w\n \u251c\u25003722 pickup -l -t unix -u\n \u2514\u25003723 qmgr -l -t unix -u\n\nDec 02 22:04:19 centos8.kifarunix-demo.com systemd[1]: Starting Postfix Mail Transport Agent...\nDec 02 22:04:20 centos8.kifarunix-demo.com postfix\/master[3721]: daemon started -- version 3.3.1, configuration \/etc\/postfix\nDec 02 22:04:20 centos8.kifarunix-demo.com systemd[1]: Started Postfix Mail Transport Agent.\n<\/code><\/pre>\n\n\n\n
Send a Test Mail to Verify Postfix Gmail SMTP Relay<\/h4>\n\n\n\n
echo \"Test Postfix Gmail SMTP Relay\" | mail -s \"Postfix Gmail SMTP Relay\" userid@gmail.com<\/strong><\/code><\/pre>\n\n\n\n
tail \/var\/log\/mail.log<\/code><\/pre>\n\n\n\n
...status=deferred (SASL authentication failed; server smtp.gmail.com[74.125.133.108] said: 535-5.7.8 Username and Password not accepted.<\/strong><\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
...\nDec 2 22:04:56 ceph-admin postfix\/qmgr[3723]: 46993C35EAC: from=<root@ceph-admin.kifarunix-demo.com>, size=513, nrcpt=1 (queue active)\nDec 2 22:05:00 ceph-admin postfix\/smtp[3732]: 46993C35EAC: to=<userid@gmail.com>, relay=smtp.gmail.com[108.177.127.109]:587, delay=4, delays=0.47\/0.33\/2\/1.2, dsn=2.0.0, status=sent (250 2.0.0 OK 1606935899 f18sm554842edt.60 - gsmtp)\n...<\/code><\/pre>\n\n\n\n
Related Tutorials<\/h3>\n\n\n\n