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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Mail script doesn't work. Need help!

Status
Not open for further replies.

pecan204

Programmer
Jan 25, 2001
24
0
0
US
Script doesn't work or give an error. see below.

Thanks for your comments!

#!/usr/bin/perl
# This script should send an e-mail


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

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

$mailprog = '/var/qmail/bin/qmail-inject';
$recipient = 'pecan204@netscape.com';
open (MAIL, "|$mailprog -t") or &dienice("Can't access $mailprog!\n");
print MAIL "To: $recipient\n";
print MAIL "Reply-to: $FORM{'email'} ($FORM{'name'})\n";
print MAIL "Subject: Form Data\n\n";

# krb print info from form to mail message.
foreach $key (keys(%FORM))
{
print MAIL "$key = $FORM{$key}\n";
}

close(MAIL);

#added to assess problems- krb!!!!!
print &quot;<html><head><title>Form Diagnostics</title></head><body
bgcolor=&quot;#ffffff&quot; text=&quot;#000000&quot; id=all>&quot;;
print &quot;<h2>Diagnostics for send mail form</h2>\n&quot;;
print &quot;Referer = $ENV{'HTTP_REFERER'}\n&quot;;
print &quot;Caller = $ENV{'CONTENT_LENGTH'}\n&quot;;
print &quot;</body></html>&quot;;

# Notify mail has been delivered - krb
print &quot;</body></html>&quot;;
print <<EndHTML;
<h2>Thank You</h2>
Thank you for writing. Your mail has been delivered.<p>
Return to our <a href=&quot; page</a>.
</body></html>
EndHTML


sub dienice
{
($errmsg) = @_;
print &quot;<h2>Error</h2>\n&quot;;
print &quot;$errmsg<p>\n&quot;;
print &quot;</body></html>\n&quot;;
exit;
}
 
there are a couple of points where your script might be messing up, but what errors are you receiving?
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
I'm not getting errors but I am also not getting the mail sent as a test.

What areas do you think are questionable?
 
Just got these error. Any suggestions on what to do?

syntax error at /data/pecan204/cgi-bin/send_mail.cgi line 27, near &quot;$key (&quot;
Execution of /data/pecan204/cgi-bin/send_mail.cgi aborted due to compilation errors.
[Fri Jan 26 18:31:38 2001] [error] [client 24.25.151.112] Premature end of script headers: /data/pecan204/cgi-bin/send_mail.cgi
[Fri Jan 26 18:31:38 2001] [error] [client 24.25.151.112] MOD_VR9: Error 500 occured while processing : /data/pecan204/cgi-bin/send_mail.cgi





 
Well, baring the syntax error for now, i'm not sure you're using your mail program correctly. I don't know the specifics of your program, but does it require a &quot;From:&quot; field in order to send? Does it require a &quot;.&quot; on a line by itself at the end of an email to correctly send a file? The problem doesn't seem to be in the script. i can compile it fine with the -c option, syntax is fine if i leave out all the html stuff (you have a (print &quot;</body></html>&quot;;) where i don't think you want it, around line 40 under the (#notify mail has been sent). it should probly be starting the html and body, because you end it again 6 lines later.) Also, i don't get the syntax error you reported having to do with the &quot;$key (&quot;, and i don't see anything wrong with it.
so, my best suggestion for now is to make sure you're using your mailing program to the letter. maybe when you open it, it needs some options thrown in to get it to read your output properly. try small scripts with small sample emailings to get it to work.
(Also, you could use CGI::Lite to parse all the data automatically instead of by hand, which would clean up your code a bit, but isn't necessary.)
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
I am a beginner myself, however, I have wrote a couple of email scripts for different servers and different platforms and one thing I found that was odd on a server in which I wrote such a script was I had to rearrange the header information to get the email to send. In my case I had to address the the header in the following manner: The subject then the from line then the to line in order for my email to send. Sometimes, we have to go about problems in different ways.

Also I would try this method for opening the mail:
open (MAIL, &quot;|$mailprog -t -f \&quot;$recipient\&quot; &quot;) || &amp;dienice;
 
I copied/pasted your code onto my box and found several simple syntax errors.

The following chunk needs to be in single quotes... The double quotes cause the interpreter to try to evaluate the string and it finds other quotes and # signs in the middle causing compile errors.

#added to assess problems- krb!!!!!
print [red]'[/red]<html><head><title>Form Diagnostics</title></head>
<body bgcolor=&quot;#ffffff&quot; text=&quot;#000000&quot; id=all>[red]'[/red];

#the closing 'EndHTML' has a space on the end. It needs to match the <<EndHTML exactly.

# the HTML you are trying to generate has two close </body></HTML> tags????

The general flow of the code looks reasonable. I think it just needs a few wrinkles ironed out.

'hope this helps....


keep the rudder amid ship and beware the odd typo
 
ALSO, there is a good FAQ on this subject. See the tab at the top of the Perl Forum front page.



keep the rudder amid ship and beware the odd typo
 
goBoating, you can link to a FAQ by copying the code number below its title, like this: faq219-364. TGML automatically makes it a link.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Tom,
I tried that in another thread and it produced a link that came back to the same thread. I thought I was loosing it, but, your faq_link above returns to this thread. Looks like a bug to me...... I'll let the admins know.


keep the rudder amid ship and beware the odd typo
 
Gents,

1.) Sent email to confirm mail prog/path to server staff- no answer yet.

2.) Tried &quot;open (MAIL, &quot;|$mailprog -t -f \&quot;$recipient\&quot; &quot;) || &amp;dienice;&quot; - no clear effect

3.) Cleaned up double html close in script

I'll check the FAQ when it is working later.

This was the latest error log. One note though the $keys error was probably induced by commenting out the print statements and adding a &quot;print &quot; per suggestion to maybe locate an error. The error below is with no commented lines. Does this help?

[Tue Jan 30 19:29:00 2001] [error] [client 24.25.151.112] Premature end of script headers: /data/pecan204/cgi-bin/send_mail.cgi
[Tue Jan 30 19:29:00 2001] [error] [client 24.25.151.112] MOD_VR9: Error 500 occured while processing : /data/pecan204/cgi-bin/send_mail.cgi

Also thanks for looking at it.
 
Just to reduce confusion, this is what it looks like now.

#!/usr/bin/perl
# This script should send an e-mail


print &quot;Content-type:text/plain\n\n&quot;;

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&amp;/, $buffer);

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
$FORM{$name} = $value;
}

