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 crontabThe 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.
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.
msmtp
for Email SendingIf 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.)
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).
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
.
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 msmtp
over 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.