Friday, 26 January 2018

How To Send E-Mail Alerts on a Redhat/CentOS VPS for System Monitoring

Mail User Agent (E-Mail Client)

Numerously available "mail user agents" are applications that are used by users (i.e. you) or other applications to send and receive e-mails. They depend on message transfer agents (MTAs) in order to work. Microsoft Outlook, Mozilla Thunderbird or even Gmail --which works online-- are all suitable examples for mail user agents.

How the mail command works :

The mail command invokes the standard sendmail binary (/usr/sbin/sendmail) which in turns connects to the local MTA to send the mail to its destination. 
The local MTA is a locally running smtp server that accepts mails on port 25.

Simple Mail Transfer Protocol (SMTP) :

In order to transfer messages between hosts, a common language (i.e. a protocol) needs to be established for them to be able to communicate with each other. Created and standardized decades ago, SMTP has become the way for sending out messages. MTAs, using the SMTP protocol, do the delivery of e-mails.

In this tutorial we will be installing mailx which is available on redhat and centos operating systems 

Installing mailx

On your CentOS/RHEL machine, execute the following:

$ yum install -y mailx

Some monitoring scripts and applications might use "email" instead "mail" or "mailx" to send e-mails. If you find yourself in this situation, you can create a symbolic link, pointing (referencing) to mailx.

In order to create a symbolic link, run the following (replace /bin/email with the link name required):

$ ln -s /bin/mailx /bin/email

Set an External SMTP Server to Relay E-Mails

In order to set up a SMTP server [configuration] for "mailx" to use, we need to edit the contents of /etc/mail.rc file where the application's [certain] settings are found.

$ vi /etc/mail.rc

Below you can find an example SMTP settings, which you will need to modify to match your provider's before appending to the top of "mail.rc".

Example : Here is domain 

set smtp=smtps://
set smtp-auth=login
set smtp-auth-password=PASSWORD
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/

Sending a simple message:

echo "Your message" | mail -s "Message Subject" email@address

Sending a message with an attachment:

echo "Message" | mail -s "Subject" -a /loc/to/attachment.txt email@address

Reading the message body from a file:

echo | mail -s "Subject" -r from@address -q /loc/to/body.txt email@address

$ mail -s "Hello World" email@address < /home/user/mailcontent.txt

Sending to multiple recipients

$ mail -s "Hello World",

Specify the FROM name and address

# echo "This is the message body" | mail -s "This is the subject"

Testing the email


Post a Comment