Linux Email Notifications for Cron Jobs and Bash Scripts

In a Linux environment, managing email notifications for tasks like cron jobs and setting up email services can often be complex. This article explores the efficient setup and use of msmtp for managing email notifications in Linux, specifically for cron job monitoring and Bash script outputs. It provides a straightforward guide to configuring msmtp, demonstrates how to direct cron outputs to specific email addresses, and shows how to send error messages only.

Monitoring Cron Jobs with Email Notifications

Monitoring cron jobs is essential, especially for important tasks like backups. You configure it once and expect it to run seamlessly, ensuring error-free operations while also providing the assurance that you'll be promptly notified should any issues arise. The MAILTO environment variable in cron is a straightforward way to receive output from your cron jobs. It sends the output (standard or error) of the job to the specified email address.

Setting Up MAILTO in crontab

The MAILTO environment variable in cron allows you to specify an email address to receive output from your cron jobs. If a job produces output (either standard output or standard error), cron sends this output in an email to the address specified in MAILTO.

MAILTO=email@example.com
0 */2 * * * /bin/backup.sh

This example will send the output of /bin/backup.sh to email@example.com every 2 hours.

Focusing only on Error Messages

To only be informed of errors, redirect the standard output to /dev/null. This way, emails are sent only when there's an error output.

MAILTO=email@example.com
0 */2 * * * /bin/backup.sh > /dev/null

Here, emails are sent only if /bin/backup.sh encounters an error.

But to make it work you have to configure an MTA (Mail Transfer Agent) in linux.

Setting Up and Configuring msmtp for Email Sending

If your our expectation is system merely sends email alerts and notifications, without the need for managing a full mail server then, msmtp aligns best with such use cases, as it is designed to forward emails to a mail server rather than act as one. msmtp offers a more accessible and simpler option for email sending from Bash, compared to other solutions like Postfix.

Installation

apt-get install msmtp msmtp-mta mailutils

(Opt for "No" if prompted for AppArmor support during installation.)

Configuration

Configure msmtp via the /etc/msmtprc file:

defaults
port 587
tls on
auth on
syslog on

account office@mail.pl
host pro1.mail.ovh.net
from office@mail.pl
user office@mail.pl
password YOUR-PASS

account default : office@mail.pl

Bear in mind that, after you test all works fine, your password should be encrypted using eg. GnuPG (GPG).

Testing Sending Emails

No it's time to check if the configuration works well:

mail -r office@mail.pl -s "Test mail" john@gmail.com  <<END
This is a test
END

Specify the sender's address explicitly if you encounter issues with the account default.

Troubleshoting msmtp

msmtp does not function as a background daemon. Instead, use journalctl -xe | grep msmtp for monitoring logs and troubleshooting.

Why to use Email Service Over Self-Hosted for system alerting

When it comes to managing emails in a Linux environment, you might be tempted to set up your own email server. However, there are compelling reasons to opt for a mailbox from a professional email service provider instead.

Msmtp vs Postfix for mail alerts

You may wondering why to choose msmtp over Postfix for sending mail from a Linux. Here are some other reasons why msmtp is a better choice in above scenarios.

Resources