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.
- Better Deliverability: Emails sent from self-hosted servers can often end up in spam folders due to various factors like IP reputation. Professional services have mechanisms in place to ensure higher deliverability rates.
- Simplified Maintenance: Running your own email server requires regular maintenance, updates, and monitoring. With a professional service, all these tasks are handled by the provider, freeing you from the complexities of server management.
- Time & Cost-Effectiveness: While setting up your own server might seem cost-effective, the hidden costs in terms of time, maintenance, and potential downtime can add up. Professional services offer predictable costs with better resource allocation.
- Reliability and Uptime: Professional email providers invest heavily in infrastructure to ensure high availability and reliability. They have dedicated teams to manage server uptime and handle any issues that might arise, which is crucial for critical alerts and notifications.
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.
- Simplicity and Ease of Use:
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. - Lightweight:
msmtp
consumes fewer resources thanPostfix
, which is beneficial for just alerts and notification sending. - Single-Purpose Focus:
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. - Less Maintenance: Running a full-fledged email server like Postfix requires regular maintenance, including handling security updates, managing the queue, and monitoring performance.