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

Perl email script not sending

Status
Not open for further replies.

pat18581

Programmer
Sep 28, 2005
1
GB
I am sending a details from a form on a webpage but no emails ever get recieved one is sent to the person that has sent it saying thank you and another to me. but they don't get sent here is the code i use can you see any problems.


use Socket;
use CGI ':standard';

#list of acceptable domains
my @okdomains=(" "//e-cp0om4q2l6umw/test1/english/test.html", "
#$lockfile="/temp/danmail.lck";

#leave either $SMTP_SERVER blank or $SEND_MAIL blank. Ensure the correct domain is entered if using $SMTP_SERVER.
my $SMTP_SERVER='217.19.248.xxx';
my $SEND_MAIL="";

#check url of reference
&valid_page;

#set standard parameters
my $returnurl=param('returnurl');
my $email=param('email');
my $subject=param('subject');
my $name=param('name');
my $cc=param('cc');
#my $mailfile="e:/logs/fiske-emails.txt";
#my $fieldfile="e:/logs/fiske-fields.txt";
&valid_data;

#set conditional parameters

#$RF=$ENV{'HTTP_REFERER'};
#$RF=~tr/A-Z/a-z/;
#if ($RF =~ /dev/)
#{
$hostmail='test1@test2.com';
#}
#else
#{
#$hostmail='test3@test2.com';
#}

$custmessage="Dear $name,\n\nThank you for your recent enquiry on our web site. We will be in touch shortly.\n\nBest Regards,\ntest";

#set date and time
my $SD=&sys_date;
my $ST=&sys_time;

#set body of message on ee side
my $message="The following request was received by the @okdomains[$website] website.\nPlease forward the Information requested in the email to the customer.\n\n";
foreach $value (param())
{
unless($value eq returnurl || $value eq subject || $value eq Submit || $value eq cc || $value eq mailfile || $value eq fieldfile)
{
$message=$message."$value : ".param($value)."\n";
}
}

#email log file
#open (EMF,">>$mailfile");
#print EMF "$email\n";
#close(EMF);

#log file for all fields submitted
#&get_the_lock;
#open(OUT_FILE,">>$fieldfile");
#foreach (param()){
#print OUT_FILE param($_)."\|";
#}
#print OUT_FILE "$SD\|$ST\|\n\n";
#close(OUT_FILE);
#&drop_the_lock;

#send the emails
&sendmail($email,$email,$hostmail,$SMTP_SERVER,$subject,$message);

if($email ne "")
{
&sendmail($hostmail,$hostmail,$email,$SMTP_SERVER,$subject,$custmessage);
}

if ($cc ne "")
{
&sendmail($email,$email,$cc,$SMTP_SERVER,$subject,$message);
}

#redirect to 'thankyou' url
print "Location: $returnurl\n\n";
exit;



sub valid_data
{
$position=index($email,'@');
$more=index($email,'@',($position+1));
if ($more>-1)
{
print "Content-type: text/html \n\n";
print 'Invalid/To many email address. Press the back button on your browser and re-enter the information.';
exit;
}
}

sub get_the_lock
{
local ($endtime);
$endtime = 60;
$endtime = time + $endtime;
while (-e $lockfile && time < $endtime)
{
# Do Nothing
}
open(LOCK_FILE, ">$lockfile");
}

sub drop_the_lock
{
close($lockfile);
unlink($lockfile);
}

sub valid_page
{
if (@okdomains == 0)
{return;}
$DOMAIN_OK=0;
$RF=$ENV{'HTTP_REFERER'};
$RF=~tr/A-Z/a-z/;
my $count=-1;
foreach $ts (@okdomains) {
$count++;
if ($RF =~ /$ts/)
{ $DOMAIN_OK=1; $website=$count;}
}
if ( $DOMAIN_OK == 0) {
print "Content-type: text/html\n\nInvalid referer.";
exit;
}
}

sub sys_date
{
%mn = ('Jan','01', 'Feb','02', 'Mar','03', 'Apr','04',
'May','05', 'Jun','06', 'Jul','07', 'Aug','08',
'Sep','09', 'Oct','10', 'Nov','11', 'Dec','12' );
$sydate=localtime(time);
($day, $month, $num, $time, $year) = split(/\s+/,$sydate);
$zl=length($num);
if ($zl == 1)
{ $num = "0$num";}
$yyyymmdd="$year\-$mn{$month}\-$num";
return $yyyymmdd;
}


sub sys_time
{
$sydate=localtime(time);
($day, $month, $num, $time, $year) = split(/\s+/,$sydate);
return $time;
}

###################################################################
#Sendmail.pm routine below by Milivoj Ivkovic (modified by bnb)
###################################################################
sub sendmail {

# error codes below for those who bother to check result codes <gr>

# 1 success
# -1 $smtphost unknown
# -2 socket() failed
# -3 connect() failed
# -4 service not available
# -5 unspecified communication error
# -6 local user $to unknown on host $smtp
# -7 transmission of message failed
# -8 argument $to empty
#
# Sample call:
#
# &sendmail($from, $reply, $to, $smtp, $subject, $message );
#
# Note that there are several commands for cleaning up possible bad inputs - if you
# are hard coding things from a library file, so of those are unnecesssary
#

my ($fromaddr, $replyaddr, $to, $smtp, $subject, $message) = @_;

$to =~ s/[ \t]+/, /g; # pack spaces and add comma
$fromaddr =~ s/.*<([^\s]*?)>/$1/; # get from email address
$replyaddr =~ s/.*<([^\s]*?)>/$1/; # get reply email address
$replyaddr =~ s/^([^\s]+).*/$1/; # use first address
$message =~ s/^\./\.\./gm; # handle . as first character
$message =~ s/\r\n/\n/g; # handle line ending
$message =~ s/\n/\r\n/g;
$smtp =~ s/^\s+//g; # remove spaces around $smtp
$smtp =~ s/\s+$//g;

$message = $message."\n.\n";

if (!$to)
{
return(-8);
}

if ($SMTP_SERVER ne "")
{
my($proto) = (getprotobyname('tcp'))[2];
my($port) = (getservbyname('smtp', 'tcp'))[2];

my($smtpaddr) = ($smtp =~
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
? pack('C4',$1,$2,$3,$4)
: (gethostbyname($smtp))[4];

if (!defined($smtpaddr))
{
return(-1);
}

if (!socket(MAIL, AF_INET, SOCK_STREAM, $proto))
{
return(-2);
}

if (!connect(MAIL, pack('Sna4x8', AF_INET, $port, $smtpaddr)))
{
return(-3);
}

my($oldfh) = select(MAIL);
$| = 1;
select($oldfh);

$_ = <MAIL>;
if (/^[45]/)
{
close(MAIL);
return(-4);
}

print MAIL "helo $SMTP_SERVER\r\n";
$_ = <MAIL>;
if (/^[45]/)
{
close(MAIL);
return(-5);
}

print MAIL "mail from: <$fromaddr>\r\n";
$_ = <MAIL>;
if (/^[45]/)
{
close(MAIL);
return(-5);
}

foreach (split(/, /, $to))
{
print MAIL "rcpt to: <$_>\r\n";
$_ = <MAIL>;
if (/^[45]/)
{
close(MAIL);
return(-6);
}
}

print MAIL "data\r\n";

$_ = <MAIL>;
if (/^[45]/)
{
close MAIL;
return(-5);
}

}

if ($SEND_MAIL ne "")
{
open (MAIL,"| $SEND_MAIL");
}

print MAIL "To: $to\n";
print MAIL "From: $fromaddr\n";
print MAIL "Reply-to: $replyaddr\n" if $replyaddr;
print MAIL "X-Mailer: Perl Powered Socket Mailer\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message";
print MAIL "\n.\n";

if ($SMTP_SERVER ne "")
{
$_ = <MAIL>;
if (/^[45]/)
{
close(MAIL);
return(-7);
}

print MAIL "quit\r\n";
$_ = <MAIL>;
}

close(MAIL);
return(1);
}
 
Don't know looks like an awful lot of code to just send 2 mails. Have you looked into making it a bit more friendly with mods like Mail::Sendmail. This mod does not use sendmail by the way so you do not have to have sendmail running for it to work.

-Alexander
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top