Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Checking mail command for success

by ronix (Novice)
on Apr 21, 2009 at 20:08 UTC ( [id://759099]=perlquestion: print w/replies, xml ) Need Help??

ronix has asked for the wisdom of the Perl Monks concerning the following question:

Venerable ones, I searched and could not find any obvious information on this. I am using the mail command in a script on AIX (I can't use any of the the slick mail modules for Perl, as it would require updating thousands of production servers). Simply put: I want to continue if the mail command is successful and exit out if it fails.
# subroutine for mailing/transferring the changes sub mail_changes { my $mailer = "|/bin/mail -s $hostn $recipients < $disk_changes"; open (MAIL, $mailer) || die "Cannot execute mail command; $!\n"; close(MAIL); if ($? > 0) { print "Do some error stuff if needed\n"; } }
Any suggestions for a better way of checking for success other then the old  $? ? thanks for your time.

Replies are listed 'Best First'.
Re: Checking mail command for success
by ikegami (Patriarch) on Apr 21, 2009 at 20:19 UTC
    What's the problem? Is $? not suitable for some reason?

    Note that you're subject to attacks by passing unescaped text to the shell.

      Note that you're subject to attacks by passing unescaped text to the shell.

      Sorry, but could you explain this in more detail?

      thanks!

        "John Doe <john@example.org>" (minus the quotes) is a valid email address, but you'll have problems if $recipients contains that.

        And of course, there's the malicious who might find a way of getting "| rm -rf /" (minus the quotes) into $host or $recipients.

        The multiple argument form of various commands gets around that problem.

        use IPC::Open3 qw( ); open(my $mailer_fh, '|-', '/bin/mail', '-s', $hostn, $recipients) or die("Cannot execute mail command: $!\n"); open(my $msg_fh, '<', $disk_changes) or die("Cannot execute message file: $!\n"); print $mailer_fh $_ while <$msg_fh>; close($mailer_fh) or die("Error sending mail message: $!\n"); $? == 0 or die("Error sending mail message: $?\n");
Re: Checking mail command for success
by mr_mischief (Monsignor) on Apr 22, 2009 at 02:48 UTC
    How are you deploying the changes to your program wrapping the mail command? Perhaps you're using ftp, sftp, scp, rsync, or something similar? Chances are any of the pure-Perl mail sending modules could be deployed the same way.

    Some of those mail sending modules even just wrap a Sendmail mail submission executable (or the mock sendmail executables offered for compatibility by any of Postfix, Exim, qmail, SMail, or a long list of others) usually found at /usr/sbin/sendmail, /usr/bin/sendmail, or (for hysterical porpoises) /usr/lib/sendmail. The trick is, they wrap it properly so you don't have to. They let you just call a simple sub from your Perl program so you don't have to sweat the details.

    If you really want to wrap an external program, consider a module that does so. If you can't stand the thought of a module, consider grabbing the code from one and crediting the author. That would let you just update the one file on those servers instead of two.

    I generally use my own interface module around Net::SMTP::TLS and have hacked support for that module into NMS formmail, too. However, you might prefer Mail::Mailer, Mail::Sendmail, MIME::Lite, or Mail::DWIM (which is one of those that will wrap around a Sendmail mail submission executable).

    Update: fixed a typo in PM link tag for Mail::DWIM

Re: Checking mail command for success
by kwaping (Priest) on Apr 21, 2009 at 23:06 UTC
    Have you tried it using system? That's too bad that you can't use any of the nice CPAN mail modules.

    ---
    It's all fine and dandy until someone has to look at the code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://759099]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found