Setting up msmtp¶
For a long time I struggled with setting up cronjobs properly. It always took a lot of trial and error. Most of the times this was due to an environment problem (or a typo). You could test your cronjob but it would also be nice to be notified whenever something goes wrong (such as your backup script failing).
In this post I explain how to set up msmtp so that your system will send you emails.
I had actually seen this output in the syslog before but never really cared much about it:
cron sends an email whenever a cronjob has an output, unless of course it cannot send the email.
So I recently set up msmtp on my server(s).
I started with these instructions.
In the end this is how it is working for me:
- 
Install
msmtp: - 
Create the config file
msmtprcin/etc(you might also need one for each user who wants to send email in~/.msmtprc)- I tried to use 
gpgfor password management to avoid storing the password as plain text. However,gpgencrypted passwords can not be decrypted with cron/sendmail. If you can, use a dedicated email account for this purpose. - Set 
chmodfor those files to0600, otherwisemsmtpwill complain (sendmail: /home/<user>/.msmtprc: contains secrets and therefore must have no more than user read/write permissions) 
 - I tried to use 
 - 
For cron to know where to send email to you need to do one of the following:
- specify default email in 
/etc/aliases - specify 
MAILTO=recipient@domain.tldin thecrontab 
 - specify default email in 
 - 
Finally, cron uses
sendmailto send out emails. Formsmtp v1.8.8+installmsmtp-mtaand set theset_from_headerconfiguration setting toon. The from address can be set toSome Name <server@domain.tld>to customize the name display.For older versions, you could just install the
msmtp-mtapackage. However, you then get aFromheader in the emails as "root (Cron Daemon) \<>". If you want to customize this, create asendmailalias in/usr/sbin. This is especially handy if you set this up for several servers and want to see which server an email is coming from. Follow these instructions to properly set it up. 
The msmtp configuration then looks as follows:
# find out more about the configuration here: https://marlam.de/msmtp/msmtprc.txt
# Set default values for all following accounts.
defaults
auth           on
tls            on
tls_starttls   on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile        /var/log/msmtp
aliases /etc/aliases
set_from_header on
# server
account        server
host           mail.domain.tld
port           587
from           "cron@srv <server@domain.tld>"
user           server@domain.tld
password
# Set a default account
account default : server
Comments
Comments are currently not supported. For the time being, please send me an email.