Click to See Complete Forum and Search --> : Trouble with sendmail script
I have been trying to write a simple cgi program that gets called whenever the user fills out the contact form. The program is supposed to take the values from the form and send them in an e-mail to the admin's e-mail address.
Here is the code:
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
my $name = "$q->param('name')";
my $email = "$q->param('email')";
my $comments = "$q->param('comments')";
open (MAIL, "| /usr/sbin/sendmail -t -i")
or die "Cannot open sendmail: $!";
print MAIL "To: <admin's address removed>";
print MAIL "Reply-To:<XXXXXx>";
print MAIL "Subject: Contact Form";
print MAIL "Content-type: text/plain \n\n";
print MAIL "Name -- $name \n";
print MAIL "email address -- $email";
print MAIL "Comments \n $comments";
close(MAIL);
I have searched around the internet trying to find a solution but couldn't solve it on my own.
Here is what I have done
Set permissions to 755
Confirmed location of Perl
Confirmed location of Sendmail
Here is what the logfile says:
Premature end of script headers: mailform.cgi
-Thanks-
fireartist
12-20-2005, 08:47 AM
Is the email being sent?
You'll certainly get an error page in the browser, though, because you're not sending a new page to the browser.
Either:
send a HTTP header and HTML for whatever page you want the person to see,
print $q->header();
print $html;
or send a redirection header to point to another page on your site - such as a 'thankyou' page.
print $q->redirect( 'http://mysite.com/thanks.htm' );
Finally, the quotes around the param() calls are wrong - get rid of them.
my $name = "$q->param('name')";
should be...
my $name = $q->param('name');
Thanks for the response!
I made the changes and nowthe browser says:
Status: 302 Moved Location: gravemind.<XXXXXX>.com/static-thanks.html
And I never get an email
The error-log still says pre-mature end of script header
Ok I fixed a little syntax error in the script.
Now I get a "HTTP 404 - File not found" error
But the logfile now has this in it
/home/virtual/site227/fst/var/subdomain/gravemind/html/cgi-bin/gravemind.<XXXXXXX>.com
It seems to try to link to the redirect to the location as if it were a folder inside my cgi-bin
fireartist
12-20-2005, 09:23 AM
If the browser is actually displaying that line - that suggests an earlier header is being sent out.
Did you only add the print redirect() line?
You didn't add both a print header() and a print redirect() did you?
And is that error log message definitely for the latest hit on the script - you can check by the time in the log - because I don't understand why it'd be saying that as the browser is definitely receiving a header of some sort.
There's definitely nothing else in the script that you haven't shown?
There may be an error with the mail sending code - I can't be sure as it's so long since I did it manually like that. I generally use MIME::Lite (http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm) or any other module from CPAN.
fireartist
12-20-2005, 09:25 AM
Read your latest post - can you copy-paste your entire program?
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
my $name = $q->param('name');
my $email = $q->param('email');
my $comments = $q->param('comments');
open (MAIL, "| /usr/sbin/sendmail -t -i")
or die "Cannot open sendmail: $!";
print MAIL "To: <XXXXXXX>\@aol.com";
print MAIL "Reply-To: <XXXXXX>\@hotmail.com";
print MAIL "Subject: Contact Form";
print MAIL "Content-type: text/plain \n\n";
print MAIL "Name -- $name \n";
print MAIL "email address -- $email";
print MAIL "Comments \n $comments";
close(MAIL);
print $q->redirect( 'gravemind.<XXXXXXXX>.com/static-thanks.html' );
#END SCRIPT
And I keep getting a "HTTP 404 - File not found"
Most likely because it tries to go to
/home/virtual/site227/fst/var/subdomain/gravemind/html/cgi-bin/gravemind.<XXXXXXX>.com
Instead of:
/home/virtual/site227/fst/var/subdomain/gravemind/html/static-thanks.html
EDIT: The mail never gets sent.
fireartist
12-20-2005, 09:42 AM
Change this
print $q->redirect( 'gravemind.<XXXXXXXX>.com/static-thanks.html' );
to this...
print $q->redirect( 'http://gravemind.<XXXXXXXX>.com/static-thanks.html' );
About the email, I suggest you check out MIME-Lite or another mailer from CPAN.
Ok thanks!
Now it takes me to the thank you page but still doesn't send the mail.
What exactly is MIME-Lite? A mail module? Does it work with a diffrent syntax then sendmail?
Could you post me a link so I could read up on it
fireartist
12-20-2005, 10:04 AM
MIME::Lite is a module which provides a friendly API to the sendmail program - one of my earlier posts in this thread had a link to the docs.
use MIME::Lite;
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message,
);
$msg->send;
winged1
12-20-2005, 10:08 AM
try putting a newline after each section;
print MAIL "To: <XXXXXXX>\@aol.com\n";
print MAIL "Reply-To: <XXXXXX>\@hotmail.com\n";
print MAIL "Subject: Contact Form\n";
print MAIL "Content-type: text/plain \n\n";
print MAIL "Name -- $name \n";
print MAIL "email address -- $email\n";
print MAIL "Comments \n $comments\n\n";
close(MAIL);
So then this should work?
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use MIME::Lite;
my $q = new CGI;
my $name = $q->param('name');
my $email = $q->param('email');
my $comments = $q->param('comments');
open (MAIL, "| /usr/sbin/sendmail -t -i")
or die "Cannot open sendmail: $!";
$msg = MIME::Lite->new(
From =>'<XXXXXX>.com',
To =>'<XXXXX>@aol.com',
Subject =>Comments,
Data =>"Name -- $name \n email address -- $email Comments \n $comments"
);
$msg->send;
close(MAIL);
print $q->redirect( 'http://gravemind.<XXXXXXX>.com/static-thanks.html' );
#END SCRIPT
fireartist
12-20-2005, 10:19 AM
get rid of the open(MAIL) or die;
then replace the close(MAIL) with $msg->send;
Thanks Guys!
The newline thing did it for me. Though I'll be sure to look into MIME lite also.
Thank you both for all your help.
~Jedo