Bullock, Howard A. wrote:
> I use MIME::Lite for sending email from my programs. If the mail server
> can not be contacted the following MIME::Lite code causes a fatal error
> aborting my process.
>
> ### Create SMTP client:
> require Net::SMTP;
> my $smtp = MIME::Lite::SMTP->new(@args)
> or Carp::croak("Failed to connect to mail server: $!\n");
> $smtp->mail($from)
> or Carp::croak("SMTP MAIL command failed:
> $!\n".$smtp->message."\n");
> $smtp->to(@to_all)
> or Carp::croak("SMTP RCPT command failed:
> $!\n".$smtp->message."\n");
> $smtp->data()
> or Carp::croak("SMTP DATA command failed:
> $!\n".$smtp->message."\n");
>
>
> I tried to trap this issue in my code using "eval", but was
> unsuccessful.
>
> if (eval {$msg->send}) {
> print "message sent\n";
> }
> else {
> print "Failed to send: [EMAIL PROTECTED]";
> }
>
> Did I use the eval improperly or is there a better to prevent the
> stopping of my process?
Try something like this (you'll need to get your @args adjusted):
use strict;
use warnings;
use MIME::Lite;
my $message = "Sending test\n";
my $from_address = '[EMAIL PROTECTED]';
my $to_address = '[EMAIL PROTECTED]';
my $subject = 'yadda yadda yah';
my $message_body = $message;
MIME::Lite->send('smtp', 'mymailserver.mynetwork.com', Timeout => 60);
my $msg = MIME::Lite->new(
From => $from_address,
To => $to_address,
Subject => $subject,
Data => $message_body,
) or die "Error creating MIME body: $!";
eval { $msg->send; };
if ($@) {
print "Failed to send: $! ($^E)";
} else {
print "message sent\n";
}
__END__
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs