{"id":5995,"date":"2020-06-01T17:42:02","date_gmt":"2020-06-01T14:42:02","guid":{"rendered":"https:\/\/kifarunix.com\/?p=5995"},"modified":"2024-03-14T22:00:53","modified_gmt":"2024-03-14T19:00:53","slug":"configure-postfix-to-use-gmail-smtp-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/configure-postfix-to-use-gmail-smtp-on-ubuntu-20-04\/","title":{"rendered":"Configure Postfix to Use Gmail SMTP on Ubuntu 20.04"},"content":{"rendered":"\n
Welcome to our tutorial on how to configure Postfix to use Gmail SMTP on Ubuntu 20.04 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\n You can install Postfix by installing the Or<\/p>\n\n\n\n During installation, you will be prompted to provide some information required to configure Postfix.<\/p>\n\n\n\n Select the mail server type configuration that best suits your environment needs. You are provided with multiple options;<\/p>\n\n\n\n Select The You can always reset these settings by re-configuring postfix after the installation by executing the command below.<\/p>\n\n\n\n Postfix is now set up with a default configuration. To make further configuration changes, edit the main Postfix configuration file, You can view Postfix configuration values using the postconf command;<\/p>\n\n\n\n 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 Find the line, 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, Restart Postfix<\/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 If you get the error, The change the following line int he main.cf;<\/p>\n\n\n\n to<\/p>\n\n\n\n And restart Postfix service.<\/p>\n\n\n\n That marks the end of our guide on how to install and configure Postfix to use Gmail SMTP relay host on Ubuntu 20.04. Enjoy.<\/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 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\nConfiguring Postfix to Use Gmail SMTP Relay<\/a><\/h2>\n\n\n\n
Install Postfix on Ubuntu 20.04<\/a><\/h3>\n\n\n\n
postfix<\/code> package itself or via the mailutils<\/code> package which installs along with it.<\/p>\n\n\n\napt install postfix<\/code><\/pre>\n\n\n\napt install mailutils<\/code><\/pre>\n\n\n\nSelect the Mail Server Type<\/h4>\n\n\n\n
\n
No configuration<\/code><\/strong>: Should be chosen to leave the current configuration unchanged.<\/li>\n\n\n\nInternet site<\/code><\/strong>: Mail is sent and received directly using SMTP.<\/li>\n\n\n\nInternet with smarthost<\/strong><\/code>: Mail is received directly using SMTP or by running a utility such as fetchmail. Outgoing mail is sent using a smarthost.<\/li>\n\n\n\nSatellite system<\/code><\/strong>: All mail is sent to another machine, called a ‘smarthost’, for delivery.<\/li>\n\n\n\nLocal only<\/code><\/strong>: The only delivered mail is the mail for local users. There is no network.<\/li>\n<\/ul>\n\n\n\nInternet Site<\/strong><\/code> to enable Postfix to sent and receive mails and press Enter to proceed.<\/p>\n\n\n\n
<\/figure>\n\n\n\nSet System Mail Name<\/h4>\n\n\n\n
mail name<\/code><\/em> is the domain name used to “qualify” _ALL_ mail addresses without a domain name, for example, kifarunix-demo.co<\/strong>m<\/code>, in our case.<\/p>\n\n\n\n
<\/figure>\n\n\n\ndpkg-reconfigure postfix<\/code><\/pre>\n\n\n\nConfiguring Postfix to Use Gmail SMTP<\/h3>\n\n\n\n
\/etc\/postfix\/main.cf<\/code> and make any necessary changes as needed.<\/p>\n\n\n\npostconf<\/code><\/pre>\n\n\n\nSet the Postfix Relay server<\/h4>\n\n\n\n
relayhost<\/strong><\/code> parameter.<\/p>\n\n\n\nrelayhost<\/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\nrelayhost =<\/code>, and set its value to Gmail SMTP domain name as shown below;<\/p>\n\n\n\n...\nmydestination = $myhostname, kifarunix-demo.com, ubuntu20, localhost.localdomain, localhost\nrelayhost = [smtp.gmail.com]:587\n<\/strong>mynetworks = 127.0.0.0\/8 [::ffff:127.0.0.0]\/104 [::1]\/128\n...<\/code><\/pre>\n\n\n\nConfigure 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<\/strong>...\n#\n...\nsmtp_tls_CApath=\/etc\/ssl\/certs\n#smtp_tls_security_level=may\nsmtp_tls_security_level=encrypt\n<\/strong>smtp_tls_session_cache_database = btree:${data_directory}\/smtp_scache\n...\nrecipient_delimiter = +\ninet_interfaces = all\ninet_protocols = all\nsmtp_sasl_auth_enable = yes\nsmtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd\nsmtp_sasl_security_options = noanonymous<\/strong>\n<\/code><\/pre>\n\n\n\nSet 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\nvim \/etc\/postfix\/sasl_passwd<\/code><\/code><\/pre>\n\n\n\n[smtp.gmail.com]:587 userid@gmail.com:password<\/code><\/pre>\n\n\n\nuserid@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\nSecure the SASL Password File<\/h4>\n\n\n\n
root<\/code>.<\/p>\n\n\n\nchown root:root \/etc\/postfix\/sasl_passwd\nchmod 600 \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\nCreate 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\npostmap \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\nls -l \/etc\/postfix\/sasl_passwd*<\/code><\/pre>\n\n\n\n-rw------- 1 root root 53 Jun 1 13:57 \/etc\/postfix\/sasl_passwd\n-rw------- 1 root root 12288 Jun 1 14:06 \/etc\/postfix\/sasl_passwd.db<\/code><\/pre>\n\n\n\nCheck 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\npostfix check<\/code><\/pre>\n\n\n\npostfix\/postfix-script: warning: symlink leaves directory: \/etc\/postfix\/.\/makedefs.out<\/code><\/strong>.<\/p>\n\n\n\nsystemctl restart postfix<\/code><\/pre>\n\n\n\nsystemctl status postfix<\/code><\/pre>\n\n\n\n\u25cf postfix.service - Postfix Mail Transport Agent\n Loaded: loaded (\/lib\/systemd\/system\/postfix.service; enabled; vendor preset: enabled)\n Active: active (exited) since Mon 2020-06-01 14:11:55 UTC; 5s ago\n Process: 7507 ExecStart=\/bin\/true (code=exited, status=0\/SUCCESS)\n Main PID: 7507 (code=exited, status=0\/SUCCESS)\n\nJun 01 14:11:55 ubuntu20 systemd[1]: Starting Postfix Mail Transport Agent...\nJun 01 14:11:55 ubuntu20 systemd[1]: Finished Postfix Mail Transport Agent.\n<\/code><\/pre>\n\n\n\nSend 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\ntail \/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...\nJun 1 14:22:42 ubuntu20 postfix\/smtp[7650]: 6892D40186: to=userid@gmail.com<\/strong>, relay=smtp.gmail.com[173.194.76.109]:587, delay=4.8, delays=0.35\/0.03\/3.6\/0.8, dsn=2.0.0, status=sent<\/strong> (250 2.0.0 OK 1591021361 k12sm19227410wrn.42 - gsmtp)\n...<\/code><\/pre>\n\n\n\nNetwork is unreachable <\/code><\/strong>;<\/p>\n\n\n\npostfix\/smtp[10417]: connect to smtp.gmail.com[2a00:1450:4013:c07::6c]:587: Network is unreachable<\/code><\/pre>\n\n\n\ninet_protocols = all<\/strong><\/code><\/pre>\n\n\n\ninet_protocols = ipv4<\/strong><\/code><\/pre>\n\n\n\nRelated Tutorials<\/h3>\n\n\n\n