Link to home
Start Free TrialLog in
Avatar of cwipfk
cwipfk

asked on

Sending email using sendmail and perl script.

Hello,
I'm currently trying to setup a perl script to send an email to a recipient based on form input from a website, the webserver and destination emailserver are seperate servers but in the same subnet, so it's not a firewall issue.  
I appear to have two email servers on my webserver, which is on a CentOS OS, sendmail, which i've just installed and one called Exim, which seems to be taking control.  
The exim log shows the following when the perl script is activated:
"2010-08-05 14:02:27 1Oh4lv-0002Yd-TW <= xxxx@servername.domain.net.fk U=horizon P=local S=499
2010-08-05 14:02:27 1Oh4lv-0002Yd-TW => cwfi <xxxxx@xxxx.co.fk> R=localuser T=local_delivery
2010-08-05 14:02:27 1Oh4lv-0002Yd-TW Completed"

Which seems to show that the Exim is accepting it and delivering it somewhere locally on the webserver, which i don't want.  As the Perl script is set to use SendMail.  Any ideas?

I'll include the Perl script below if that helps, but i'd say from the Exim log, that it is working:


#!/usr/bin/perl
# location of Perl (above) - check your doc or ask admin
#
#__________________________________________________________
#
# URL to go to if there is a problem with form input
$ErrorPage = "/a_fail.html";
#
#URL to go to if there is a problem with email format
$EmailError = "/a_email_fail.html";
# URL to go to when form has been successfully submitted
$ThankPage = "/a_thanks_Green.html";
# URL to go to if a 'foreign' referer calls the script
#$EvilReferer = "http://www.xxxxx.co.fk";
# E-mail address to send intake form to (your address)
# If not using PERL 5, escape the @ thus: \@ instead of @
$YourEmail = 'xxxxx@xxx.co.fk';
# Script works only on your server(s) - ('URL1','URL2')
#@referers = ('','domain.co.fk');
# Location of mail program - check your doc or ask admin
$MailProgram = '/usr/sbin/sendmail';
# Subject of the e-mail autoreply to the submitter
$Subject = "Go Green, Go Paperless" ;
# Header line in the auto-reply message
$Header = "Go Green, Go Paperless" ;
# Brief tail message for body of e-mail autoreply
$TailMessage = "If your message requires a reply, we'll get back to you soon.";
# Your signature lines the end of the autoreply e-mail
$Signature1 = "Webmaster";
$Signature2 = "domain.url";
#____________________________________________________________
#&CheckReferingURL;
&ReadParse;
$client_name = $in{'client_name'};
$client_email = $in{'client_email'};
$client_AccountNo = $in{'client_AccountNo'};
$client_phone = $in{'client_phone'};
$DirectDebit = $in{'DirectDebit'};
$ItemisedB = $in{'ItemisedB'};
&GetDate;
&SendSubmission;
print "Location: $ThankPage\n\n";
exit;
# _________________________________________________________
sub SendSubmission {
open (MAIL,"|$MailProgram -t");
#
print MAIL "To: $YourEmail\n";
print MAIL "From: $client_email\n";
print MAIL "Subject: $Subject\n";
print MAIL "$Date\n\n";
#
print MAIL "Details\n\n";
print MAIL "Name: $client_name\n";
print MAIL "Email: $client_email\n";
print MAIL "AccountNo: $client_AccountNo\n";
print MAIL "Phone: $client_phone\n";
print MAIL "Direct Debit Form: $DirectDebit\n";
print MAIL "Itemised?: $ItemisedB\n\n";
#
close (MAIL);
}
# _________________________________________________________
sub GetDate {
@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
;
@months = ('01','02','03','04','05','06','07','08','09','10','11','12');
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year+1900;
$Date = "$days[$wday] $months[$mon]/$mday/$year";
}
# _________________________________________________________
sub ReadParse { local (*in) = @_ if @_;
local ($i, $key, $val); if ( $ENV{'REQUEST_METHOD'} eq "GET" )
{$in = $ENV{'QUERY_STRING'};}
elsif ($ENV{'REQUEST_METHOD'} eq "POST")
{read(STDIN,$in,$ENV{'CONTENT_LENGTH'});}
else {
$in = ( grep( !/^-/, @ARGV )) [0];
$in =~ s/\\&/&/g; } @in = split(/&/,$in);
foreach $i (0 .. $#in) {
$in[$i] =~ s/\+/ /g;
($key, $val) = split(/=/,$in[$i],2);
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val; } return length($in); }
exit;




Any input would be greatly appreciated,

Ross
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmmm.. well, first off, change this line:

  open (MAIL,"|$MailProgram -t");

to:

  open (MAIL,"|$MailProgram -oi -t");

To ignore lines containing a single period (rarely needed, but a good idea, just in case you process a textarea where the user put a period on a line by itself).


It's odd that EXIM is doing a local delivery and reporting success.  Are you sure it isn't delivering to a local account somehow?  'sendmail' can be used for both sending and receiving e-mail, but it may be set up and/or used to do only one.  You can easily send mail using 'sendmail' on a server that is actually running Exim to do local & remote delivery.

There are several possibilities as to what could be going on here.  The web server might incorrectly be configure to handle mail for the other server, in which case it will definitely be doing a local delivery (maybe the web server once *was* the e-mail server).  Remove this as a possibility - set the destination e-mail to a Gmail or other 3rd part service and see what happens.
Avatar of bguyton3
bguyton3

You could consider using Net::SMTP to avoid any dependencies to a particluar mail client.  Example, add to the top:

use Net::SMTP;

and then change SendSubmission to

sub SendSubmission {
  my $smtp = Net::SMTP->new('localhost');
  #
  print MAIL "To: $YourEmail\n";
  print MAIL "From: $client_email\n";
  print MAIL "Subject: $Subject\n";
  print MAIL "$Date\n\n";
  #
  print MAIL "Details\n\n";
  print MAIL "Name: $client_name\n";
  print MAIL "Email: $client_email\n";
  print MAIL "AccountNo: $client_AccountNo\n";
  print MAIL "Phone: $client_phone\n";
  print MAIL "Direct Debit Form: $DirectDebit\n";
  print MAIL "Itemised?: $ItemisedB\n\n";
  #
  close (MAIL);
}

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I assume that the script you're currently using was downloaded from some web site instead of being written by you.  It is very poorly written, and I'm being polite.  It should not be used.

The formmail script from scriptarchive that Dave pointed to is better, but still poorly written.  The one from the NMS project is well written and would be a good choice if you simply need the form submission info.

http://nms-cgi.sourceforge.net/scripts.shtml
If you are familiar an used to this script, you might want to check whether the domain
xxx.co.fk is also configured on this domain and thus considered local.

Happened to me and the issue was purely dns

HTH

Rodrigo O
Here's a function I use that doesn't require an external module, just the location of sendmail.  Short and sweet, works perfectly for my automated reports.

sub sendEmail {
        my ($to, $from, $subject, $message) = @_;
        my $sendmail = '/usr/sbin/sendmail';

        open(MAIL, "|$sendmail -oi -t");
        print MAIL "From: $from\n";
        print MAIL "To: $to\n";
        print MAIL "Subject: $subject\n\n";
        print MAIL "$message\n";

        close(MAIL);
}
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.