cancel
Showing results for 
Search instead for 
Did you mean: 

cgi script to send mail failing

mikefa
Newbie
Posts: 8
Registered: ‎01-09-2007

cgi script to send mail failing

Hi,
A simple cgi script that's been working fine now fails.  The script takes the data from a form and uses sendmail to email the content back to me. 
I've tried changing the sendmail location from /usr/sbin to /usr/lib which made no difference and I changed the email address to my plusnet address which also didn't change anything.
I suspect it may have stopped working when php was upgraded.
Has anybody come across this or can anybody see the problem with the attached script?

Thanks,
Mike

#!/usr/bin/perl -wT
#
# mail-form cgi
# Quick and dirty Web form processing script.  Emails the data
# to the recipient.
#
# You should change any variable that has "CONFIGURE" in the
# comment.
#### modules (these don't need changing)
#
# be strict, define all variables
use strict;
# use CGI.pm for parsing form data and printing headers/footers
use CGI;
# redirect errors to the browser window
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
# create an instance of CGI.pm:
my($cgi) = CGI->new;
# CONFIGURE: change this to the proper location of sendmail on your system:
my($mailprog) = '/usr/sbin/sendmail';
# Set the path, so taint mode won't complain. This should be the path
# that sendmail lives in!  (If you're using /usr/lib/sendmail, then
# the path had better be /usr/lib here.)
$ENV{PATH} = "/sbin/lib";
# CONFIGURE: change this to the e-mail address you want to receive the
# mailed form data
my($recipient) = 'info@millennium-internet.co.uk';
# CONFIGURE: change this to your URL:
my($homepage) = "http://www.millennium-internet.co.uk/";
# CONFIGURE: change this to the subject you want the e-mail to have
# WARNING: Do NOT Let the form specify the subject line, or your
# program can be hijacked by spammers!!!
my($subj) = "Question from Millennium Website";
# everything below this shouldn't need changing, except possibly the
# "thank you" page at the end.
# Print out a content-type header
print $cgi->header;
# CGI.pm automatically parses the data, and you can retrieve it using
#    $cgi->param("fieldname")
# Now send mail to $recipient
open (MAIL, "|$mailprog -t") || &dienice("Can't open $mailprog!\n");
print MAIL "To: $recipient\n";
print MAIL "From: $recipient\n";
print MAIL "Subject: $subj\n\n";
# print all of the form fields:
foreach my $i ($cgi->param()) {
    print MAIL $i . ": " . $cgi->param($i) . "\n";
}
print MAIL "\n\n";
# print some extra info:
# print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n";
# print MAIL "HTTP From: $ENV{'HTTP_FROM'}\n";
# print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n";
# print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n";
close (MAIL);
# CGI.pm prints the HTML header
print $cgi->start_html(-title=>"Thank You",
      -bgcolor=>"#ffffff",
      -text=>"#000000");
# CONFIGURE:
# Print a thank-you page - you can customize this however you wish.
# Be sure to use proper HTML, and escape any $-signs or @-signs with a
# backslash, e.g.:  \$25.00, nullbox\@cgi101.com
print <<EndHTML;
<H2>Thank you for writing!</H2>
<p>
Thank you for your message.  Return to the
<a href="$homepage">home page.</a>
</p>
EndHTML
# CGI.pm prints the HTML footer
print $cgi->end_html;
# error handler
sub dienice {
    my($msg) = @_;
    print $cgi->start_html(-title=>"Error");
    print qq(<h2>Error</h2>\n);
    print $msg;
    print $cgi->end_html;
    exit;
}
# the end.
6 REPLIES 6
Peter_Vaughan
Grafter
Posts: 14,469
Registered: ‎30-07-2007

Re: cgi script to send mail failing

Make sure the perl script only has 700 permissions (rwx------). Any other permissions set for group or world/all will mean the script will not run.
http://community.plus.net/library/settings/cgi-platform-php-upgrade-faq/ item 6
mikefa
Newbie
Posts: 8
Registered: ‎01-09-2007

