FTP error checking- call e-mail sub

Discussion in 'Perl Misc' started by Nex_s, Aug 4, 2005.

  1. Nex_s

    Nex_s Guest

    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 <>';
    $mail{To} = 'To Me <>';
    $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
     
    Nex_s, Aug 4, 2005
    #1
    1. Advertising

  2. Nex_s

    Dave Weaver Guest

    On 4 Aug 2005 11:09:50 -0700, Nex_s <> wrote:
    >
    > $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 <>';
    > $mail{To} = 'To Me <>';
    > $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 <>',
    To => 'To Me <>',
    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)
     
    Dave Weaver, Aug 8, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    5,747
  2. Ben
    Replies:
    2
    Views:
    938
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,129
    Lawrence D'Oliveiro
    May 20, 2011
  4. D. Buck
    Replies:
    2
    Views:
    566
    D. Buck
    Jun 29, 2004
  5. Nex_s

    FTP error checking- call e-mail sub

    Nex_s, Aug 4, 2005, in forum: Perl Misc
    Replies:
    0
    Views:
    84
    Nex_s
    Aug 4, 2005
Loading...

Share This Page