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

I've been trying to get various perl mailing scripts running. They all pretend to run and everything displays correctly, but I don't actually receive e-mail sent to myself. I've checked with my webhost to verify the perl and sendmail paths are correct, and of course my e-mail is correct. My website uses Apache and Linux. I've stripped the sendmail code to:
#!/usr/bin/perl $from="don\@canzona.com"; $to="don\@canzona.com"; $subject="I\'m sending myself a test e-mail!"; $sendmailpath="/usr/sbin/sendmail"; if (open (SENDMAIL, "| $sendmailpath -t")) { print SENDMAIL "Subject: $subject\n"; print SENDMAIL "From: $from\n"; print SENDMAIL "To: $to\n"; print SENDMAIL "This is a test e-mail.\n\n"; close (SENDMAIL); print "Content-type: text/html \n\n"; print "Subject: $subject\n"; print "<BR>"; print "From: $from\n"; print "<BR>"; print "To: $to\n"; print "<BR>"; print "This is a test e-mail.\n\n"; print "<BR>"; print "e-mail sent.\n"; } exit;
Everything displays correctly, but I don't receive the e-mail. Can someone either tell me what stupid mistake I've made, or suggest a way to check what sendmail is actually doing?

Replies are listed 'Best First'.
Re: seeking sendmail enlightenment
by mpeppler (Vicar) on Jun 20, 2002 at 18:03 UTC
    You're missing a newline between the "To:" line and the body:
    if (open (SENDMAIL, "| $sendmailpath -t")) { print SENDMAIL "Subject: $subject\n"; print SENDMAIL "From: $from\n"; print SENDMAIL "To: $to\n"; print SENDMAIL "\n" ### Need a blank line between headers and +body! print SENDMAIL "This is a test e-mail.\n\n"; close (SENDMAIL);

    In addition - use strict, even though it's not the issue here.

    Michael

Re: seeking sendmail enlightenment
by stajich (Chaplain) on Jun 20, 2002 at 16:56 UTC
    a) Have you tried Mail::Mailer

    b) So you are actually running this in a cgi script - you may check your apache error log, perhaps your httpd uid doesn't have permission to execute the sendmail exe?

    c) have you tried your code in a simple perl script without worrying about the webserver part? Does that work?

Re: seeking sendmail enlightenment
by mirod (Canon) on Jun 20, 2002 at 16:57 UTC

    You could add code to check whether the openand the close succeed (when you output to a pipe the open might succeed but the close will fail if the process down the pipe fails):

    open (SENDMAIL, "| $sendmailpath -t") or die "error opening SENDMAIL (senmailpath is $sendmailpath): $!"; ... close (SENDMAIL) or die "error when closing SENDMAIL: $!";

    You can then look at the httpd logs, or use use CGI::Carp qw(fatalsToBrowser); to output the errors directly to the browser (just don't leave this in the real live code!).

Re: seeking sendmail enlightenment
by d4vis (Chaplain) on Jun 20, 2002 at 16:59 UTC
    Should your path to sendmail be /usr/lib/sendmail rather than sbin?
    A -w switch on the shebang will tell you for sure.
    (#!/usr/bin/perl -w)

    ~monk d4vis
    #!/usr/bin/fnord

Re: seeking sendmail enlightenment
by RMGir (Prior) on Jun 20, 2002 at 17:04 UTC
    I don't know what the problem is with sendmail in your case, but invoking it that way has been flaky for me.

    A good alternative to/wrapper around Sendmail is MIME::Lite. It will send via sendmail or via smtp. A very nice module to use...
    --
    Mike

Re: seeking sendmail enlightenment
by Anonymous Monk on Jun 20, 2002 at 21:23 UTC
    I did't see end the email (with a period on it's own line).
    open (MAIL, "| /usr/sbin/sendmail -t"); print MAIL <<EndMail; To: user\@host.com From: Mail Report Subject: $subj EndMail print MAIL `This is a test.\n`; print MAIL "\n.\n"; # A single period on its own line. close MAIL;
      I just finished debugging this problem on a server running 5.8.0. The thing is, I never had to do this under 5.6.1 or 5.003. I know it's always been a command line requirement, but does anyone know if this is a new requirement in 5.8, or is this a bug?

      Spacewarp

      DISCLAIMER:
      Use of this advanced computing technology does not imply an endorsement
      of Western industrial civilization.
Re: seeking sendmail enlightenment
by ajdelore (Pilgrim) on Jun 20, 2002 at 18:55 UTC

    As far as I can tell, you're not opening the pipe correctly. You seem to have a space between the pipe character and the path. I think it should be:

    open SENDMAIL, "|$sendmailpath -t"

    Personally, I always found mucking around with sendmail with perl to be a real pain in the butt, due to all the various security issues and lockdowns that tend to come up. I tend to just use the good old unix 'mail' program instead.

    I realize that it isn't as secure as sendmail, but in my case I'm usually just e-mailing things to myself from scripts that are running as cron jobs. You'd have to watch yourself with CGI but I think with proper taint checking you'd be alright... (anyone comment on this?)

    </ajdelore>

Re: seeking sendmail enlightenment
by blahblahblah (Priest) on Oct 02, 2002 at 04:27 UTC
    Lots of good suggestions above. One other thing that's useful to know for debugging sendmail is the "-v" flag, which turns on "verbose" output. Try changing this line in your code:
    if (open (SENDMAIL, "| $sendmailpath -t"))
    to this:
    if (open (SENDMAIL, "| $sendmailpath -t -v > /tmp/maildebug.txt"))
    Then look at the /tmp/maildebug.txt after you run your script and you'll see the output of running the command.

    Or you could just use something like the following, if you want to just see the output as part of the page (but make sure you print the Content-type header first):

    if (open (SENDMAIL, "| $sendmailpath -t -v"))