{"id":1947,"date":"2019-01-06T23:12:09","date_gmt":"2019-01-06T20:12:09","guid":{"rendered":"http:\/\/kifarunix.com\/?p=1947"},"modified":"2024-03-11T22:06:34","modified_gmt":"2024-03-11T19:06:34","slug":"configure-postfix-to-use-gmail-smtp-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/configure-postfix-to-use-gmail-smtp-on-ubuntu-18-04\/","title":{"rendered":"Configure Postfix to Use Gmail SMTP on Ubuntu 18.04"},"content":{"rendered":"\n<p>In this guide, we are going to learn how to configure Postfix to use Gmail SMTP on Ubuntu 18.04 to relay mails. To ensure reliable mail delivery, Postfix MTA can be configured to relay mails through an external SMTP server such as Gmail SMTP server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring Postfix to Use Gmail SMTP on Ubuntu 18.04<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Install Postfix on Ubuntu 18.04<\/h3>\n\n\n\n<p>If Postfix is not already installed on your Ubuntu 18.04 server, run the command below to install it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install mailutils<\/code><\/pre>\n\n\n\n<p>During installation, you will be prompted to provide some information required to configure Postfix. The first prompts asks you to choose the type of configuration that best suits your environment. Select <code>Internet Site<\/code> to enable Postfix to sent and received mails and press Enter to proceed.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/internet-site.png\"><img loading=\"lazy\" decoding=\"async\" width=\"973\" height=\"865\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/internet-site.png\" alt=\"Configure Postfix to Use Gmail SMTP on Ubuntu 18.04\" class=\"wp-image-1948\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/internet-site.png 973w, https:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/internet-site-768x683.png 768w\" sizes=\"(max-width: 973px) 100vw, 973px\" \/><\/a><\/figure><\/div>\n\n\n<p>Set the mail name. Mail name specifies the domain part that is used in a mail ID, e.g <code>example.com<\/code> for an email ID, <code>userid@example.com<\/code>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/post-fix-mail-name.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1858\" height=\"477\" src=\"http:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/post-fix-mail-name.png\" alt=\"\" class=\"wp-image-1949\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/post-fix-mail-name.png 1858w, https:\/\/kifarunix.com\/wp-content\/uploads\/2019\/01\/post-fix-mail-name-768x197.png 768w\" sizes=\"(max-width: 1858px) 100vw, 1858px\" \/><\/a><\/figure><\/div>\n\n\n<p>Press Enter to finalize the installation.<\/p>\n\n\n\n<p>Note that you can always reconfigure Postfix by running the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dpkg-reconfigure postfix<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Configuring Postfix to Use Gmail SMTP<\/h3>\n\n\n\n<p>Now that Postfix is installed, proceed to configure it to use Gmail as a mail relay.<\/p>\n\n\n\n<p>Open the Postfix configuration file, <code>\/etc\/postfix\/main.cf<\/code> and configure is as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/postfix\/main.cf<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Set the Postfix relay server<\/h4>\n\n\n\n<p>Find the line, <code>relayhost =<\/code> and setting its value to Gmail SMTPS such that it looks like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"nv\">relayhost<\/span> <span class=\"o\">=<\/span> <span class=\"o\">&#91;<\/span>smtp.gmail.com<span class=\"o\">]<\/span>:587<\/code><\/pre>\n\n\n\n<p>Next add the following lines to the end of the configuration file.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Enable SMTP Authentication<\/h4>\n\n\n\n<p>To enable SMTP server authentication, you need to;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Enable Cyrus-SASL support for authentication by setting the value of <code class=\" language-none\">smtp_sasl_auth_enable<\/code> to <code>yes<\/code>.\n<pre class=\"screen\">smtp_sasl_auth_enable = yes<\/code><\/pre>\n<\/li><li>Configure Postfix to use the file with the SASL credentials. This can be done by defining the path to <code>sasl_passwd<\/code> as follows;\n<pre class=\"screen\">smtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd<\/code><\/pre>\n<\/li><li>Set the SASL security options to disable options that allows anonymous authentication.\n<pre>smtp_sasl_security_options = noanonymous<\/code><\/pre>\n<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Enable STRTTLS Encryption<\/h4>\n\n\n\n<p>Enforce STARTTLS encryption for outgoing SMTP with Postfix by adding the following line. When a non-empty value is specified, this overrides the obsolete parameters <code>smtp_use_tls<\/code>, <code>smtp_enforce_tls<\/code>, and <code>smtp_tls_enforce_peername<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"nv\">smtp_tls_security_level<\/span> <span class=\"o\">=<\/span> encrypt<\/code><\/pre>\n\n\n\n<p>Define the path to CA certificates. The public root certificates are usually found under <code>\/etc\/ssl\/certs\/ca-certificates.crt<\/code> on Debian\/Ubuntu systems.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span class=\"nv\">smtp_tls_CAfile<\/span> <span class=\"o\">=<\/span> \/etc\/ssl\/certs\/ca-certificates.crt<\/code><\/pre>\n\n\n\n<p>These lines should look like this;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n...\n<strong>relayhost = [smtp.gmail.com]:587\n<\/strong>...\n<strong>smtp_sasl_auth_enable = yes\nsmtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd\nsmtp_sasl_security_options = noanonymous\nsmtp_tls_security_level = encrypt\nsmtp_tls_CAfile = \/etc\/ssl\/certs\/ca-certificates.crt<\/strong>\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Add Credentials to sasl_passwd<\/h4>\n\n\n\n<p>Since Postfix is acting a as mail client, it has to know when to provide a username and password. Therefore, create the sasl_passwd file define above,<code> \/etc\/postfix\/sasl_passwd<\/code> and set the credentials of the mail relay server as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;smtp.gmail.com]:587 userid@gmail.com:password<\/code><\/pre>\n\n\n\n<p>Replace your email ID appropriately.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Secure sasl_passwd<\/h4>\n\n\n\n<p>The credentials are set in plaintext. Hence to make it abit secured, change ownership and permission to <tt class=\"systemitem\"><code>root<\/code><\/tt> and <code>read-write<\/code> only respectively.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>chown root:root \/etc\/postfix\/sasl_passwd\nchmod 600 \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create sasl_passwd DB file<\/h4>\n\n\n\n<p>Postfix requires that the&nbsp;<code>sasl_passwd<\/code> file to be a database such that it can be read faster. Use <code>postmap<\/code> command to convert the file into a database, <span class=\"database\"><code>sasl_passwd.db<\/code><\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>postmap \/etc\/postfix\/sasl_passwd<\/code><\/pre>\n\n\n\n<p>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<pre class=\"wp-block-preformatted\"><code>ls -l \/etc\/postfix\/sasl_passwd*\n-rw------- 1 root root    51 Jan  6 21:57 \/etc\/postfix\/sasl_passwd\n-rw------- 1 root root 12288 Jan  6 22:04 \/etc\/postfix\/sasl_passwd.db<\/code><\/pre>\n\n\n\n<p>Restart Postfix<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart postfix<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Send a Test Mail<\/h3>\n\n\n\n<p>To verify that all is well, send the test mail as shown below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"Test Postfix Gmail Relay\" | mail -s \"Postfix Gmail Relay\" userid@gmail.com<\/code><\/pre>\n\n\n\n<p>You can tail the logs to check what is happening. If you encounter such an error <code>...SASL authentication failed; server smtp.gmail.com...<\/code>, you need to <a href=\"https:\/\/myaccount.google.com\/lesssecureapps\" target=\"_blank\" rel=\"noopener\">allow less secure apps to access your gmail account<\/a>. Otherwise, you should be able to receive the test mail.<\/p>\n\n\n\n<p>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<p><a href=\"https:\/\/kifarunix.com\/configure-postfix-to-use-gmail-app-passwords\/\" target=\"_blank\" rel=\"noreferrer noopener\">Configure Postfix to Use Gmail App Passwords<\/a><\/p>\n\n\n\n<p>You can also check our previous article on <a title=\"Configure Postfix as Send-Only SMTP Server on Fedora 29\" href=\"https:\/\/kifarunix.com\/configure-postfix-as-send-only-smtp-server-on-fedora-29\/\" target=\"_blank\" rel=\"bookmark noopener\">how to configure Postfix as send-only SMTP server on Fedora 29<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we are going to learn how to configure Postfix to use Gmail SMTP on Ubuntu 18.04 to relay mails. To ensure reliable<\/p>\n","protected":false},"author":1,"featured_media":13773,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,272,64],"tags":[5705,5707,1164,5706,67],"class_list":["post-1947","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-email","category-ubuntu-18-04","tag-configure-postfix-to-use-gmail-smtp-on-ubuntu","tag-gmail-relay-postfix","tag-postfix-gmail-relay","tag-smtp-postfix-gmail","tag-ubuntu-18-04","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1947"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=1947"}],"version-history":[{"count":3,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1947\/revisions"}],"predecessor-version":[{"id":21100,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/1947\/revisions\/21100"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/13773"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=1947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=1947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=1947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}