I like my servers to run unattended and to send me emails from time to time: when a cron job finished, when something bad happens, when something good happens. This way, I don’t have to login to all of them and manually check everything is all right, and I can see what they do from my phone.
On the other hand, I would just hate to have a full-featured MTA installed on all my servers and virtual machines, just to send a few emails a day. I pretty much preffer to use a really simple program that can send emails to a remote server via SMTP and require as little maintainance as possible.
Such a program is msmtp. I just love it! It’s small, easy to configure and use, and it can even replace sendmail (which is the default program used by cron, at and friends to send emails with the results of their actions).
After I install msmtp and copy my config file to a new server or VPS, I can just link /usr/lib/sendmail and /usr/sbin/sendmail to point to /usr/bin/msmtp and I’m all set: all programs that I use can send me emails.
Except… they can’t. Because most cron jobs will try to send a email to “root”. Any reasonable SMTP server (the one that I use included) will just refuse to receive remote emails for “root”, so in most cases simply replacing sendmail with msmtp will just not work.
What is needed is to convince msmtp to use aliases, so it will know that email sent to root is actually to be delivered to my private email. The bad news is that msmtp itself does not support aliases out the the box.
Fortunately, someone already thought about this, so there is a sample script in /usr/share/doc/msmtp/examples/find_alias (Debian setup), called find_alias_for_msmtp.sh, which does exactly this: it can be called instead of msmtp, will look into /etc/aliases, apply any aliases and then run msmtp with the right email address as a recipient.
Please note that the script that is included in the Debian package required some patching in order to work correctly, so I suggest you download my version here.
After you download the script, edit it (there’s a default email address in there), then copy it to /usr/bin and make sendmail a link to it:
# chmod +x /usr/bin/msmtp.sh # cd /usr/lib # ln -sf ../bin/msmtp.sh sendmail # cd /usr/sbin # ln -sf ../bin/msmtp.sh sendmail
And of course, define root, postmaster or other system/local accounts as aliases to your real email address. The file you need to edit is /etc/aliases (create it if it doesn’t exist):
root: you@server.com postmaster: you@server.com
And that’s it! If your /etc/msmtprc file is also correctly configured (read the manual), you should receive in your regular inbox all email that was sent by cron, at and other system tools.
Enjoy, and let me know what you think in the comments section!
Image credit: Fletcher Prince.
Related posts:







Afair, nullmailer does have own way to route all local emails to an administrative remote mailbox, no aliases nor extra tweak required. Not sure though how other features compares to those available in msmtp.