Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wherefore to bail when trying to MAIL? 4

Status
Not open for further replies.

EdwardMartinIII

Technical User
Sep 17, 2002
1,655
US
Good morning!

I've read through a couple of different strings on a similar problem, but haven't nailed down exactly where my script is having trouble.

An excerpt from the HTML page that calls it:
Code:
    <form method="post" action="[URL unfurl="true"]http://www.petting-zoo.org/cgi/GPMailer.cgi">[/URL]
      <table border="1">
        <tr>
          <td colspan="3">Name: <input type="text" size="75" name="Name" /></td>

An excerpt from the CGI file:
Code:
#!/usr/local/bin/perl -w

use CGI;
$q = new CGI;

$sendmailpath = '/usr/lib/sendmail';
$recipient = "edward\@petting-zoo.org";
$name = $q->param("Name");
$email = $q->param("Email");

open(MAIL, "|$sendmailpath-t $recipient") || die "Can't open $sendmailpath.\n";
print MAIL "Reply-to: $email\n";
print MAIL "From: $email\n";
print MAIL "Subject: Webmail\n\n";
print MAIL "\n\n";
print MAIL "Test: $name";
close (MAIL);


print ("Content-Type: text/html\n\n");
print ("<!DOCTYPE html\n");
print ("     PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n");
print ("     \"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");[/URL]
print ("\n");
print ("<html xmlns=\"[URL unfurl="true"]http://www.w3.org/1999/xhtml\"[/URL] xml:lang=\"en\" lang=\"en\">\n");
print ("<head>\n");
print ("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n");
print ("  <title>Thank you!</title>\n");
print ("  <link rel=\"stylesheet\" href=\"../Style2003.css\" type=\"text/css\" />\n");
print ("</head>\n");
print ("<body>\n");
print ("  <h1>Thank you!</h1>\n");
...

Now, the variables seem to be properly assigned values and the HTML part of the show works just dandy (including when I print up varaibales). When I comment out all but the OPEN line, I get my web page, but an error about insufficient paramaters (Sorry, I can't replicate it from this location). When I try everytghing from the OPEN command to the CLOSE command, I receive an error 500 (no big surprise, huh?)

I know my path to perl is good and I'm pretty sure my path to sendmail is good because I copied it from the earthlink hosting help (but if there's a way to verify this in the code, I'd appreciate advice).

My task is simple -- I just want this HTML form to send me the data in a piece of e-mail.

Is there something obvious I'm missing in my script or the HTML? I can post more if it'l help, but these seemed to be the relevent pieces.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Why are you using sendmail directly? Move to Net::SMTP and your problems will go away. Sendmail implementations change from system to system and it sounds like your sending it bogus data.

When chosing between installing Net::SMTP or debugging sendmail on a foreign system I chose Net::SMTP.

The real answer here is a typo on your part
Code:
$sendmailpath-t $recipient

You need a space between sendmail and -t
Code:
$sendmailpath -t $recipient

Sendmail is evil, move to Net::SMTP if you want portable and solid code.
 
When I remove the "-t", it didn't work either. I'm also pretty sure my initial attempts had the space between the call and the parameter. (I must've dutched it when I posted it, but i'll doublechck to make sure)

I use sendmail because I know Earthlink has it and -- in theory -- this is the correct path. I have not run across too many claims that it is "evil". My Perl book describes the use of sendmail in examples, so I tend to want to use that. In the past, sendmail's done well for me on this server, but I've rewritten the site lately and am trying to get this piece of CGI to work.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Claims or not, its evil. Look, its breaking isnt it? The books are not always correct. If you search the archives here you will see a LOT of sendmail related issues, its just a needless layer of OS dependancy. What happens is the sendmail changes its args between OS and versions. So if earthlink changes anything your code loses portability.

Regardless, its not about removing the -t, you have to have that for sendmail. Its the fact that you need a space between your sendmail call and the -t. If that doesn't work log in and run sendmail by hand to see what arguments are required for the earthlink version. Sendmail is not constant across versions and platforms.

If you moved to Net::SMTP this problem would go away and Net::SMTP is most likely installed at Earthlink. For an extra 8 lines of code your problems go away, you get platform neutrality, you get maintainable and portable code and you get to interact directly with the SMTP stream at a protocol level. All for free. Wow! :)