Re: cgi script to send mail failing

Thanks Peter,
I have tried changing the permissions.  It was set to 740.  I've changed it to 700 but still get the same result. 
Any other suggestions?
Thanks,
Mike 
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: cgi script to send mail failing

The script seems to work. Do you get anything if you call it directly with ?foo=bar after the url?
Gabe
mikefa
Newbie
Posts: 8
Registered: ‎01-09-2007

Re: cgi script to send mail failing

Hi Gabe,
I get the same result as if I'd pressed submit on my form.
http://ccgi.mikefa.plus.com/cgi-bin/mill/millemail.php?foo=bar or form
# # mail-form cgi # Quick and dirty Web form processing script. Emails the data # to the recipient. # # You should change any variable that has "CONFIGURE" in the # comment. #### modules (these don't need changing) # # be strict, define all variables use strict; # use CGI.pm for parsing form data and printing headers/footers use CGI; # redirect errors to the browser window use CGI::Carp qw(warningsToBrowser fatalsToBrowser); # create an instance of CGI.pm: my($cgi) = CGI->new; # CONFIGURE: change this to the proper location of sendmail on your system: my($mailprog) = '/usr/sbin/sendmail'; # Set the path, so taint mode won't complain. This should be the path # that sendmail lives in! (If you're using /usr/lib/sendmail, then # the path had better be /usr/lib here.) $ENV{PATH} = "/sbin/lib"; # CONFIGURE: change this to the e-mail address you want to receive the # mailed form data my($recipient) = 'info@millennium-internet.co.uk'; # CONFIGURE: change this to your URL: my($homepage) = "http://www.millennium-internet.co.uk/"; # CONFIGURE: change this to the subject you want the e-mail to have # WARNING: Do NOT Let the form specify the subject line, or your # program can be hijacked by spammers!!! my($subj) = "Question from Millennium Website"; # everything below this shouldn't need changing, except possibly the # "thank you" page at the end. # Print out a content-type header print $cgi->header; # CGI.pm automatically parses the data, and you can retrieve it using # $cgi->param("fieldname") # Now send mail to $recipient open (MAIL, "|$mailprog -t") || &dienice("Can't open $mailprog!\n"); print MAIL "To: $recipient\n"; print MAIL "From: $recipient\n"; print MAIL "Subject: $subj\n\n"; # print all of the form fields: foreach my $i ($cgi->param()) { print MAIL $i . ": " . $cgi->param($i) . "\n"; } print MAIL "\n\n"; # print some extra info: # print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n"; # print MAIL "HTTP From: $ENV{'HTTP_FROM'}\n"; # print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n"; # print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n"; close (MAIL); # CGI.pm prints the HTML header print $cgi->start_html(-title=>"Thank You", -bgcolor=>"#ffffff", -text=>"#000000"); # CONFIGURE: # Print a thank-you page - you can customize this however you wish. # Be sure to use proper HTML, and escape any $-signs or @-signs with a # backslash, e.g.: \$25.00, nullbox\@cgi101.com print <
Thank you for writing!
Thank you for your message. Return to the home page.
EndHTML # CGI.pm prints the HTML footer print $cgi->end_html; # error handler sub dienice { my($msg) = @_; print $cgi->start_html(-title=>"Error"); print qq(
Error
\n); print $msg; print $cgi->end_html; exit; } # the end.
I've tried passing the variables that the form passes to the script and get the same result.
Which has got me wondering if they are not being passed correctly to the script by the form.
Any reason for anything to have changed in this area?
You can see the actual form at www.millennium-internet.co.uk if it helps.
Any suggestions gratefully received.
Thanks,
Mike
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: cgi script to send mail failing

Try changing the extension to .pl
Gabe
mikefa
Newbie
Posts: 8
Registered: ‎01-09-2007

Re: cgi script to send mail failing

Hi Gabe,
That's it!  It now works as before.
Thanks a million.
Cheers,
Mike