SMTP/Bad File descripter problem

M

marx

I'm having difficulty sending e-mail via SMTP. I keep getting the
following error. Attached is my code and below that is the form, any
help would be much apprecieated.


Error:
Bad file descriptor




#!c:\perl\bin -w
#
# Edit here =
#
# Set only ONE of either $mail_path which should go to a
# mail program like sendmail, or $smtp_server which will use
# a SMTP server. Do not set both at once!

#$mail_path='/usr/sbin/sendmail'; #example /use/lib/sendmail
$smtp_server='localhost'; #example smtp.mydomain.com

#Email address (Who will be receiving completed forms)

$toemail='(e-mail address removed)';

#Check access to script? If yes, please specify your domain as
mydomain.com
$domain='localhost';

#Collect email addresses in file? if yes, specify unix/nt path to file
where emails will be stored
$store='';

#Enable autoresponder?

$autoresponder=0;

#email message for responder

$aresp=qq~ Hello!

Thank you for form filling! I check it later and reply to you.

~;

#Default subject

$subject='Form submitted!';

#Default redirect

$redirect="http://my.yahoo.com";

#It all!
#See info about usage at readme.txt
################################################################################
$CRLF="\015\012";
eval { &main; };
if ($@) { error("Content-type: text/html\n\n <html>fatal error:
$@</html>"); }
sub main{
%in=&parse;
if ($smtp_server) {use Socket;$proto =
(getprotobyname('tcp'))[2]; $port = getservbyname('smtp',
'tcp'); $smtp_server =~ s/^\s+//g; $smtp_server =~
s/\s+$//g; $smtpaddr = ($smtp_server =~
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) ?
pack('C4',$1,$2,$3,$4) : (gethostbyname($smtp_server))[4]; if
(!$smtpaddr) {die "SMTP host not found!";exit;}}

#Check for requied fields as $email, subject, message

if ($in{'fromemail'} !~ /.+@.+\..+/) {error("Invalid email address");}
if (!$in{'subject'}) {$in{'subject'} = $subject;}

#Check access if need

if ($domain) {
if ($ENV{'HTTP_REFERER'} !~ /$domain/is) {
error("Access forbidden by administrator");
}
}

#Collect email address if need

if ($store) {
open (OUT, ">>$store") or error("Cannot write to file : $!");
print OUT "$in{'fromemail'}\n";
close OUT;
}



foreach $item (keys %in) {
$var="$item";
$val="$in{$item}";
if ($var =~ /\_val/is) {
if ($val eq "") {$var=~ s#\_val##is;$var=~ s#^(.*?)\_##is;error("$var
cannot be empty!");}

}
}



#Definition of variables

$fromemail=$in{'fromemail'}; #Sended email
$subject = $in{'subject'};

$url=$in{'redirect'}; # redirect if success

if (!$url) {$url=$redirect;}

$ccopy=$in{'cc'}; # carbon copy
$from=$in{'from'}; # From (name)

if ($ccopy) {
if ($ccopy !~ /.+@.+\..+/) {error("Invalid CC email
address");}
}

$efrom=$fromemail;

if ($from) {$efrom="$from <$fromemail>";}


delete $in{'cc'}; delete $in{'from'};
delete $in{'redirect'};
delete $in{'fromemail'};

#Generation of Message

$etext=qq~Hello!
This is results of submitted form by $ENV{'REMOTE_ADDR'}.

Submitter Email : $efrom

Fields:
~;

foreach $key (sort keys %in) {
$etext.="$key = $in{$key}\n";
}
$etext.="\n\n";

#send email



if (!$smtp_server) {
open (SM, "|$mail_path -t") or error("Error sending mail! Reason :
$!");
print SM "From: ", $efrom, "\n";
print SM "To: $toemail", "\n";
print SM "Cc: $ccopy\n", if ($ccopy);
print SM "X-Mailer: Email manager (http://localhost/)\n";
print SM "Subject: ", $subject, "\n\n";
print SM $etext;
close SM;

if ($autoresponder) {

open (SM, "|$mail_path -t") or error("Error sending mail! Reason :
$!");
print SM "From: ", $toemail, "\n";
print SM "To: $efrom", "\n";
print SM "X-Mailer: Email manager (http://localhost/)\n";
print SM "Subject: ", $subject, "\n\n";
print SM $aresp;
close SM;
}


}else{
init_smtp($smtp_server);
socket(SOCK, AF_INET, SOCK_STREAM, $proto) or error ("Socket
operation failed : Reason $!");
connect(SOCK, pack('Sna4x8', AF_INET, $port, $smtpaddr)) or error
("Connection failed : Reason $!");
$oldfh = select(SOCK);
$| = 1; select($oldfh);$_ = <SOCK>;
if (/^[45]/) { close SOCK; error("Service not available : Reason
$!"); }
print SOCK "helo localhost$CRLF"; $_ = <SOCK>;
if (/^[45]/) { close SOCK; error ("Communication error : Reason $!");
}
print SOCK "mail from: <", $efrom, ">$CRLF"; $_ = <SOCK>;
if (/^[45]/) { close SOCK; error ("Communication error : $proto
$toemail $mail_path $smtp_server Reason $!"); }
foreach (split(/,/, $toemail)) {
(/<(.*)>/) ? print SOCK "rcpt to: $1$CRLF" : print SOCK "rcpt to:
<$_>$CRLF";
$_ = <SOCK>; if (/^[45]/) { close SOCK;error("Unknown user. Email
address not valid"); }
}
print SOCK "data$CRLF";$_ = <SOCK>;
if (/^[45]/) { close SOCK; error("Communication error : Reason $port
$!"); }
print SOCK "To: $toemail", $CRLF;
print SOCK "From: $efrom",$CRLF;
print SOCK "Cc: $ccopy\n", if ($ccopy);
print SOCK "X-Mailer: Email manager (http://localhost/)$CRLF";
print SOCK "Subject: $subject",$CRLF,$CRLF;
$message =~ s/\r//g;
$message =~ s/\n/$CRLF/g;
print SOCK $etext;
print SOCK $CRLF, '.', $CRLF;$_ = <SOCK>;
if (/^[45]/) { close SOCK;error("Transfer failed : Reason $!"); }
print SOCK "quit", $CRLF;$_ = <SOCK>;close SOCK;


if ($autoresponder) {

socket(SOCK, AF_INET, SOCK_STREAM, $proto) or error("Socket
operation failed : Reason $!");
connect(SOCK, pack('Sna4x8', AF_INET, $port, $smtpaddr)) or
error("Connection failed : Reason $!");
$oldfh = select(SOCK);
$| = 1; select($oldfh);$_ = <SOCK>;
if (/^[45]/) { close SOCK; error("Service not available : Reason
$!"); }
print SOCK "helo localhost$CRLF"; $_ = <SOCK>;
if (/^[45]/) { close SOCK; error("Communication error : Reason $!");
}
print SOCK "mail from: <", $toemail, ">$CRLF"; $_ = <SOCK>;
if (/^[45]/) { close SOCK; error("Communication error : Reason $!");
}
foreach (split(/,/, $efrom)) {
(/<(.*)>/) ? print SOCK "rcpt to: $1$CRLF" : print SOCK "rcpt to:
<$_>$CRLF";
$_ = <SOCK>; if (/^[45]/) { close SOCK;error("Unknown user. Email
address not valid"); }
}
print SOCK "data$CRLF";$_ = <SOCK>;
if (/^[45]/) { close SOCK; error("Communication error : Reason $!");
}
print SOCK "To: $efrom", $CRLF;
print SOCK "From: $toemail",$CRLF;
print SOCK "X-Mailer: Email manager (http://localhost/)$CRLF";
print SOCK "Subject: $subject",$CRLF,$CRLF;
$aresp =~ s/\r//g;
$aresp =~ s/\n/$CRLF/g;
print SOCK $aresp;
print SOCK $CRLF, '.', $CRLF;$_ = <SOCK>;
if (/^[45]/) { close SOCK;error("Transfer failed : Reason $!"); }
print SOCK "quit", $CRLF;$_ = <SOCK>;close SOCK;


}


}




}

if ($url) {
if (!$smtp_server) {
print "Location: $url \n\n"; exit;
}else{
print "Content-type: text/html\n\n"; #for some servers.
print qq~<html><META HTTP-EQUIV="Refresh" Content=
"0; URL=$url"></html>~;exit;
}
}
print "Content-type: text/html\n\n";

print qq~
<html><head><title>Email results</title></head>
<body>
<font face="Verdana">
<center><font size="3"><b>Email is sent</b><font
face="2"><br></center>
<br><br><br>
<small><center>(c)-2001, <a
href="localhost">localhost</a></center></font>

</body>
</html>
~;
exit;

sub error{
$mes=shift;
print qq~Content-type: text/html\n\n
<font face="Verdana" size="2"><center><b>Error</b> : $mes<br><br>
<a href="javascript:history.go(-1)">Back</a>.<br><br><br>
<small><a href="http://localhost">localhost</a>
~;
exit;
}
sub parse{my (@pairs, %in);my (@pairs, %in);my ($buffer, $pair, $name,
$value);if ($ENV{'REQUEST_METHOD'} eq 'GET') {@pairs = split(/&/,
$ENV{'QUERY_STRING'});}elsif($ENV{'REQUEST_METHOD'} eq 'POST')
{read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});@pairs = split(/&/,
$buffer);}PAIR: foreach $pair (@pairs) {($name, $value) = split(/=/,
$pair);$name =~ tr/+/ /;$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",
hex($1))/eg;$value =~ tr/+/ /;$value =~
s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;($value eq "---")
and next PAIR;exists $in{$name} ? ($in{$name} .= "~~$value") :
($in{$name} = $value);}return %in;}

sub init_smtp{$smtp_server=shift;use Socket;$proto =
(getprotobyname('tcp'))[2];$port = getservbyname('smtp',
'tcp');$smtp_server =~ s/^\s+//g;$smtp_server =~ s/\s+$//g;$smtpaddr =
($smtp_server =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) ?
pack('C4',$1,$2,$3,$4) : (gethostbyname($smtp_server))[4]; if
(!$smtpaddr) {return "SMTP host not found!";}return "ok";}

#form
<form method="post" action="/mail/formmail/formmail.pl">

<tr>
<td width="32%"><font size="2" face="Verdana">Your
name</font></td>
<td width="68%"><input type="text" name="from" size="20"></td>
</tr>
<tr>
<td width="32%"><font size="2" face="Verdana">Your
email</font></td>
<td width="68%"><input type="text" name="fromemail"
size="20"></td>
</tr>
<tr>
<td width="32%"><font size="2" face="Verdana">Subject</font></td>
<td width="68%"><select size="1" name="subject">
<option selected>Please contact me tomorrow</option>
<option>Please contact me today</option>
</select></td>
</tr>
<tr>
<td width="100%" colspan="2">
<p align="center"><font size="2"
face="Verdana">Fields</font></td>
</tr>
<tr>
<td width="50%"><font size="2" face="Verdana">Age</font></td>
<td width="50%"><input type="text" name="01_AGE_val"
size="20"></td>
</tr>
<tr>
<td width="50%"><font size="2" face="Verdana">Town</font></td>
<td width="50%"><input type="text" name="02_Town" size="20"></td>
</tr>
<tr>
<td width="50%"><font size="2" face="Verdana">Income</font></td>
<td width="50%"><select size="1" name="03_Income">
<option>Under $1000 / month</option>
<option>$1000 - 2000 per month</option>
<option>$2000 - $3000 per month</option>
<option>$3000 and more per month</option>
<option selected>Not specifed</option>
</select></td>
</tr>
<tr>
<td width="50%"><font size="2" face="Verdana">Interesting in our
services?</font></td>
<td width="50%"><select size="1"
name="04_Interested_in_our_services">
<option selected>Yes</option>
<option>No</option>
</select></td>
</tr>
<tr>
<td width="50%"><font size="2" face="Verdana">Country</font></td>
<td width="50%"><input type="text" name="05_Country_val"
size="20"></td>
</tr>
<tr>
<td width="50%"></td>
<td width="50%"><input type="submit" value="Send it!"
name="B1"></td>
</tr>
</form>
 
E

Eric Schwartz

I'm having difficulty sending e-mail via SMTP. I keep getting the
following error. Attached is my code and below that is the form, any
help would be much apprecieated.


Error:
Bad file descriptor

<snip ensuing 366 lines>

Your bug is that you're not using Net::SMTP. It is cross-platform, so
should work on any system you can run Perl on. I believe it's bundled
with Perl, but if not, I know you can get it on CPAN.

Your problem, which is separate from your bug, is that you either
haven't read the Posting Guidelines, which are posted regularly here,
and archived on groups.google.com, or you don't know how to post a
*small* (20 lines or so should do the trick) complete, runnable
program that exhibits the problem.

Though clpm has a bad rap for it, people here really do like to help
others out. But nobody wants to wade through an excessively long
script like that for such a vaguely-described problem.

-=Eric
 
G

gnari

[snipped perl4 cgi/smtp script
that does not use CGI, Net:SMTP, nor strict]

there is everything wrong with your post

you probably are not running a SMTP server either.

simplify your problem by cutting out everything that does not have
to do with SMTP. replace the rest with something that uses Net::SMTP.
fix your indenting in that, and add use strict.
if it compiles, but still does not run, post it here with any new info,
like error line, and so on.

gnari
 
J

James Willmore

I'm having difficulty sending e-mail via SMTP. I keep getting the
following error. Attached is my code and below that is the form, any
help would be much apprecieated.


Error:
Bad file descriptor

<snip>

In short - this is a mess.

Let me offer a few suggestions:
1) enable strict and warnings
2) since you appear to want to run this as a CGI script, enable taint
checking as well
3) for the love of everything, please use the CGI module

Finally - *please* start reading up on how to write CGI scripts. Visit
http://www.w3.org/Security/Faq/ for how to write secure CGI scripts. And
use Google to search USENET (aka Google Groups) for writing CGI scripts.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Ask five economists and you'll get five different explanations
<(six if one went to Harvard). -- Edgar R. Fiedler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top