Activities

October 2011
M T W T F S S
« Sep   Nov »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Bash : Sending email using SMTP authentication

I’m having a need to sending email alerts using Google SMTP which may help to notify faster and mails were not go to spam box ( Remember 250 smtp relay limit on Google account). Also this is the best solution if you want to send a mail safely to other domain rather than using the default smtp which would probably send email from “localhost” having poor mail reputation among on other SMTP servers.

These mails are blocked most of the cases either by looking the IP reputation from where mails come from or identifying the sender from address. Also found “MUTT” is another great tool to send email those having large attachment. I had posted about it this blog earlier.

How to send email using smtp account from bash

1. Install sendEmail : – Download the sendmail program from http://caspian.dotconf.net/menu/Software/SendEmail/. Extract it and copy the file “sendEmail” to system path. This is a perl program which requires certain SSL libraries installed on system if you are planning to use Google SMTP.

[root@host2 sendEmail-v1.56]# ls
CHANGELOG  README  README-BR.txt  sendEmail  sendEmail.pl  TODO
[root@host2 sendEmail-v1.56]# cp sendEmail /usr/bin/

2. Install Perl Moudles required for Google SMTP. you need to install Net::SSLeay and IO::Socket::SSL for this.

[root@rc-123 public_html]# perl -MCPAN -eshell
Terminal does not support AddHistory.
cpan shell -- CPAN exploration and modules installation (v1.7602)
ReadLine support available (try 'install Bundle::CPAN')
cpan> install Net::SSLeay
cpan> install IO::Socket::SSL

That’s it here you go, This is the command to send email using Google SMTP,

#sendEmail -f nagios@serveridol.com -t liju@domain1.com -u "This is a test mail" -m " Hello this my test mail using Google smtp"  -s smtp.gmail.com:587  -xu nagios@serveridol.com  -xp password  -o tls=yes

-f : Email from address.
-t : Email to address.
-u : Email Subject.
-m : Email message body.
-s : server name (if your smtp using special port, you need to mentioned it by column)
-xu : Username associated with the above smtp server.
-xp : Password of SMTP username associated with the above smtp server.
-o tls : Forcing tls encryption to be used.

Integrating Nagios with Google SMTP

1. Open the file /usr/local/nagios/etc/resource.cfg

$USER5$=nagios@serveridol.com
$USER7$=smtp.gmail.com:587
$USER9$=nagios@serveridol.com
$USER10$=password

Nothing to wonder. It’s just some variable declarations !!! .

2. Now open the file ” /usr/local/nagios/etc/objects/commands.cfg” comment out the notification associated with notify-host-by-email and notify-host-by-email . make a backup of this file first.

Then replace it with the following lines,

    a. This is for “notify-host-by-email”
#'notify-host-by-email' command definition
define command{
 command_name notify-host-by-email
 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/sendEmail -s $USER7$ -xu $USER9$ -xp $USER10$ -t $CONTACTEMAIL$ -o tls=yes -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n"
 }

    b. This is for notify-service-by-email

# 'notify-service-by-email' command definition
define command{
 command_name notify-service-by-email
 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/bin/sendEmail -o tls=yes -s $USER7$ -t $CONTACTEMAIL$ -f $USER5$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu $USER9$ -xp $USER10$  -m "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$"
 }

Then restart the nagios. All should be work.

If you want to check whether sendEmail program is working or not ?
Create the default log file and set the permission. Sendmail log all the email sending activities in this file.

[root@rc-012] touch /var/log/sendEmail
[root@rc-012] chmod 666 /var/log/sendEmail

Happy SendEmailing !! 🙂

1 comment to Bash : Sending email using SMTP authentication

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>