May 15, 2020

MSMTP: Sending Mail With a Linux Server

If you are running a Linux server, you probably want to enable that thing to send emails to you. This is useful, for instance, to tell you that someone got blocked by fail2ban. In earlier years, SSMTP has been a good candidate to achieve this kind of thing. Today, Debian, Raspbian, and probably Ubuntu server OSes have no packet for SSMTP anymore. However, you can use MSMTP to achieve the same thing. The configuration is actually even more straight forward than with SSMTP but it has a terrible pitfall, which drove me mad for some time.

Please, install the packet msmtp and msmtp-mta:

apt install msmtp msmtp-mta

Otherwise, you won't be able to send emails. What msmtp-mta does is the following:

╭─root@host /home/foo
╰─➤  which sendmail
/usr/sbin/sendmail

╭─root@host /home/foo
╰─➤  ls -l /usr/sbin/sendmail
lrwxrwxrwx 1 root root 12 Feb 15  2019 /usr/sbin/sendmail -> ../bin/msmtp

Or in other words: it creates a symlink from sendmail to msmtp. So every program that calls sendmail actually calls msmtp and can send mail. On one server I did it correctly, on the other I forgot this step. The result was: no emails. Somehow I figured, that sendmail was missing, installed it, but still no emails. I always thought sendmail uses msmtp but that is wrong. msmtp replaces sendmail.

The configuration works as follows: In /etc/msmtprc you have to create the main configuration. Here you basically tell MSMTP the account details of the SMTP server it should use plus some other settings. For a GMAIL SMTP server it looks like this:

# Set default values for all following accounts.
defaults
auth                    on
tls                     on
tls_trust_file          /etc/ssl/certs/ca-certificates.crt
logfile                 ~/.msmtp.log

# Gmail
account        gmail
host           smtp.gmail.com
port           587
user           foobar@googlemail.com
from           foobar@googlemail.com
password       foobarpasswordfoobar

# Set a default account
account default: gmail
# Map local users to mail addresses (for crontab)
aliases /etc/aliases

The second configuration file is /etc/aliases. Here you tell the system how the different system users can be reached by mail.

root: holger.foo@blubber.bar
www-data: holger.foo@blubber.bar

© holger 2015 - 2020 |