Sendmail within CGI scripts

Installing, Configuring, Troubleshooting server daemons such as Web and Mail
Post Reply
Zoro
Posts: 29
Joined: 2007/12/15 11:05:30
Location: USA

Sendmail within CGI scripts

Post by Zoro » 2008/06/05 02:41:10

The email server is functioning properly, users can send and receive email through the POP accounts.

However, the internal sendmail features of our CGI programs are suddenly not working properly. Basically, a visitor fills out an online email form and the script sends it to our email address. It is a simple and effective method of keeping our email address within the script and away from spammers' bots.

The CGI mail programs were working fine for months since we installed the CentOS/Bluequartz NuOnce ver. 4.8.
http://www.nuonce.net/support/viewthread.php?tid=1848

Then, I noticed that there weren't any messages sent to the website using the contact form for the past week.

First, I checked the error log and it states:

[Mon Jun 02 14:06:41 2008] [error] [client ##.##.##.##] malformed header from script. Bad header=/home/.sites/28/site1/.users/1: cgiwrap, referer: http://www.domain.com/contact.html

All the CGI scripts are working normally. Only the scripts that use the internal Sendmail features are not working properly. In fact, this specific script worked perfectly for years and no changes were made to it. Checking the syntax on the script returned the following:

[b]perl -wc mail.cgi
mail.cgi syntax OK[/b]

I've included the (mail.cgi) script and web form for others who wish to use it and perhaps find the problem, to determine if its the script or system related issues, since there was an update to Dovecot recently. It can be custom tailered to your preferences by simply adding more fields to the web form. But for testing purposes, here's the simplified script and the current one that is giving the error message.

All server services are normal. I'm stomped. I appreciate any help. Thank you.

[code]
_______________________________
#!/usr/bin/perl
$MailProgram = '/usr/sbin/sendmail';
$MyEmail = 'webmaster@domain.com';

require 5;

sub ErrorHTML
{
my $ts = join '<li>',@_;
print "Content-type: text/html\n\n";
print <<HTML;
<html><body bgcolor="white"><blockquote><blockquote>
<h4>Press the back button and try again:</h4><ul><li>$ts</ul>
</blockquote></blockquote></body></html>
HTML
exit;
} # sub ErrorHTML

sub Parse
{
my $buffer;
ErrorHTML('Form must use method "POST"') unless ($ENV{REQUEST_METHOD} eq 'POST');
read(STDIN,$buffer,$ENV{CONTENT_LENGTH});
my @p = split(/&/,$buffer);
for(@p)
{
$_ =~ tr/+/ /;
my ($n,$v) = split(/=/,$_);
$n =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$v =~ s/\<(.*?)(embed|object|script|applet)(.*?)\>/<$1_${2}_$3>/sig;
$v =~ s/(?:[\n\r]|%0[ad])(b?cc\:.+\@.{4})/ $1/sig;
$In{$n} = $v;
}
} # sub Parse

sub DiscardExtraSpaces {my $s=$_[0];$s=~s/\A\s+//;$s=~s/\s+\Z//;$s=~s/\s\s+/ /g;return $s;}

sub ValidEmail
{
return 0 if $_[0] =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
return 0 if $_[0] !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,}|[0-9])(\]?)$/;
return 1;
} # sub ValidEmail

Parse;
$MailProgram .= ' -t' if $MailProgram =~ /sendmail\Z/;
$In{realname} = $In{name} if $In{name};
ErrorHTML('A name must be provided.') unless $In{realname} =~ /\w/;
ErrorHTML('Please provide an email address.') unless $In{email};
ErrorHTML("The email address \"$In{email}\" appears to be invalid.") unless ValidEmail($In{email});
ErrorHTML("The name, \"$In{realname}\", appears to be incorrect. Maybe you typed an email address by mistake?") if $In{realname} =~ /\@/;
$In{realname} = DiscardExtraSpaces($In{realname});
$In{subject} = 'Message' unless $In{subject};
open MAIL,"|$MailProgram";
print MAIL <<TO_END;
Content-type: text/html; charset=windows-1252
To: $MyEmail
From: "$In{realname}" <$In{email}>
Subject: $In{subject}

