<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>moritzvd.eu - Linux</title>
      <link>https://moritzvd.eu</link>
      <description></description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="https://moritzvd.eu/tags/linux/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Mon, 25 Nov 2019 20:40:00 +0000</lastBuildDate>
      <item>
          <title>Sending emails via SMTP on Ubuntu or Debian Servers</title>
          <pubDate>Mon, 25 Nov 2019 20:40:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://moritzvd.eu/posts/email-with-smtp-debian-ubuntu/</link>
          <guid>https://moritzvd.eu/posts/email-with-smtp-debian-ubuntu/</guid>
          <description xml:base="https://moritzvd.eu/posts/email-with-smtp-debian-ubuntu/">&lt;p&gt;I used to sift trough my shell history and bookmarks every time set up sending email on a new Debian or Ubuntu server. This should help everybody who just wants to send email trough an old school ISP or free email service.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Be aware don&#x27;t use &lt;code&gt;ssmtp&lt;&#x2F;code&gt; anymore. It&#x27;s unmaintained and has &lt;a href=&quot;https:&#x2F;&#x2F;tracker.debian.org&#x2F;news&#x2F;956307&#x2F;ssmtp-removed-from-testing&#x2F;&quot;&gt;been removed
from Debian&lt;&#x2F;a&gt;
and Ubuntu will most definitely follow suit.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;install-msmtp&quot;&gt;Install msmtp&lt;a class=&quot;anchor&quot; aria-hidden=&quot;true&quot; href=&quot;#install-msmtp&quot; hidden=&quot;&quot;&gt;#&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;First we need the awesome program called &lt;a href=&quot;https:&#x2F;&#x2F;marlam.de&#x2F;msmtp&#x2F;&quot;&gt;msmtp&lt;&#x2F;a&gt; to route all the server&#x27;s mail through a standard SMTP server.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;sudo apt-get install msmtp msmtp-mta mailutils&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;set-up-msmtp&quot;&gt;Set up msmtp&lt;a class=&quot;anchor&quot; aria-hidden=&quot;true&quot; href=&quot;#set-up-msmtp&quot; hidden=&quot;&quot;&gt;#&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;sudo nano &#x2F;etc&#x2F;msmtprc&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Set default values for all following accounts.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;defaults
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Use the mail submission port 587 instead of the SMTP port 25.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;port&lt;&#x2F;span&gt;&lt;span&gt; 587
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Always use TLS.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;tls&lt;&#x2F;span&gt;&lt;span&gt; on
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Set a list of trusted CAs for TLS. The default is to use system settings, but
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# you can select your own file.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;tls_trust_file&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;etc&#x2F;ssl&#x2F;certs&#x2F;ca-certificates.crt
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# The SMTP server of your ISP
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;account&lt;&#x2F;span&gt;&lt;span&gt; isp
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;host&lt;&#x2F;span&gt;&lt;span&gt; mail.isp.example
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; smithjoe@isp.example
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;auth&lt;&#x2F;span&gt;&lt;span&gt; on
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt; 12345
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Set default account to isp
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;account&lt;&#x2F;span&gt;&lt;span&gt; default: isp
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Map local users to mail addresses
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;aliases&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;etc&#x2F;aliases
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above is based on the &lt;a href=&quot;https:&#x2F;&#x2F;marlam.de&#x2F;msmtp&#x2F;msmtprc.txt&quot;&gt;example&lt;&#x2F;a&gt;. The
program has many more authentication methods.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;install-and-set-up-mailx&quot;&gt;Install and set up mailx&lt;a class=&quot;anchor&quot; aria-hidden=&quot;true&quot; href=&quot;#install-and-set-up-mailx&quot; hidden=&quot;&quot;&gt;#&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;In order to be able to use the &lt;code&gt;mail&lt;&#x2F;code&gt; command wee need to install &lt;code&gt;mailx&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;sudo apt-get install bsd-mailx&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Set mail transport agent to use &lt;code&gt;msmtp&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;sudo nano &#x2F;etc&#x2F;mail.rc&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;append the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt; mta=&#x2F;usr&#x2F;bin&#x2F;msmtp
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;set-up-aliases&quot;&gt;Set up aliases&lt;a class=&quot;anchor&quot; aria-hidden=&quot;true&quot; href=&quot;#set-up-aliases&quot; hidden=&quot;&quot;&gt;#&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;We need to link system users with email addresses in order for system users to receive mails from cronjobs.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;sudo nano &#x2F;etc&#x2F;aliases&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Send root to Jane
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;root:&lt;&#x2F;span&gt;&lt;span&gt; jane_doe@example.com
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Send everything else to admin
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;default:&lt;&#x2F;span&gt;&lt;span&gt; admin@domain.example
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;sudo nano &#x2F;etc&#x2F;mail.rc&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;append:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;alias &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;root &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;root&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;jane_doe@example.com&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Emails are now sent to this address if e.g. a cronjob fails. Also a general
fallback address is used if messages don&#x27;t belong to root. Of course more users
can be set.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;test-it&quot;&gt;Test it!&lt;a class=&quot;anchor&quot; aria-hidden=&quot;true&quot; href=&quot;#test-it&quot; hidden=&quot;&quot;&gt;#&lt;&#x2F;a&gt;
&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;echo &amp;quot;Hello World&amp;quot; | msmtp -d bob@example.com&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Test sending a mail to root&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;echo &amp;quot;Testing msmtp from ${HOSTNAME} with mail command&amp;quot; | mail -s &amp;quot;hi root&amp;quot; root&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Test sending a mail to another email adress&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;echo &amp;quot;Testing msmtp from ${HOSTNAME} with mail command&amp;quot; | mail -s &amp;quot;hi there&amp;quot; bob@example.com&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;links&quot;&gt;Links&lt;a class=&quot;anchor&quot; aria-hidden=&quot;true&quot; href=&quot;#links&quot; hidden=&quot;&quot;&gt;#&lt;&#x2F;a&gt;
&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;marlam.de&#x2F;msmtp&#x2F;&quot;&gt;https:&#x2F;&#x2F;marlam.de&#x2F;msmtp&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;Msmtp&quot;&gt;https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;Msmtp&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
    </channel>
</rss>
