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

sendmail close error

by vit (Friar)
on Jul 20, 2011 at 18:22 UTC ( [id://915704]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,
Below is the code which is working fine on a number of my web applications on the same server.
But on a new application it logs "Error closing mail" and does not send an email. I do not see any difference with my other working applications and do not see how to find a bug. I checked I am passing all parameters correctly.
So any thoughts what I should check?
sub EmailBySendmail { my $to_ptr = shift; my $from = shift; my $subject = shift; my $message = shift; foreach my $to_ad (@$to_ptr) { unless(open (MAIL, "|/usr/sbin/sendmail -t")) { print "error.\n"; warn "Error starting sendmail: $!"; } else { print MAIL "From: $from\n"; print MAIL "To: $to_ad\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$message"; close(MAIL) || warn "Error closing mail: $!"; print "Mail sent.\n"; } } return; }

Replies are listed 'Best First'.
Re: sendmail close error
by mr_mischief (Monsignor) on Jul 20, 2011 at 21:50 UTC

    You're probably better off not reinventing this particular wheel. Net::SMTP and Net::SMTP::TLS have always served me well. Others will recommend Mail::Sender or MIME::Lite. There's plenty to recommend any of those. In fact, if you're wanting a form mailer, do a search for 'nms formmail'.

    If you're still wanting to know what else to check in this particular case, a good place to start would be your mail server logs. If you don't have access to them your hosting provider will.

Re: sendmail close error
by onelesd (Pilgrim) on Jul 20, 2011 at 18:28 UTC
    Have you checked sendmail and perl versions, and are you using:
    use strict; use warnings;
Re: sendmail close error
by Marshall (Canon) on Jul 20, 2011 at 18:30 UTC
    What happens if you open MAIL for writing?
    # now: open (MAIL, "|/usr/sbin/sendmail -t") open (MAIL, '>', "|/usr/sbin/sendmail -t")
    Udate: didn't notice the | at small font size at my end. The 2 arg version will work as written. the 3 arg version would be: open (MAIL, '|-', "/usr/sbin/sendmail -t")

    Weird, the open() works, but the close() fails. Does the print() work? Nobody ever checks this, but it does return a status like open or close.

      I am not sure I understand what you suggest but
      print "Mail sent.\n";
      is working
        What we have is the sequence
        1. open MAIL pass
        2. print MAIL ????
        3. close MAIL fail
        step (1) succeeds, step (3) fails. The natural question is did step (2) fail or succeed? I've never seen close on a pipe fail, but it does in this case. I've never seen a print to an open pipe fail either. But, since we are into rare, weird stuff, just curious about the print to the MAIL pipe. print() returns status just like open() or close(). I admit to being stumped. This might tell us something surprising or not - but its easy to do.

        print MAIL ...... or warn "print to MAIL failed".

        Other way to code:

        #!/usr/bin/perl -w use strict; my $status = print "this is a test\n"; print "first print returned $status\n"; __END__ output: this is a test first print returned 1
Re: sendmail close error
by stonecolddevin (Parson) on Jul 21, 2011 at 19:48 UTC

    Don't pipe directly to sendmail. It's insecure and sloppy. Like mr_mischief said, use a module.

    Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found