Send system email with Gmail and sSMTP
First, let’s make sure that you have Mutt installed to send emails
sudo -i apt install mutt
Next, let’s make sure root has an email directory.
touch /var/mail/root
Required Packages
Luckily, installing this on Debian/Ubuntu is dead simple.
apt-get install ssmtp
Then, you’ll need to edit your ssmtp.conf file. If you have two factor authentication turned on, I’d just you create a one-time password for this in Gmail and use that as the password below.
nano /etc/ssmtp/ssmtp.conf
and paste something like this in.
root=GmailUsername@gmail.com mailhub=smtp.gmail.com:587 rewriteDomain=gmail.com hostname=fileserver.local TLS_CA_FILE=/etc/ssl/certs/ca-certificates.crt UseTLS=YES UseSTARTTLS=YES AuthUser=GmailUsername AuthPass=GmailPassword AuthMethod=LOGIN FromLineOverride=YES
* Press crtl+x, and then y to save and exit. Then add each account that you want to be able to send mail from by editing,
nano /etc/ssmtp/revaliases
and paste…
root:GmailUsername@gmail.com:smtp.gmail.com:587 youruser:GmailUsername@gmail.com:smtp.gmail.com:587
Just replace youruser with your Ubuntu username and GmailUsername@gmail.com with your Gmail address. That’s it 🙂
Conclusion
Try a test email with Mutt.
mutt
This is a command line email client, so it’s not “quite” as flashy as the web-based Gmail client. Here are the steps.
- At the first screen with the blue bars at the top and bottom, press m for a new email.
- Next, type in the email address that you’d like to send the email to at the bottom of the screen, and press ENTER when you are done.
- Next, type the Subject for your email as TEST EMAIL FROM MUTT and press ENTER again.
- Then, it will load up a Nano screen for you to type your email.
- Type in the contents of your email.
- When you are done, press Ctrl+x, then y to confirm the email and exit.
When everything is setup, you will see a screen like this.
y:Send q:Abort t:To c:CC s:Subj a:Attach file d:Descrip ?:Help From: rootTo: testemail@gmail.com Cc: Bcc: Subject: TEST EMAIL FROM MUTT Reply-To: Fcc: ~/sent Mix: Security: None -- Attachments - I 1 /tmp/mutt-frontend-0-4010345-16456953681 [text/plain, 7bit, us-ascii, 0.1K] -- Mutt: Compose [Approx. msg size: 0.1K Atts: 1]-----------------------------------------------------------------------------------------------------------------------------------------
Press y to send the test email. If it worked, it will say sending Message at the bottom of the screen, followed by Mail sent. Press q to exit Mutt.
Now that this is configured, you can use this for any system alerts you might have.
- SnapRAID Sync Script
- Hard drive space usage
- mdadm
- cron jobs
- etc.