John-
 
Log in and run sendmail by hand"? You mean telnet in? Are there still ISPs that allow that? Earthlink does not allow telnet access.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Hm, adding the space didn't sem to matter much. At least this time I don't get an error 500.

I currently have
Code:
open(MAIL, "|$sendmailpath -t $recipient") || die "Can't open $sendmailpath.\n";
print MAIL "Reply-to: $email\n";
print MAIL "From: $email\n";
print MAIL "Subject: Webmail\n\n";
print MAIL "\n\n";
print MAIL "Test: $name";
close (MAIL);

When I hit the form, it seems to run the CGI, and display the HTML that apears after the e-mail sequence. So, at least I'm not error 500 before that, but I'm not receiving e-mail and can't figure out why.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Edward...

Take the parens out your MAIL script:

open MAIL, "|$sendmailpath-t $recipient" || die "Can't open $sendmailpath.\n";
print MAIL "Reply-to: $email\n";
print MAIL "From: $email\n";
print MAIL "Subject: Webmail\n\n";
print MAIL "\n\n";
print MAIL "Test: $name";
close MAIL;


There's always a better way. The fun is trying to find it!
 
I dont think that will make a difference tviman, its syntatically the same I think. Maybe? I dunno, might be some subtly in the script that I don't recognize. I rarely code direct to the OS like that anymore. it doesn't scale across installations well.

The core issue here I think is that earthlink's sendmail probably wants different arguments then he is providing or is requiring some sort of authentication potentially..



 
I see what you're saying but I also use Earthlink (with an identical script) and the ONLY difference I can see (other than the parens) is the $recipient variable in the open MAIL statement. I've never used this before and frankly, I don't see the need for it. Maybe if Edward pulled that from his script something might change?

Ahhh.. one more thing - but I doubt if it will make a difference... I place this: print ("Content-Type: text/html\n\n"); before I call MAIL rather than after - no particular reason other than it's the way I've always done it.

There's always a better way. The fun is trying to find it!
 
Yea, headers first is important, at least then when they is an error you maybe get it printed to browser!

I'll be interested in how this one shakes out..
 
Okay.

I took out the parenthesis, hardcoded the recipient address, and shifted the whole EMAIL collection of directives to below the HTML. The HTML includes the line:
Code:
print ("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n");

...which I think ought to cover it.

The thing appears to run fine, the response web page is presented, but no e-mail is actually produced, nor is there any apparent error displayed on the resultant web page.

The sample HTML is
and the CGI is
(although I'm not sure if you can see that)

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
I'd run the sendmail command in a script as a backtick and see what is returned

so

Code:
#!/usr/local/bin/perl -w

use CGI;
$q = new CGI;
print $q->header;

$sendmailpath = '/usr/lib/sendmail';
$recipient = "edward\@petting-zoo.org";
print "Executing sendmail<HR>";
print `$sendmailpath -t $recipient`;
print "<HR>Executing complete<BR>";

That will print to browser what sendmail is outputting. Maybe it will tell you what is wrong.

This is where Net::SMTP comes in handy, you could have saved a few hours of time ;)

 
It reads
Executing sendmail
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
Executing complete


