Saturday, March 5, 2016

Linux: How To Setup Email Alerts on Linux Using Gmail as SMTP

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 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).

Setup

To install the ssmtp (Simple S.M.T.P) package, use the following command:

sudo aptitude install ssmtp

Then edit the configuration file:

#vi /etc/ssmtp/ssmtp.conf

Adjust and add as necessary the following parameters:

root=username@gmail.com

Change it from postmaster to the machines admin’s Email.

mailhub=smtp.gmail.com:587

Your mail server in our case this is Gmail so we have to specify the port as 587, for regular SMTP servers this is usually not necessary.

hostname=username@gmail.com

Usually the name of the machine is automatically filled by the package setup, if the machine has a mailbox this should be fine, but if it doesn’t or the name is not the same as the mailbox adjust accordingly.

UseSTARTTLS=YES

Enable TLS for secure session communication.

AuthUser=username

The username of the sending mailbox

AuthPass=password

The password of the sending mailbox

FromLineOverride=yes

Sends the hostname instead of root[root@hostname.FQDN].

In order to make the default (root) “from” field be the server name, edit the/etc/ssmtp/revaliases file:

#vi /etc/ssmtp/revaliases

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

root:machine-name@some-domain.com:smtp.gmail.com

Incredibly this is all you have to do to enable the ability. From now on, the machine will Email you when something is up.

Confirming setup

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

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

The “-vvv” turns on verbosity output so don’t get alarmed… this is just in case you encounter any problems, you will have some sort of output to Google for.

If all goes well, you should be getting the Email in a couple of seconds.

No comments:

Post a Comment