How To Setup Email Alerts on Mint Linux Using sSMTP
Here’s how to setup mail and ssmtp to send alerts to your gmail account.
This technique is meant for your Linux box sitting in your house acting as a home server. In fact, since many home ISPs block regular outbound email, you might find this technique a great way to ensure you still get administration emails, even from your home servers.
Configuring the Email sending ability for a machine immediately gives us the upshot that a lot of the system’s administration problems and critical alerts will be automatically sent to that system’s designated administrator. The most noteworthy example is that cron job execution errors fall into this category and therefore will notify the admin that there was a problem with scheduled jobs such as backups as soon as they happen including the standard error (stderr) output in the report. We will also be able to incorporate emailing from bash scripts.
Don’t worry—it usually doesn’t spam you with too much information and you can just de-configure it if it becomes to annoying.
On Debian based systems, other then a lot of very versatile emailing related programs there is also the ssmtp package, which is well suited if all you want to do is have an agent to send emails using another email server (MTA), without having to setup an entire postfix or sendmail configuration to do so.
Prerequisites and assumptions
Before we get started, you’ll want to make sure that all of these conditions are met:
- You have an SMTP server that can receive the emails from your machines and send them to the recipient (i.e. your corporate exchange or Gmail).
- You have the credentials for a user that is able to send Email on that server (i.e. a mailbox or a Gmail account).
- This procedure has been used and tested on Mint.
- You will see me use gedit as the editor program, this is just because I’m used to it… you may use any other editor that you’d like.
To install the ssmtp (Simple S.M.T.P) package, use the following command:
sudo apt-get install ssmtp
Now edit config file:
sudo gedit /etc/ssmtp/ssmtp.conf
Config ssmtp.conf file for sSMTP
# Config file for sSmtp # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. #root=postmaster # MX records are consulted. Commonly mailhosts are named mail.domain.com #mailhub=mail mailhub=smtp.gmail.com:587 AuthUser=Gmail username AuthPass=Gmail password #For TLS and SSL UseSTARTTLS=YES # Where will the mail seem to come from? #rewriteDomain= rewriteDomain=gmail.com # The full hostname #hostname=MyMediaServer.home email@example.com # Are users allowed to set their own From:? # YES - Allow the user to specify their own From: # NO - Use the system generated From: address FromLineOverride=YES
Now edit a new config file:
sudo gedit /etc/ssmtp/revaliases
root:firstname.lastname@example.org:smtp.gmail.com:587 user:email@example.com:smtp.gmail.com:587 // note: $ whoami # gives user
sudo apt-get install mailutils
Now you can test something like this.
sudo df -h | mail -s "Disk Info" firstname.lastname@example.org
If all goes well, you should be getting the Email in a couple of seconds.
To troubleshoot start with checking your mail log.
sudo tail -f /var/log/mail.log