Send email alerts using ssmtp

Here’s how to setup sending emails through command line. A common application could be to send an alert message through user created script.

Prerequisites

  • Ubuntu Server/Desktop
  • Gmail account

Setup

Update Ubuntu Repository

sudo apt-get update

Install the ssmtp package

sudo apt-get install ssmtp

configure ssmtp

Edit the configuration file. The lines without the # are the ones we are interested in.

sudo nano /etc/ssmtp/ssmtp.conf

Adjust and add as necessary to match the following parameters

Change "MyEmailAddress" and "MyPassword" to your own.

# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
#root=postmaster
root=MyEmailAddress@gmail.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
#mailhub=mail
mailhub=smtp.gmail.com:587

AuthUser=MyEmailAddress@gmail.com
AuthPass=MyPassword
UseTLS=YES
UseSTARTTLS=YES

# Where will the mail seem to come from?
#rewriteDomain=
rewriteDomain=gmail.com

# The full hostname
#hostname=MyMediaServer.home
hostname=MyMediaServer.home

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

Create aliases for local usernames (optional)

edit the /etc/ssmtp/revaliases file

sudo nano /etc/ssmtp/revaliases

And add into it the desired translation which in our Gmail examples case will be

root:username@gmail.com:smtp.gmail.com:587
mainuser:username@gmail.com:smtp.gmail.com:587

From now on, the machine will Email when requested through command line or script.

Check setup

Lets test that our ssmtp setup was correct by sending an Email

echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv destination-email-address@some-domain.com

The “-vvv” turns on verbosity output.

Errors

Unfortunately a few thing can go wrong.

  • Check username and password again

  • Check google is not blocking your new device

EmailAlerts (last edited 2017-05-11 22:03:33 by evilnick)