Hello Joel,

Monday, August 13, 2001, Joel Divekar <[EMAIL PROTECTED]> wrote:

JD> Hi Friends

JD> Friday evening I heard a New that there is a change of MTA on our 
JD> WebServer. And then all my Perl scripts running on our WebServer refuse to 
JD> send me any stats / logs by email. And this gave me a jolt on Saturday 
JD> morning. I requested our Admin to send me docs related to our new MTA and 
JD> got the copy odf the same immediately. I opened my general purpose 
JD> sub-routine file modified mail sending part accordingly and again all my 
JD> scripts started sending me mails again. WOW but what happens if there is 
JD> change of MTA again and again and again.
JD> So I decided to write a simple SMTP program which makes a socket connection 
JD> to the specified port and send mail irrespective of a MTA.

JD> I started with the following code.

JD> ===============================================
JD> # Please note my system runs Win2k + Activestate Perl
JD> # And My Server Red Hat 6.2
JD> ===============================================

JD> #!/usr/bin/perl -w

JD> # use packages
JD> use strict;
JD> use IO::Socket;

JD> # Data Variables
JD> my ($SMTP_Server) = "10.10.10.10";
JD> my ($SMTP_Port) = "25";
JD> my ($CR) = "\r\n";
JD> my $Socket;

JD> # Mail Related Data
JD> my $from = "joel\@testlinux.net";
JD> my $to = "joel\@testlinux.net";

JD> # Open Socket - SMTP Connection
JD> $Socket = IO::Socket::INET->new(PeerAddr => "$SMTP_Server",
JD>                                  PeerPort => "smtp($SMTP_Port)",
JD>                                  Proto    => "tcp",
JD>                                  Type     => "SOCK_STREAM")
JD>            or die "Error : Cannot Open SMTP Socket. Reason : $!";

JD> # set buffering off or autoflush
JD> $|=1;

JD> # say hello the mail host
JD> # print $Socket "HELO $SMTP_Server" . $CR;

JD> # Send Mail Command

JD> # mail from
JD> print $Socket "MAIL FROM: $from" . $CR;

JD> # mail to
JD> print $Socket "RCPT TO: $to" . $CR;

JD> # Data command
JD> print $Socket "DATA" . $CR;

JD> # test message
JD> print $Socket "Test Message" . $CR;
JD> print $Socket "." . $CR;

JD> #quit SMTP Session
JD> print $Socket "QUIT" . $CR;

JD> # Close Connection
JD> close $Socket;

JD> # Print Socket Close
JD> print "SMTP Socket Closed" . $CR;

JD> ===============================================

JD> Problem : and the mail is received by me immediately
JD>                 but it gives To: as undisclosed-recipients

JD> Help : a) Where did I go wrong in my code ?
nothing wrong in your code. just add
print $Socket "To: $to\n";
after DATA command.

JD>            b) Any guideline on how to improve my code
perldoc Net::SMTP

           use Net::SMTP;

           $smtp = Net::SMTP->new('mailhost');

           $smtp->mail($ENV{USER});
           $smtp->to('postmaster');

           $smtp->data();
           $smtp->datasend("To: postmaster\n");
           $smtp->datasend("\n");
           $smtp->datasend("A simple test message\n");
           $smtp->dataend();

           $smtp->quit;

JD>            c) Any guidelines on attaching file(s)
http://search.cpan.org/doc/ERYQ/MIME-Lite-2.111/lib/MIME/Lite.pm

Best wishes,
 Maxim                            mailto:[EMAIL PROTECTED]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to