$mailprog = '/var/qmail/bin/qmail-inject';
$recipient = 'pecan204@netscape.com';
open (MAIL, &quot;|$mailprog -t -f \&quot;$recipient\&quot; &quot;) || &amp;dienice;
#open (MAIL, &quot;|$mailprog -t&quot;) or &amp;dienice(&quot;Can't access $mailprog!\n&quot;);
print MAIL &quot;To: $recipient\n&quot;;
print MAIL &quot;Reply-to: $FORM{'email'} ($FORM{'name'})\n&quot;;
print MAIL &quot;Subject: Form Data\n\n&quot;;

foreach $key (keys(%FORM))
{
print MAIL &quot;$key = $FORM{$key}\n&quot;;
}

close(MAIL);

print <<EndHTML;
<h2>Thank You</h2>
Thank you for writing. Your mail has been delivered.<p>
Return to our <a href=&quot; page</a>.
</body></html>
EndHTML


sub dienice
{
($errmsg) = @_;
print &quot;<h2>Error</h2>\n&quot;;
print &quot;$errmsg<p>\n&quot;;
print &quot;</body></html>\n&quot;;
exit;
}
 
Good Morning Pecan,
I copied the most recent version of your code, changed $mail_prog to point at my sendmail and ran it from a command prompt. It sent me an email. The mail had no text( I had not run it as a CGI), but it sent the mail. I then changed the recipient to pecan204@netscape.com and ran it again. You should have an empty email with a subject of 'Form Data' waiting for you (unless you already got it).

Conclusion: syntax works.

Some Possible reasons it won't work for you:
1 - you are uploading your script from a PC and forgetting to convert to UNIX line terminators.

2 - you are uploading, converting to UNIX, and forgetting to set the execute bits.

3 - Maybe that is not where you mail executable is??






keep the rudder amid ship and beware the odd typo
 
Hi goboating,

Did not recieve any e-mail with ..form.

Aren't Unix line terminators just a semi colan.

Protections are 755

 
The latest error log.

[Wed Jan 31 19:26:13 2001] [error] [client 24.25.151.112] Premature end of script headers: /data/pecan204/cgi-bin/send_mail.cgi
[Wed Jan 31 19:26:13 2001] [error] [client 24.25.151.112] MOD_VR9: Error 500 occured while processing : /data/pecan204/cgi-bin/send_mail.cgi

I also noted a operation timed out. Maybe it cannot get to the mail program.

I think I ill try to setup on another server.
 
Hey pecan,
' just curious ..... is that pea'-can or pec-kaun'?

I don't know why you did not receive the email. I may have typo'd your address in the script when I ran it.

The 'Premature end of script headers: /data/pecan204/cgi-bin/send_mail.cgi' error usually means your code did not run at all. It probably blew up and thus failed to create the appropriate HTML header stuff.

About the UNIX line endings. DOS files have both &quot;\n&quot; and &quot;\r&quot;. Unix files have only a &quot;\n&quot;. If you are writing your code on a PC (Windows) box and are then uploading (ftp) the code to a unix box, you need to convert the file to the unix format or it will not run.

If that is the case, that may be your first problem. Your ftp software may allow you to do an on-the-fly conversion when you do the upload. If not, get another ftp client, WSFTP is available on the net, ....it works well and has that option.

'hope this helps.


keep the rudder amid ship and beware the odd typo
 
Hi goBoating,

Well I started in pea'-can land then I moved to pec-kaun land then I moved back to pea'-can land and now would like to move back to pec-kaun land, but I got this job with stk options that I don't know were it is going.

How about you? Is your anchor in warm waters or are you snowed over and in storage?

I edited everything for unix. I am setting up now at virtualave.net. Looks like a nice service.

Will check in when I give that a try.

Thanks for the help!
 
Wanted to update all.

I setup this page on virtualave and they happen to have two different mail programs.
One qmail-inject also, and sendmail, is their newer program.

Script worked for both, so I would conclude that the vr9 program wasn't working correctly.

Thanks for all the good help!

 
warm water.... always warm waters.... South Carolina


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top