{"id":97714,"date":"2019-08-02T08:00:22","date_gmt":"2019-08-02T08:00:22","guid":{"rendered":"https:\/\/fedoramagazine.org\/?p=28960"},"modified":"2019-08-02T08:00:22","modified_gmt":"2019-08-02T08:00:22","slug":"use-postfix-to-get-email-from-your-fedora-system","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2019\/08\/02\/use-postfix-to-get-email-from-your-fedora-system\/","title":{"rendered":"Use Postfix to get email from your Fedora system"},"content":{"rendered":"<p>Communication is key. Your computer might be trying to tell you something important. But if your mail transport agent (<a rel=\"noreferrer noopener\" aria-label=\"MTA (opens in a new tab)\" href=\"https:\/\/en.wikipedia.org\/wiki\/Message_transfer_agent\" target=\"_blank\">MTA<\/a>) isn&#8217;t properly configured, you might not be getting the notifications. Postfix is a MTA <a href=\"https:\/\/en.wikipedia.org\/wiki\/Postfix_(software)\">that&#8217;s easy to configure and known for a strong security record<\/a>. Follow these steps to ensure that email notifications sent from local services will get routed to your internet email account through the Postfix MTA.<\/p>\n<p> <span id=\"more-29016\"><\/span> <\/p>\n<h2>Install packages<\/h2>\n<p>Use <em>dnf <\/em>to install the required packages (<a href=\"https:\/\/fedoramagazine.org\/howto-use-sudo\/\">you configured <\/a><em><a href=\"https:\/\/fedoramagazine.org\/howto-use-sudo\/\">sudo<\/a><\/em><a href=\"https:\/\/fedoramagazine.org\/howto-use-sudo\/\">, right?<\/a>):<\/p>\n<pre class=\"wp-block-preformatted\">$ sudo -i\n# dnf install postfix mailx<\/pre>\n<p>If you previously had a different MTA configured, you may need to set Postfix to be the system default. Use the <em>alternatives<\/em> command to set your system default MTA:<\/p>\n<pre class=\"wp-block-preformatted\">$ <strong>sudo alternatives --config mta<\/strong>\nThere are 2 programs which provide 'mta'. Selection Command\n*+ 1 \/usr\/sbin\/sendmail.sendmail 2 \/usr\/sbin\/sendmail.postfix\nEnter to keep the current selection[+], or type selection number: 2<\/pre>\n<h2>Create a <em>password_maps<\/em> file<\/h2>\n<p>You will need to create a Postfix lookup table entry containing the email address and password of the account that you want to use to for sending email:<\/p>\n<pre class=\"wp-block-preformatted\"># MY_EMAIL_ADDRESS=glb@gmail.com\n# MY_EMAIL_PASSWORD=abcdefghijklmnop\n# MY_SMTP_SERVER=smtp.gmail.com\n# MY_SMTP_SERVER_PORT=587\n# echo \"[$MY_SMTP_SERVER]:$MY_SMTP_SERVER_PORT $MY_EMAIL_ADDRESS:$MY_EMAIL_PASSWORD\" &gt;&gt; \/etc\/postfix\/password_maps\n# chmod 600 \/etc\/postfix\/password_maps\n# unset MY_EMAIL_PASSWORD\n# history -c<\/pre>\n<p>If you are using a Gmail account, you&#8217;ll need to configure an &#8220;app password&#8221; for Postfix, rather than using your gmail password. See &#8220;<a rel=\"noreferrer noopener\" aria-label=\"Sign in using App Passwords (opens in a new tab)\" href=\"https:\/\/support.google.com\/accounts\/answer\/185833\" target=\"_blank\">Sign in using App Passwords<\/a>&#8221; for instructions on configuring an app password.<\/p>\n<p>Next, you must run the <em>postmap<\/em> command against the Postfix lookup table to create or update the hashed version of the file that Postfix actually uses:<\/p>\n<pre class=\"wp-block-preformatted\"># postmap \/etc\/postfix\/password_maps<\/pre>\n<p>The hashed version will have the same file name but it will be suffixed with <em>.db<\/em>.<\/p>\n<h2>Update the <em>main.cf<\/em> file<\/h2>\n<p>Update Postfix&#8217;s <em>main.cf<\/em> configuration file to reference the Postfix lookup table you just created. Edit the file and add these lines.<\/p>\n<pre class=\"wp-block-preformatted\">relayhost = smtp.gmail.com:587\nsmtp_tls_security_level = verify\nsmtp_tls_mandatory_ciphers = high\nsmtp_tls_verify_cert_match = hostname\nsmtp_sasl_auth_enable = yes\nsmtp_sasl_security_options = noanonymous\nsmtp_sasl_password_maps = hash:\/etc\/postfix\/password_maps<\/pre>\n<p>The example assumes you&#8217;re using Gmail for the <em>relayhost<\/em> setting, but you can substitute the correct hostname and port for the mail host to which your system should hand off mail for sending.<\/p>\n<p>For the most up-to-date details about the above configuration options, see the man page:<\/p>\n<pre class=\"wp-block-preformatted\">$ man postconf.5<\/pre>\n<h2>Enable, start, and test Postfix<\/h2>\n<p>After you have updated the main.cf file, enable and start the Postfix service:<\/p>\n<pre class=\"wp-block-preformatted\"># systemctl enable --now postfix.service<\/pre>\n<p>You can then exit your <em>sudo<\/em> session as root using the <em>exit<\/em> command or <strong>Ctrl+D<\/strong>. You should now be able to test your configuration with the <em>mail<\/em> command:<\/p>\n<pre class=\"wp-block-preformatted\">$ echo 'It worked!' | mail -s \"Test: $(date)\" glb@gmail.com<\/pre>\n<h2>Update services<\/h2>\n<p>If you have services like <a rel=\"noreferrer noopener\" aria-label=\"logwatch (opens in a new tab)\" href=\"https:\/\/src.fedoraproject.org\/rpms\/logwatch\" target=\"_blank\">logwatch<\/a>, <a rel=\"noreferrer noopener\" aria-label=\"mdadm (opens in a new tab)\" href=\"https:\/\/fedoramagazine.org\/mirror-your-system-drive-using-software-raid\/\" target=\"_blank\">mdadm<\/a>, <a rel=\"noreferrer noopener\" aria-label=\"fail2ban (opens in a new tab)\" href=\"https:\/\/fedoraproject.org\/wiki\/Fail2ban_with_FirewallD\" target=\"_blank\">fail2ban<\/a>, <a rel=\"noreferrer noopener\" aria-label=\"apcupsd (opens in a new tab)\" href=\"https:\/\/src.fedoraproject.org\/rpms\/apcupsd\" target=\"_blank\">apcupsd<\/a> or <a href=\"https:\/\/www.linux.com\/learn\/automated-certificate-expiration-checks-centos\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"certwatch (opens in a new tab)\">certwatch<\/a> installed, you can now update their configurations so that their email notifications will go to your internet email address.<\/p>\n<p>Optionally, you may want to configure all email that is sent to your local system&#8217;s root account to go to your internet email address. Add this line to the <em>\/etc\/aliases<\/em> file on your system (you&#8217;ll need to use <em>sudo<\/em> to edit this file, or switch to the <em>root<\/em> account first):<\/p>\n<pre class=\"wp-block-preformatted\">root: glb+root@gmail.com<\/pre>\n<p>Now run this command to re-read the aliases:<\/p>\n<pre class=\"wp-block-preformatted\"># newaliases<\/pre>\n<ul>\n<li>TIP: If you are using Gmail, you can <a rel=\"noreferrer noopener\" aria-label=\"add an alpha-numeric mark (opens in a new tab)\" href=\"https:\/\/gmail.googleblog.com\/2008\/03\/2-hidden-ways-to-get-more-from-your.html\" target=\"_blank\">add an alpha-numeric mark<\/a> between your username and the <strong>@<\/strong> symbol as demonstrated above to make it easier to identify and filter the email that you will receive from your computer(s).<\/li>\n<\/ul>\n<h2>Troubleshooting<\/h2>\n<p><strong>View the mail queue:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">$ mailq<\/pre>\n<p><strong>Clear all email from the queues:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\"># postsuper -d ALL<\/pre>\n<p><strong>Filter the configuration settings for interesting values:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">$ postconf | grep \"^relayhost\\|^smtp_\"<\/pre>\n<p><strong>View the <em>postfix\/smtp<\/em> logs:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">$ journalctl --no-pager -t postfix\/smtp<\/pre>\n<p><strong>Reload <em>postfix<\/em> after making configuration changes:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">$ systemctl reload postfix<\/pre>\n<hr class=\"wp-block-separator\" \/>\n<p><em>Photo by&nbsp;<\/em><a href=\"https:\/\/unsplash.com\/@sharonmccutcheon?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\"><em>Sharon McCutcheon<\/em><\/a><em>&nbsp;on&nbsp;<a href=\"https:\/\/unsplash.com\/search\/photos\/envelopes?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\">Unsplash<\/a><\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Communication is key. Your computer might be trying to tell you something important. But if your mail transport agent (MTA) isn&#8217;t properly configured, you might not be getting the notifications. Postfix is a MTA that&#8217;s easy to configure and known for a strong security record. Follow these steps to ensure that email notifications sent from [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[644,261,606,45,43,645,334,46,646,647,47,648,649,609,650,651,44],"class_list":["post-97714","post","type-post","status-publish","format-standard","hentry","category-fedora-os","tag-app-password","tag-email","tag-faqs-and-guides","tag-fedora","tag-for-system-administrators","tag-gmail","tag-linux","tag-magazine","tag-mailx","tag-mobile","tag-news","tag-postfix","tag-relay","tag-secure","tag-sendmail","tag-smtp","tag-using-software"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/97714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=97714"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/97714\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=97714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=97714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=97714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}