No e-mail arrives. 8(

I sent by-hand a test e-mail after I executed this, and the test e-mail arived, but the CGI-product didn't.

tviman, how do you view your error logs. I can see mine via Earthlinks Console, but I can't view 'em. I was thinking there might be something useful in there -- if I can just get it.

Thanks, guys!

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Edward, just so we're all on the same page, what variable are you using for your email address - $email or $recipient?

There's always a better way. The fun is trying to find it!
 
$recipient, although I've stripped it out of the CGI and replaced it with my actual e-mail address to avoid exactly that sort of confusion. $email is the email address as carried from the form.

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Well buddy, if that's the case then you're going to be kicking yourself... Here's your code:

Code:
open(MAIL, "|$sendmailpath-t [COLOR=red][b]$recipient[/b][/color]") || die "Can't open $sendmailpath.\n";
print MAIL "Reply-to: [COLOR=red][b]$email[/b][/color]\n";
print MAIL "From: $email\n";
print MAIL "Subject: Webmail\n\n";
print MAIL "\n\n";
print MAIL "Test: $name";
close (MAIL);

Notice the placement of $email and $recipient. $email is where your email is being sent. As a test, swap $email for $recipient and I'll bet that you'll get an email. (Or fill out your form using YOUR email address.)

As far as viewing the logs, I FTP into the site and download the logs to my machine.

There's always a better way. The fun is trying to find it!
 
Okay...

I did this:
Code:
open MAIL, "|$sendmailpath -t edward\@petting-zoo.org" || die "Can't open $sendmailpath.\n";
print MAIL "Reply-to: edward\@petting-zoo.org\n";
print MAIL "From: edward\@petting-zoo.org\n";
print MAIL "Subject: Webmail\n\n";
print MAIL "\n\n";
print MAIL "Test: $name";
close MAIL;

Which pretty much eliminates any possibility I'm sending it to the wrong fella.

No Server 500 errors, web page appears fine and no e-mail seems to be delivered to "any" of the addresses.

Just in case, here's the full CGI:

Code:
#!/usr/local/bin/perl -w

use CGI;
$q = new CGI;
print $q->header;

$sendmailpath = '/usr/lib/sendmail';
$name = $q->param("Name");
$email = $q->param("Email");

print ("<!DOCTYPE html\n");
print ("     PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n");
print ("     \"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");[/URL]
print ("\n");
print ("<html xmlns=\"[URL unfurl="true"]http://www.w3.org/1999/xhtml\"[/URL] xml:lang=\"en\" lang=\"en\">\n");
print ("<head>\n");
print ("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n");
print ("  <title>Thank you!</title>\n");
print ("  <link rel=\"stylesheet\" href=\"../Style2003.css\" type=\"text/css\" />\n");
print ("</head>\n");
print ("<body>\n");
print ("  <h1>Thank you!</h1>\n");
print ("\n");
print ("  <div class=\"MenuItems\">\n");
print ("    <ul class=\"MenuList\">\n");
print ("      <li class=\"MenuLevel1\"><a href=\"../index.html\"><b>Home</b></a></li>\n");
print ("      <li class=\"MenuLevel1\"><a href=\"../Population.html\">Population</a></li>\n");
print ("      <li class=\"MenuLevel2\"><a href=\"../Dwight.html\">Dwight</a></li>\n");
print ("      <li class=\"MenuLevel2\"><a href=\"../Edward.html\">Edward</a></li>\n");
print ("      <li class=\"MenuLevel3\"><a href=\"../PubsMain.html\">Published Work</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../Comics.html\">Comics</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../Cartoons.html\">Cartoons</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../Humor.html\">Humor</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../Prose.html\">Fiction Prose</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../TWriting.html\">Technical Writing</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../Illustration.html\">Illustration</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../Movies.html\">Movies</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../NonFiction.html\">Non-Fiction Prose</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../Promo.html\">Promotional</a></li>\n");
print ("      <li class=\"MenuLevel4\"><a href=\"../Webwork.html\">Webwork</a></li>\n");
print ("      <li class=\"MenuLevel3\"><a href=\"../Programming.html\">Programming</a></li>\n");
print ("      <li class=\"MenuLevel3\"><a href=\"../Robotics.html\">Robotics</a></li>\n");
print ("      <li class=\"MenuLevel3\"><a href=\"../Reviews.html\">Book Reviews</a></li>\n");
print ("      <li class=\"MenuLevel3\"><a href=\"../Weirdness.html\">Weird Stuff</a></li>\n");
print ("      <li class=\"MenuLevel2\"><a href=\"../Erin.html\">Erin</a></li>\n");
print ("      <li class=\"MenuLevel2\"><a href=\"../Griffin.html\">Griffin</a></li>\n");
print ("      <li class=\"MenuLevel2\"><a href=\"../Katrina.html\">Katrina</a></li>\n");
print ("      <li class=\"MenuLevel2\"><a href=\"../Nicole.html\">Nicole</a></li>\n");
print ("      <li class=\"MenuLevel2\"><a href=\"../Cats.html\">The Beasts</a></li>\n");
print ("      <li class=\"MenuLevel1\"><a href=\"../Activities.html\">Activities</a></li>\n");
print ("      <li class=\"MenuLevel1\"><a href=\"../Links.html\">Links</a></li>\n");
print ("      <li class=\"MenuLevel1\">------------------------</li>\n");
print ("      <li class=\"MenuLevel1\"><em><a href=\"mailto:edward\@petting-zoo.org\">Write us!</a></em></li>\n");
print ("    </ul>\n");
print ("  </div>\n");
print ("\n");
print ("  <div class=\"BodyText\">\n");
print ("    <p>Thank you for sending your information.</p>\n");
print ("    <p>I will write back an acknowledgement once I get this into my database.  If you don't hear back from me in a couple days, drop me an e-mail, because I'm either out of town or my CGI is whacked.</p>\n");
print ("    <p class=\"CenteredContent\">[Return to my <a href=\"../Movies.html\">Movies page</a> ]</p>\n");
print ("  </div>\n");
print ("\n");
print ("  <div class=\"FooterText\">\n");
print ("    <p class=\"CenteredContent\"><a href=\"[URL unfurl="true"]http://validator.w3.org/check/referer\"[/URL] target=\"new\"><img src=\"../UtilityImages/valid-xhtml10.gif\" alt=\"Valid XHTML 1.0!\" height=\"31\" width=\"88\" border=\"0\" /></a> <a href=\"[URL unfurl="true"]http://jigsaw.w3.org/css-validator/\"><img[/URL] src=\"../UtilityImages/ValidCSS.gif\" height=\"31\" width=\"88\" alt=\"Valid CSS!\" /></a></p>\n");
print ("  </div>\n");
print ("</body>\n");
print ("</html>\n");

open MAIL, "|$sendmailpath -t edward\@petting-zoo.org" || die "Can't open $sendmailpath.\n";
print MAIL "Reply-to: edward\@petting-zoo.org\n";
print MAIL "From: edward\@petting-zoo.org\n";
print MAIL "Subject: Webmail\n\n";
print MAIL "\n\n";
print MAIL "Test: $name";
close MAIL;

[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Edward, my friend, you've got me stumped! Let's try this just to see what we're getting from the form. Make a test script as follows:

Code:
#!/usr/local/bin/perl -w

use CGI;
$q = new CGI;
print $q->header;

$name = $q->param("Name");
$email = $q->param("Email");

print "Content-type: text/html\n\n";

print <<EOF;
<html>
<head>
<title></title>
</head>

<body>
Name = $name<br>
Email = $email
</body>
<html>
EOF

This will tell us what we're getting from the form. If all is as it should be, then change the SENDMAIL script by removing your email address from the open MAIL statement:

Code:
open MAIL, "|$sendmailpath -t -i" || die "Couldn't open Mail Program: $!";

There's always a better way. The fun is trying to find it!
 
Until you get sendmail working, I'd definitely look into using Net::SMTP as siberian suggested. I had a problem on my server getting sendmail to work reliably... I could send a message and in the logs it looked like everything was fine but I'd never get the message. If I remember correctly it would actually work when sending to some e-mail addresses but not all or not every time. Anyway, it was very strange and I never got it resolved so I used Net::SMTP, which worked perfectly.

 
Note that || has a higher precedence than , so you should either reinsert the ( ) or use 'or' instead of '||'
Code:
open MAIL, "|$sendmailpath-t $recipient"  || die "Can't open $sendmailpath.\n"; #bad, will never die
open(MAIL, "|$sendmailpath-t $recipient") || die "Can't open $sendmailpath.\n"; #good!
open MAIL, "|$sendmailpath-t $recipient"  or die "Can't open $sendmailpath.\n"; #my preferred method
That should at least tell you if sendmail died for some reason. But I'm going to have to jump on the modules-are-better-than-system-shell-calls-net-smtp bandwagon. Be sure to do some taint checking, especially when passing things to outside programs. Hate to become a spammer portal.

________________________________________
Andrew - Perl Monkey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top