FTP error checking- call e-mail sub

N

Nex_s

Hi All,

I'm doing my best to get through this one. I want to error check on
each part of the FTP process. If it fails I want to call an e-mail
sub. Unfortunately the FTP process prints an error and as such my
script fails. For some reason I also can't pass the appropriate error
message to the e-mail sub neither. Here's my code.

$errmessage = "file does not exist";
$ftp->put("temp_buffer1.txt")
or die &email($errmessage);

sub email ($errmessage) {

use Mail::Sendmail;
my %mail;
$mail{From} = 'From Me <[email protected]>';
$mail{To} = 'To Me <[email protected]>';
$server = '123.456.789.10';

$mail{Smtp} = $server;
$mail{Subject} = 'Process failure!!';
$mail{Message} = $errmessage;
$mail{Message} = "It was sent on " . Mail::Sendmail::time_to_date() .
"\n";

# Go send it
sendmail %mail

}

Any help would be appreciated.

Chris
 
D

Dave Weaver

$errmessage = "file does not exist";
$ftp->put("temp_buffer1.txt")
or die &email($errmessage);

Why do you think the only possible failure is the lack of existence of
the file you want to put? You might as well use the proper failure
message that Net::FTP has. This is rougly how I'd do it (untested) :

$ftp->put( $filename )
or fatal( "Couldn't put '$filename' : " . $ftp->message )

sub fatal {
my ( $msg ) = @_;
email( $msg );
exit 1; # use "die $msg" instead, if you want the msg to STDERR too.
}

sub email ($errmessage) {

This has been addressed in another reply.
use Mail::Sendmail;
my %mail;
$mail{From} = 'From Me <[email protected]>';
$mail{To} = 'To Me <[email protected]>';
$server = '123.456.789.10';

$mail{Smtp} = $server;
$mail{Subject} = 'Process failure!!';
$mail{Message} = $errmessage;
$mail{Message} = "It was sent on " . Mail::Sendmail::time_to_date() .
"\n";

Written slightly nicer (IMHO) like this:

sub email {
my ( $errormsg ) = @_;

use Mail::Sendmail;
my %mail = (
From => 'From Me <[email protected]>',
To => 'To Me <[email protected]>',
Smtp => '123.456.789.10',
Subject => 'Process failure!!',
Message => $errormsg
. "\nIt was sent on "
. Mail::Sendmail::time_to_date()
."\n",
);

sendmail %mail or die $Mail::Sendmail::error
}

(untested)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top