Archive for the 'Perl' Category

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 => “”,
Timeout => 20,
Debug   => 0,
) || die “SMTP failed”;

$smtp->mail(‘’);     # use the sender’s address here
$smtp->recipient(‘’, ‘’, { Notify => [‘FAILURE’,’DELAY’], SkipBad => 1 });   # Good
#        $smtp->to(‘’);        # recipient’s address
#        $smtp->to(‘’);        # recipient’s address
$smtp->data();                      # Start the mail

# Send the header.
$smtp->datasend(“Priority: Urgent\n”);
$smtp->datasend(“To:\, somebody.else\\n”);
$smtp->datasend(“Subject: Windows Perl Test Mail.\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, like this:

    C:\> pl2bat

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:

unix mail with perl