Blogger templates

Monday, 8 April 2013

perl-send-email-without-using-sendmail.


perl-send-email-without-using-sendmail.

In a recent exercise, I was required to write a script which would attempt sending email through an smtp server instead of sendmail. At a periodic interval, the script would check if the smtp server is able to send emails or not. Basically, this script ran as a background process.

#!/usr/bin/perl

use Net::SMTP;
use POSIX qw/strftime/;
if (@ARGV != 5)
{
  usage();
  exit;             
}


my $smtpmailhost = $ARGV[0];
my $from_address = $ARGV[1];
my $to_address = $ARGV[2];
my $timeout = $ARGV[3];
my $interval = $ARGV[4];

my $logdir = "/tmp/email_logs/";

my $datestamp = strftime('%m-%d-%y_%H:%M:%S',localtime);
$mday = (localtime(time))[3];

my $logfile = $logdir."$smtpmailhost\_email_log\_$datestamp.log";
open LOG, ">>","$logfile" or die $!;
LOG->autoflush(1);
while(1) {
    $smtp = Net::SMTP->new($smtpmailhost,
                            Timeout => $timeout);
    $cur_datestamp = strftime('%m-%d-%y_%H:%M:%S',localtime);
    $cur_mday = (localtime(time))[3];
    if(!$smtp) {
        print LOG "\n$cur_datestamp : Error creating smtp object for $smtpmailhost: $!";
        sleep $interval;
        next;              
    }      
    if($cur_mday != $mday) {
        close(LOG);
        $logfile = $logdir."$smtpmailhost\_email_log\_$cur_datestamp.log";
        open LOG, ">>","$logfile" or die $!;
        LOG->autoflush(1);
        $mday = $cur_mday;
    }  
    $smtp->mail($from_address);
    $smtp->to($to_address);
    $smtp->data();
    $smtp->datasend("Subject: $smtpmailhost - Test email message\n");
    $smtp->datasend("\n");
    $smtp->datasend("Test email has been sent successfully at $cur_datestamp!");
    $smtp->dataend();
    $smtp->quit;
    print LOG "\n$cur_datestamp : Test Email sent via $smtpmailhost!";  
     sleep $interval;  
}

close(LOG);
sub usage
{
  print "Invalid parameters \nUsage: ./smtpemail_send.pl \n";
}

No comments:

Post a Comment

 

Blogroll

Hello Friends myself Sahaj Ohri live at Jaipur city , This Website is mainly for Youth, You can explore a youth network here on www.techowner.blogspot.in As it is generally an information sharing platform when you can get information and also share information. If you are having some information.I am 19 years old guy from India who have interest in making blogs.I am a simple guy who likes to spent his time with Friends,watching movies,and reading various types of books. I loves to sharemy personal view with those people who have interest inmaking website, blog etc.Here I also give you some technical knowledge about making website attractive and some other technical support(knowledge) using WordPress as your CMS.Dear Friends, As an author, I’m trying my best to improve this platform day by day. If you are having any idea or suggestion in your mind about this website then you are free to contact me and share your thought. Contact : For Issues regarding advertisement, copyright issues or ideas sharing, you can contact us on the following details. sahaj.ohri3@gmail.com

About

Hello Friends myself Sahaj Ohri live at Jaipur city , This Website is mainly for Youth, You can explore a youth network here on www.techowner.blogspot.in As it is generally an information sharing platform when you can get information and also share information. If you are having some information.I am 19 years old guy from India who have interest in making blogs.I am a simple guy who likes to spent his time with Friends,watching movies,and reading various types of books. I loves to sharemy personal view with those people who have interest inmaking website, blog etc.Here I also give you some technical knowledge about making website attractive and some other technical support(knowledge) using WordPress as your CMS.Dear Friends, As an author, I’m trying my best to improve this platform day by day. If you are having any idea or suggestion in your mind about this website then you are free to contact me and share your thought. Contact : For Issues regarding advertisement, copyright issues or ideas sharing, you can contact us on the following details. sahaj.ohri3@gmail.com