
FishMonger
Veteran
/ Moderator
Jan 8, 2009, 3:59 PM
Post #12 of 15
(4314 views)
|
Re: [frist44] How to handle failure of sub procedures?
[In reply to]
|
Can't Post
|
|
The main advantage of using Net::SMTP is that it's a core module. However, IMO its syntax is a little "noisy". I prefer to use MIME::Lite. http://search.cpan.org/~rjbs/MIME-Lite-3.023/lib/MIME/Lite.pm Try this version, which assumes that you've already verified that the passed in vars are properly defined.
sub sendEmail { # Bring in the passed parameters my ($s_email, $s_smtp, $s_ip) = @_; # Create mail object and set headers my $smtp = Net::SMTP->new($s_smtp, Timeout => 60); if (! $smtp) { warn "Failed to connect to $s_smtp smtp server\n"; return undef; } $smtp->mail($s_email); $smtp->to($s_email); $smtp->data(); # Set from, to and subject fields $smtp->datasend("From: $s_email\n"); $smtp->datasend("To: $s_email\n"); $smtp->datasend("Subject: $s_ip is NOT responding!!!"); $smtp->datasend("\n"); # Set email body $smtp->datasend("Importance: high\n"); $smtp->datasend(ctime()); $smtp->datasend("\n"); $smtp->datasend("\n"); $smtp->datasend("IP Address: $s_ip\n"); my $status = $smtp->dataend(); $smtp->quit; return $status; } Or my preference, which uses MIME::Lite
sub sendEmail { my ($s_email, $s_smtp, $s_ip) = @_; my $message = "Importance: high\n" . ctime() . "\n\n" . "IP Address: $s_ip\n"; MIME::Lite->send('smtp', $s_smtp, Timeout=>60); my $msg = MIME::Lite->new( From => $s_email, To => $s_email, Subject => "$s_ip is NOT responding!!!", Data => $message ); $msg->send; }
(This post was edited by FishMonger on Jan 8, 2009, 4:00 PM)
|