SendMail Function Failure

Hi All,

Background: We use SendMail function (given below) to send emails to users. The email address are obtained as ouptut of a stored procedure in sybase.

We have defined a SendMail function as below in a .pm file and it is used in a .pl script.

Code Snippet:

sub SendMail
{
    my (
         $Message ,
         $Subject ,
         $To,
         $CC
       ) = @_;
    my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time) ;
    my $PgmName = "$hour:$min:$sec" ;
    Log( "\nRecepients To: << $To >>\n" ) ;
    Log( "\nRecepients CC: << $CC >>\n" ) ;
    open(SENDMAIL, "|/usr/lib/sendmail -oi -t")
        || die "Can't fork for sendmail: $!\n";
#Content-Transfer-Encoding: quoted-printable
    print SENDMAIL << "EOF";
From: $MAIL_FROM
Date: $PgmName
To: $To
CC: $CC
Subject: $Subject
Content-Type: text/html; charset="iso-8859-1"
$Message
EOF
close(SENDMAIL) || die "sendmail didn't close nicely";
   if( $? !=0  )
   {
      Log("System Mail Failed");
   }
   else
   {
      Log ("Mail Sent Successfully");
   }
}

The above SendMail is called in a perl script.

Issue:

Lets say we have 10 email address to which emails has to be send, and the 5th email address's mail box is no longer available though the email address is a valid one. Henceforth the sendmail function fails with "
sendmail didn't close nicely " message and email is not send to the rest of the people.

Expected Resolution Options

i) Validate whether the mailbox is still active. OR
ii) Prevent the sendmail from failing & continue to send the mail to the other people.

Kindly provide a possible solution. Thanking All in advance for your time.

I had similar problems with sendmail (fails to exit properly and sometimes locks up). I switched to MIME::Lite and all these problems went away.

Also found it a good idea to validate the email addresses (from and to) before trying anything (Email::Valid is pretty easy to use).

I've thrown together an example of what I use below.

#!/usr/bin/perl
 
use MIME::Lite;
use Email::Valid;
 
...
 
if (!Email::Valid->address($MAIL_FROM)) {
    Log("From email address << $MAIL_FROM >> is invalid\n") ;
    exit ;
}
 
... Other checks here ...
 
my $msg;
 
### Create the multipart "container" to be sent to recipient:
$msg = MIME::Lite->new(
    From      =>$MAIL_FROM,
    To        =>$TO_EMAIL,
    Subject   =>$EMAIL_SUBJECT,
    Type      =>'multipart/mixed'
        ) or die "Error creating multipart container: $!\n";
 
### Add the body part:
$msg->attach(
    Type     =>'TEXT',
    Data     =>$EMAIL_BODY."\n\n"
    ) or die "Error adding the text message part : $!\n";
 
### Add a pdf attachement:
$msg->attach(Type     => 'application/postscript',
    Path     => $TEMP_PDF_FILE,
    Filename => $ATTACH_NAME,
    Disposition => 'attachment'
    );
 
eval { $msg->send('smtp', "my.smtp.server.com", Timeout=>60); };
die "Error: $@" if ($@);
 
...