NAME: $In{realname}<br>
EMAIL: $In{email}<br>
IP: $ENV{REMOTE_ADDR}<br>
FORM: <a href="$ENV{HTTP_REFERER}">$ENV{HTTP_REFERER}</a><br>
MESSAGE:<br>
$In{message}<br>
-------------------------------------<br>
TO_END
for (sort keys %In)
{
next unless /\AX[\_\-]/i;
$tt = substr($_,4);
print MAIL "$tt\: $In{$_}<br>";
}
close MAIL;
unless($In{thankyou}) { $In{thankyou} = $In{redirect} ? $In{redirect} : $ENV{HTTP_REFERER}; }
print "Location: $In{thankyou}\n\n";
[/code]
_______________________________

Here's the code to insert into the section of a new Contact.html web form for this specific script.

_______________________________
[code]
<div align="center">
<center>
<table border="0" cellspacing="0" width="640" cellpadding="0">
<tr>
<td>
<div align="center">
<table border="0" cellspacing="0" width="100%" cellpadding="0" bgcolor="#FFFFFF">

<tr>
<td>
<table border="0" cellpadding="2" cellspacing="0" width="100%">
<tr>
<td width="100%">
<form action="http://www.domain.com/cgi-bin/mail.cgi" method="post">
<input type="hidden" value="http://www.domain.com/confirmation.html" name="thankyou">
<div align="center">
<center>

<table border="0" bgcolor="#F5F5EB" cellpadding="4" style="border: 1px solid #E6E6E6; ; border-collapse:collapse" bordercolor="#111111" cellspacing="3">
<tbody>
<tr>
<td align="left" colspan="2"><b><font face="verdana,arial,helvetica" size="1" color="#800000">Contact
ABC  </font></b><font face="arial,helvetica" size="1" color="#FF0000">(all
fields are
required)</font></td>
</tr>
<tr>
<td align="right"><font face="verdana,arial,helvetica" size="1">Full
name:</font></td>

<td><font face="verdana,arial,helvetica" size="1"><input size="40" name="name"></font></td>
</tr>
<tr>
<td vAlign="middle" align="right"><font face="verdana,arial,helvetica" size="1">Email:</font></td>
<td><font face="verdana,arial,helvetica" size="1"><input size="35" name="email"></font></td>
</tr>
<tr>
<td vAlign="middle" align="right"><font face="verdana,arial,helvetica" size="1">Subject:</font></td>

<td><font face="verdana,arial,helvetica" size="1"><input size="35" name="subject"></font></td>
</tr>
<tr>
<td vAlign="top" align="right"><font face="verdana,arial,helvetica" size="1">Message : </font></td>
<td><font face="verdana,arial,helvetica" size="1"><textarea name="message" rows="5" wrap="on" WRAP cols="65"></textarea></font></td>
</tr>

<tr>
<td><font face="verdana,arial,helvetica" size="1"> </font></td>
<td><font face="verdana,arial,helvetica" size="1"><input type="submit" value="OK send this message"></font></td>
</tr>
</tbody>
</table>
</center>
</div>
</form>

</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</td>
</tr>

</table>
</center>
</div>
[/code]_______________________________

Zoro
Posts: 29
Joined: 2007/12/15 11:05:30
Location: USA

Re: Sendmail within CGI scripts

Post by Zoro » 2008/06/14 07:50:14

With the help and guidance from Brian of [url=http://www.nuonce.net]NuOnce Networks[/url], this issue has been resolved.

After researching the sendmail options, I've resolved the problem with the following change to the [b]mail.cgi[/b] script.

[code]$MailProgram = '/usr/sbin/sendmail -i -t -f root';[/code]

Post Reply

Return to “CentOS 4 - Server Support”