Sending Mails with Perl using Net::SMTP

pradeep's Avatar author of Sending Mails with Perl using Net::SMTP
This is an article on Sending Mails with Perl using Net::SMTP in Perl.
There are many ways to send email from a Perl script, one the all time popular being using sendmail. But, there are other ways of sending emails, one of which is using the module Net::SMTP.All you have to do is to include the Net::SMTP module in your script and try using the example below.

Code: Perl
use Net::SMTP;
  #Create a new object with 'new'.
  $smtp = Net::SMTP->new("");
  #Send the MAIL command to the server.
  #Send the server the 'Mail To' address.
  #Start the message.
  #Send the message.
  $smtp->datasend("Hello World!\n\n");
  #End the message.
  #Close the connection to your server.
Read more about Net::SMTP here.
l8rgator's Avatar, Join Date: Jun 2007
Newbie Member
I had some trouble getting net:SMTP working on a server that required smtp authentication on a windows web server using cgi perl.

We finally got it working, so I wanted to share the code somewhere:

Code: PERL

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Net::SMTP;
use MIME::Base64;

      print header;
      print start_html("Environment");

      #Keep debug off in order for web and email to both work correctly on large messages
      $smtp = Net::SMTP->new(
          '[B][/B]' ,
# may need a helo parameter here on some servers
           Timeout => 30,
           Debug   => 0,

      $smtp->datasend("AUTH LOGIN\n");

      #  -- Enter sending email box address username below.  We will use this to login to SMTP --
      $smtp->datasend(encode_base64('[B]TYPEEMAILACCOUNTUSERNAMEHERE[/B]') );

      #  -- Enter email box address password below.  We will use this to login to SMTP --
      $smtp->datasend(encode_base64('[B]TYPEEMAILACCOUNTPASSWORDHERE[/B]') );
      #  -- Enter email FROM below.   --

      #  -- Enter email TO below --


      #This part creates the SMTP headers you see
      $smtp->datasend("To: [B]Test\[/B]\n");
      $smtp->datasend("From: A Test Account <[B]TEST\[/B]>\n");
      $smtp->datasend("Content-Type: text/html \n");
      $smtp->datasend("Subject: A Test Message");
      # line break to separate headers from message body

      $smtp->datasend("Here is my test message body");


pradeep's Avatar, Join Date: Apr 2005
Team Leader
Normally SMTP uses PLAIN authentication, why are you base64 encoding it?? There are many authentication mechanisms!
l8rgator's Avatar, Join Date: Jun 2007
Newbie Member
Only because PLAIN didn't work, and this did.

I'm open to suggestions.
pradeep's Avatar, Join Date: Apr 2005
Team Leader
There a method auth ( USERNAME, PASSWORD ), try using this instead, it'll automatically select the appropriate authentication mechanism!
l8rgator's Avatar, Join Date: Jun 2007
Newbie Member
That's what we thought, but after working on it that way for a day we just still couldn't get it to work.

I am not an expert on CGI/Perl, so I was working with someone who was - I apologize if I don't explain very well, but here is what I got out of it:

I'm sorry I didn't write down the exact error we started with, but it might have been the
535 Incorrect authentication data

Finally after a day of struggling, we decided to try AUTH LOGIN instead of PLAIN or the other option. That didn't work at first, so we tried encoding the username & password. Suddenly, it worked.
It seemed to need to wait for a response also, so that's why the $smtp->response(); was added.

Does that even make sense???
rgermano's Avatar, Join Date: Jan 2009
Newbie Member
This code only seems to work. In case you have a negative response from the server, eg. $smtp->response() will still return ok
Lizapotter's Avatar, Join Date: Feb 2009
Go4Expert Member
Hello pardeep

Good article, There are a number of ways to tell Perl scripts where to send email.

Here is the script. Below the code is a discussion of it's various elements.

$Mailer = '/sbin/sendmail -t';
open MAIL,"|$Mailer";
From: me\
To: $Email
Subject: My first mailer

This is the auto-response email sent when I launched the
script with a "?$Email" following the script's URL.

close MAIL;
print "Content-type: text/html\n\n";
print '<center>T H A N K Y O U !</center>';
# end of script

The first line of the script must have the location of Perl on your server.
rahul_mawana's Avatar, Join Date: Apr 2009
Today mostly SMTP server require SSL authntication so u will have to use
Net::SMTP_auth instead of
.It works fine i tested it if any body has problem about it then tell me ,i will post smtp server name(obivsly free h yaar) nd script that works fine,
till then happy progqaming
espi3030's Avatar, Join Date: Feb 2010
Newbie Member
I have a subfolder in my inbox named backups, is there any way I can get #!/usr/bin/perl -w use Net::SMTP; to send the e-mails to a subfolder in my inbox?

Thank you,