Solved

Problem with Perl IO::Sockets script...incomplete output

Posted on 2006-07-13
8
367 Views
Last Modified: 2010-08-05
Hi,
I have recently installed a script a few days ago... Auction Script
Got everything working except email.

I have spent the last three days going through the forums and trying solutions, but to no avail.

The problem I am having is that the mail portion (below) is sending incomplete emails to the smtp server.
This is the only script usine IO::Sockets on my system and is being a pain..

I am running a Windows 03 server, IIS 6, Perl 5.8x (latest version), PHP, etc.
I have many other perl scripts which email just fine.

The auction script shows that the mail is sent from the script, but it never arrives.
I run my own mail servers on two different servers that I have allowed relaying from the site, but the mail that gets to the mail servers is corrupt. One mail servers logs show the mail is complete, but doesn't show it conforms to RFC standards and has incomplete headers. The other mail server shows an error 0, the the script is sending the email in an improper format.  It looks like the mail server isn't getting anything after it requests data, but I am not that experienced with this type of problem.

My site with the auction script is still in development at the moment and I am lost?

I am open to suggestions ????

This is what's getting through to the mail server with the A-S Script ;

8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 Connected
8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 >>> 220-mail.myserver.com ESMTP Merak 7.5.2; Mon, 10 Jul 2006 10:30:46 -0400
8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 <<< HELO 8.15.1.xxx
8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 >>> 250 mail.myserver.com Hello 8.15.1.xxx [8.15.1.xxx], pleased to meet you.
8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 <<< MAIL FROM: info@realestate.net
8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 >>> 250 2.1.0 <info@realestate.net>... Sender ok
8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 <<< RCPT TO: compuad77@xxx.com
8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 >>> 250 2.1.5 <compuad77@xxx.com>... Recipient ok; will forward
8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 <<< DATA
8.15.1.xxx [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 >>> 354 Enter mail, end with "." on a line by itself
SYSTEM [000003EC] Mon, 10 Jul 2006 10:30:46 -0400 Disconnected

There is nothing in the error logs, and the above are from the smtp's server logs.

Any Ideas would be appreciated.

Here is the subroutine causing my headache !

sub sendemail {
#---------------------------------------------------------------------------#
        my ($to,$subject,$from,$message) = @_;
 &send_error("COMMON::SENDMAIL\n\n\"To\" not sent to sendemail")      if ($to eq '');
 &send_error("COMMON::SENDMAIL\n\n\"Subject\" not sent to sendemail") if ($subject eq '');
 &send_error("COMMON::SENDMAIL\n\n\"From\" not sent to sendemail")    if ($from eq '');
 &send_error("COMMON::SENDMAIL\n\n\"Message\" not sent to sendemail") if ($message eq '');
        my $trash;
        if ($config{'mailhost'} ne '0') {
                eval('use IO::Socket; 1;') or &send_error("COMMON::SENDEMAIL\n\nIO::Socket could not be loaded by the script.  Please see the script documentation for details.  It looks like this server is using perl version $].  IO::Socket may not be included with versions of perl prior to 5.00404.");
                my $remote;
                $remote = IO::Socket::INET->new("$config{'mailhost'}:smtp(25)");
                $remote->autoflush();
                print $remote "HELO $config{'mailhost'}\r\n";
                $trash = <$remote>;
                print $remote "MAIL FROM: $config{'mailfrom'}\r\n";
                $trash = <$remote>;
                print $remote "RCPT TO: $to\r\n";
                $trash = <$remote>;
                print $remote "DATA\r\n";
                $trash = <$remote>;
                print $remote "To: $to\nFrom: <$config{'mailfrom'}>\nSubject: $subject\r\n\r\n";
                print $remote $message;
                print $remote "\r\n.\r\n";
                $trash = <$remote>;
                print $remote "QUIT\r\n";
        }
        else {
                open MAIL, "|$config{'mailprog'}";
                print MAIL "To: $to\n";
  print MAIL "From: $config{'mailfrom'}\n";
  print MAIL "Subject: $subject\n\n";
 
  print MAIL "$message\n";
  print MAIL "\n";
  print MAIL ".\n";
 
                close MAIL;
        }
}
#---------------------------------------------------------------------------#
0
Comment
Question by:compuad77
  • 4
  • 4
8 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17104326
Try changing these lines:

               print $remote "To: $to\nFrom: <$config{'mailfrom'}>\nSubject: $subject\r\n\r\n";
                print $remote $message;
                print $remote "\r\n.\r\n";
 
To:

               print $remote "To: $to\r\nFrom: <$config{'mailfrom'}>\r\nSubject: $subject\r\n\r\n";
               print $remote $message;
               print $remote "\r\n\r\n.\r\n";

The ending "." should have two CRLFs before it.  The other change was just for consistency sake.

 
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17104344
And also since you're on windows, I think just using the standard "\n" would give you the proper line terminators on your IO::Socket.  Unless you've flipped the socket to binmode somewhere.  I don't think a standard IO::Socket->new does an automatic open to binmode.

Since you've already got the \r\n throughout your code, you might as well flip your filehandle to binmode just to make sure the "\r\n" isn't sending "\r\r\n" behind the scenes.

   $remote = IO::Socket::INET->new("$config{'mailhost'}:smtp(25)");
   binmode $remote;
   $remote->autoflush();



0
 

Author Comment

by:compuad77
ID: 17106746
Hi, I tried the suggestions above, but still no joy.
Could it be doing this because there is no Mime or Content Type headers in the message format ?

???

-Richard
0
 

Author Comment

by:compuad77
ID: 17108027
Hi,
I don't think the script is sending everything the mail server needs;

Here is what is sent to the mail server from a working script on the same server;

8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 Connected
SYSTEM          [00001184] Fri, 14 Jul 2006 09:24:22 -0400 Client session DNS server 67.28.119.82 issuing MX query for "gmail.com"
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 >>> 220-mail.compu-ad.net ESMTP Merak 7.5.2; Fri, 14 Jul 2006 09:24:22 -0400
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 <<< EHLO localhost.localdomain
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 >>> 250-mail.compu-ad.net Hello localhost.localdomain [8.15.1.218], pleased to meet you.
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 <<< MAIL FROM:<info@realestate-fsbo.net>
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 >>> 250 2.1.0 <info@realestate-fsbo.net>... Sender ok
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 <<< RCPT TO:<info@realestate-fsbo.net>
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 >>> 250 2.1.5 <info@realestate-fsbo.net>... Recipient ok
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 <<< DATA
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 >>> 354 Enter mail, end with "." on a line by itself
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:22 -0400 *** <info@realestate-fsbo.net> <info@realestate-fsbo.net> 1 876 00:00:00 OK
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:23 -0400 >>> 250 2.6.0 876 bytes received in 00:00:00; Message accepted for delivery
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:23 -0400 <<< QUIT
8.15.1.218      [000016E0] Fri, 14 Jul 2006 09:24:23 -0400 >>> 221 2.0.0 mail.compu-ad.net closing connection
SYSTEM          [000016E0] Fri, 14 Jul 2006 09:24:23 -0400 Disconnected
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 25

Expert Comment

by:clockwatcher
ID: 17109828
The content-type isn't required.   Nothing's really required within the DATA block.  It would run into problems if there was something malformed in the MAIL FROM:, RCPT TO:, or you're not ending your DATA correctly.  Pretty much the only places that you'd have problems.

You might try to add some debug prints.  Temporarily replace it with this:

        if ($config{'mailhost'} ne '0') {
                eval('use IO::Socket; 1;') or &send_error("COMMON::SENDEMAIL\n\nIO::Socket could not be loaded by the script.  Please see the script documentation for details.  It looks like this server is using perl version $].  IO::Socket may not be included with versions of perl prior to 5.00404.");
                print "Content-type: text/html\n\n";   # make sure we're safe to write to the browser
        my $remote;
      binmode $remote;
                $remote = IO::Socket::INET->new("$config{'mailhost'}:smtp(25)");
                $remote->autoflush();
                print $remote "HELO $config{'mailhost'}\r\n";
                print $trash = <$remote>;
                print $remote "MAIL FROM: $config{'mailfrom'}\r\n";
                print $trash = <$remote>;
                print $remote "RCPT TO: $to\r\n";
                print $trash = <$remote>;
                print $remote "DATA\r\n";
                print $trash = <$remote>;
                print $remote "To: $to\nFrom: <$config{'mailfrom'}>\nSubject: $subject\r\n\r\n";
                print $remote $message;
                print $remote "\r\n\r\n.\r\n";
                print $trash = <$remote>;
                print $remote "QUIT\r\n";
      print $trash = <$remote>;
          }

If you can't get it to work, you could always replace that section of code with a mailer anyway.  Net::SMTP ought to be installed under a default ActiveState install.
0
 

Author Comment

by:compuad77
ID: 17113968
It still seems to be doing the same thing.  I have replaced the code above as suggested.
I do have Net::SMTP and it is being used by another script on the same server.
Can you suggest how, where and what code to replace????
You have been a lifesaver so far......

Thanks
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 17115228
Here's a replacement with Net:SMTP.  I'm not convinced it's going to have any better luck, but give it a try.  The IO::Socket one should have been working.

sub sendemail {
#---------------------------------------------------------------------------#
       my ($to,$subject,$from,$message) = @_;
       &send_error("COMMON::SENDMAIL\n\n\"To\" not sent to sendemail")      if ($to eq '');
       &send_error("COMMON::SENDMAIL\n\n\"Subject\" not sent to sendemail") if ($subject eq '');
       &send_error("COMMON::SENDMAIL\n\n\"From\" not sent to sendemail")    if ($from eq '');
       &send_error("COMMON::SENDMAIL\n\n\"Message\" not sent to sendemail") if ($message eq '');

       if ($config{'mailhost'} ne '0') {

          use Net::SMTP;
          my $smtp = Net::SMTP->new($config{'mailhost'});
          $smtp->mail($config{'mailfrom'});
          $smtp->to($to);
          $smtp->data();
          $smtp->datasend("To: $to\n");
          $smtp->datasend("From: <$config{mailfrom}>\n");  
          $smtp->datasend("Subject: $subject\n");
          $smtp->datasend("\n");
          $smtp->datasend($message);
          $smtp->dataend();
          $smtp->quit();

       }
       else {
         
          open MAIL, "|$config{'mailprog'}";
          print MAIL "To: $to\n";
          print MAIL "From: $config{'mailfrom'}\n";
          print MAIL "Subject: $subject\n\n";
          print MAIL "$message\n";
          print MAIL "\n";
          print MAIL ".\n";
          close MAIL;

       }
}
0
 

Author Comment

by:compuad77
ID: 17115544
You Are Great !

It works like a charm !

thanks again !
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now