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

im having a problem with a script that emails a generated password to people. i think the problem lies with the sendmail prog, because i dont see a problem with my code, but its possibly one of those things that someone else would catch real qiuck...any problems with this snippet?
open(MAIL,"|/usr/slib/sendmail -t"); print MAIL "To: $rEmail\n"; print MAIL "From: $respondAddr\n"; print MAIL "Subject: Registration confirmation\n\n"; print MAIL "Your username is '$rUser' and your randomly generated pass +word is '$rPass'.\n"; print MAIL "After you log in, you can change your password.\n\n"; print MAIL "To complete the registration process and log in, go to the + following URL:\n"; print MAIL "$BaseUrl"."index.cgi?action=signup&phase=three&ruser=$rUse +r"; print MAIL "."; close(MAIL);

Replies are listed 'Best First'.
Re: sendmail problem...
by mattriff (Chaplain) on Feb 02, 2003 at 19:53 UTC
    Are you sure the open() call to sendmail is succeeding? That would be the first place to start, like:
    open(MAIL,"|/usr/slib/sendmail -t") or die; # or other error routine

    I'm assuming that the problem is that no mail is sent?

    - Matt Riffle

      yeah, i tried that already, i actually copied the code from my local copy of the script, not the one on the server. i wanrted to make sure though that it isnt a syntax error that 'perl -c' wouldnt catch.
Re: sendmail problem...
by fokat (Deacon) on Feb 02, 2003 at 23:11 UTC

    Additional things to check:

    • Make sure you have autoflush turned on in the filehandle. You can use something like (adapted from The Perl Cookbook

      my $old_fh = select(MAIL); $| = 1; select($old_fh);
    • The last print (the dot) should be:

      print MAIL ".\n";
    • According to the RFCs, line endings should be "\r\n" and not "\n" although this is usually ok.

    In any case, please do consider using Net::SMTP, which allows your script to send the email in a more robust fashion.

    Best regards

    -lem, but some call me fokat

Re: sendmail problem...
by tradez (Pilgrim) on Feb 02, 2003 at 20:28 UTC
    Net::SMTP
    Have you tried this module? I have used it many times for email and I have always had good luck with it. Something I have also noticed while playing with email from a script, is moving around the order of the Header portions, i.e. From:, To:, then Subject: and so on. I can't really explain why, but I have had a lot of luck with doing this whenever I run acrossed issues of a script not sending the correct email, or no email at all.

    Tradez
    "Never underestimate the predicability of stupidity"
    - Bullet Tooth Tony, Snatch (2001)
Re: sendmail problem...
by jobber (Sexton) on Feb 02, 2003 at 22:27 UTC
    Hi,
    The problem could be one of many things,
    1. Run the code with the -w option to make sure the /usr/slib/sendmail actually exists and that you can execute.
    2. Also check to make sure you can relay outside of your network. Try sending the mail to an account on the same machine as yourself. If that works, then send it outside your network and see if it reaches there. If not, your mailserver may not allow relaying outside.