Martin's Tex-Blog

Posts on programming and generally technical topics

How to send email from your perl script?

leave a comment »


Suppose you have some script working as a cron job and you want to be informed of its status from time to time. The obvious thing is to use email notifications but as always its not that easy. Especially if you have SSL / TLS protected email provider like gmail. If you have configured local email server then its straightforward solution to just use it. Other wise you will want to use some of the extensions packages. I found that using packages that allow to use email servers like gmail is not easy – it probably requires recompilation of your perl distibution – I’am not going to dive into details here so if you want a quick solution then:

1. Use email provider that is not requiring SSL connection

2. Use Mail::SendEasy

as for 2. below is a simple routine to send email :

use Mail::SendEasy;

sub ReportEmail {
  my $body = shift;
  $status =1;
  $cnt=0;
  do { # in case of errors on the line...
    my $mail = new Mail::SendEasy(
      smtp => 'smtp.somwhere.com',
      port => 587,
      user => 'username' ,
      pass => 'password' ,
    ) ;
    $status = $mail->send(
      from    => 'martin@somewhere.com' ,
      from_title => 'script report' ,
      reply   => 'martin@somewhere.com' ,
      error   => 'martin@somewhere.com' ,
      to      => 'my_email@somewhere.com' ,
      subject => "WebSite was hijacked!!!!" ,
      msg     => $body,
      msgid   => "0101",
      );
    if (!$status) {
     # log errors here
     # print LOG "SendMailError (".$cnt.") ".$mail->error ;
      $cnt++;
    }
  } while($status == 0 && $cnt != 10);
}
Advertisement

Written by Marcin

January 6, 2011 at 7:26 pm

Posted in perl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: