Introduction
In this tutorial you will learn how to setup sSMTP in order to receive email notifications from your server. This makes it possible to get the output of a cronjob via email.
Prerequisites
- An email account that will send the emails.
Step 1 - Install sSMTP
Connect to your server via SSH, and install sSMTP. You can do that by running: apt update && apt install -y ssmtp
Step 2 - Update the sSMTP Configuration
The configuration files of sSMTP can be found under /etc/ssmtp
.
Use nano /etc/ssmtp/ssmtp.conf
to open the config in an editor.
Now you have to add some lines to the final config. When you are done it should look like this:
[email protected]
mailhub=mail.example.com:465
FromLineOverride=NO
UseSTARTTLS=YES # Does your email server support start tls if no simply change this to NO
UseTLS=YES # Does your email server support tls if no simply change this to NO
[email protected]
AuthPass=password
rewriteDomain=yourdomain.com
[email protected]
Once that is done you will need to edit /etc/ssmtp/revaliases
. You can use nano /etc/ssmtp/revaliases
to edit the file.
The config should look like this when you are done editing:
# sSMTP aliases
#
# Format: local_account:outgoing_address:mailhub
#
# Example: root:[email protected]:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
root:[email protected]:mail.example.com:465
Step 3 - Setup a cronjob
Now you can setup a cronjob with crontab -e
then choose the editor you like (for example nano).
Before your cronjobs you have to add [email protected]
. Now you will get an email whenever a cronjob runs.
Step 4 - Exclude a cronjob from sending email
If you don't want a cronjob to send emails you can simply add a >/dev/null 2>&1
to its end. This will redirect the output of that job to /dev/null
and no email will be sent.
Conclusion
With sSMTP configured on your server, you will now get emails for every cronjob. This can be super helpful for example if you are auto upgrading some services on your machine. However, it can also be annoying. If you for example have a cronjob that runs every minute you will also get an email every minute, so make sure you carefully select which cronjobs will send emails.