Sending mail using Perl on Windows NET::SMTP

May 11th, 2010

NET:SMTP is a bit limited compared to Mail::Sender  or MIME::Lite.  For simple emails is is nice and simple.  It can also be handy when working on system where it is very difficult to make changes.  In my case I had 15 system with different version of Perl and make changes was difficult at best.

#!/perl/bin -w

use Net::SMTP;

$smtp = Net::SMTP->new( Host => “your.mail.server.com”,
Timeout => 20,
Debug   => 0,
) || die “SMTP failed”;

$smtp->mail(‘my.email@null.com’);     # use the sender’s address here
$smtp->recipient(‘someones.email@null.com’, ‘somebody.else@null.com’, { Notify => [‘FAILURE’,’DELAY’], SkipBad => 1 });   # Good
#        $smtp->to(‘someones.email@null.com’);        # recipient’s address
#        $smtp->to(‘somebody.else@null.com’);        # recipient’s address
$smtp->data();                      # Start the mail

# Send the header.
$smtp->datasend(“Priority: Urgent\n”);
$smtp->datasend(“To: someones.email\@null.com, somebody.else\@null.com\n”);
$smtp->datasend(“From: my.email\@null.com\n”);
$smtp->datasend(“Subject: Windows Perl Test Mail.\n”);
$smtp->datasend(“\n”);

# Send the body.
$smtp->datasend(“Please reply if you recieved this email.  My name is Mud.\n”);
$smtp->dataend();                   # Finish sending the mail
$smtp->quit;                        # Close the SMTP connection

If you call the pl2bat utility on your Perl script helloworld.pl, like this:

    C:\> pl2bat helloworld.pl

it will produce a batch file, helloworld.bat. You can then invoke the script just like this:

    C:\> helloworld
     Hello, World!


You can invoke it on the command line like this:

cat myfile.txtYou can use it with I/O redirection (Windows 2000 SP3 and later) like this:

cat myfile.txt > newfile.txt cat myfile.txt | more

The best approach to use when writing Perl scripts that need to send email and also need to work on both UNIX and Windows systems is to use the Mail::Sender modules

You can install Mail::Sender by running:

  ppm install mail-sender

…at a command prompt. Documentation for using Mail::Sender is available here:

http://search.cpan.org/~jenda/Mail-Sender-0.8.10/Sender.pm

unix mail with perl

http://www.revsys.com/writings/perl/sending-email-with-perl.html

MIME::Lite::TT::

Trackback URI | Comments RSS

Leave a Reply

For spam filtering purposes, please copy the number 5583 to the field below: