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 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.
MAILTO in crontab
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
0 */2 * * * /bin/backup.sh
This example will send the output of
firstname.lastname@example.org every 2 hours.
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.
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.
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.
apt-get install msmtp msmtp-mta mailutils
(Opt for "No" if prompted for AppArmor support during installation.)
msmtp via the
account default : email@example.com
Bear in mind that, after you test all works fine, your password should be encrypted using eg. GnuPG (GPG).
No it's time to check if the configuration works well:
mail -r firstname.lastname@example.org -s "Test mail" email@example.com <<END
This is a test
Specify the sender's address explicitly if you encounter issues with the
msmtp does not function as a background daemon. Instead, use
journalctl -xe | grep msmtp for monitoring logs and troubleshooting.
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.
You may wondering why to choose
Postfix for sending mail from a Linux. Here are some other reasons why
msmtp is a better choice in above scenarios.
msmtp is a straightforward SMTP client with minimal setup requirements, making it ideal for sending emails without the overhead of managing a full email server.
msmtp consumes fewer resources than
Postfix, which is beneficial for just alerts and notification sending.
msmtp is designed specifically for sending emails and doesn't include the more complex server functionalities that Postfix offers. This makes it a great tool for scripts, cron jobs, and systems where only email sending (and not receiving or managing mailboxes) is required.