Learn how to Make Money Online doing freelancing, Affiliate Marketing, Blogging and many more ...
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > Perl

Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Article Tools Search this Article Display Modes

Sending Mails with Perl using Net::SMTP


On 15th January, 2007
Wink Sending Mails with Perl using Net::SMTP

Show Printable Version Email this Page Subscription Add to Favorites Copy Sending Mails with Perl using Net::SMTP link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

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("smtp.go4expert.com");
  #Send the MAIL command to the server.
  $smtp->mail("pradeep\@go4expert.com");
  #Send the server the 'Mail To' address.
  $smtp->to("shabbir\@go4expert.com");
  #Start the message.
  $smtp->data();
  #Send the message.
  $smtp->datasend("Hello World!\n\n");
  #End the message.
  $smtp->dataend();
  #Close the connection to your server.
  $smtp->quit();
Read more about Net::SMTP here.
Old 06-23-2007, 10:55 AM   #2
Newbie Member
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
l8rgator is on a distinguished road

Re: Sending Mails with Perl using Net::SMTP


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
#!C:\perl\bin\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]smtp.MYEMAILSMTPSERVER.com[/b]' ,
# may need a helo parameter here on some servers
           Timeout => 30,
           Debug   => 0,
       );

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

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

      #  -- Enter email box address password below.  We will use this to login to SMTP --
      $smtp->datasend(encode_base64('[b]TYPEEMAILACCOUNTPASSWORDHERE[/b]') );
      $smtp->response();
                     
      #  -- Enter email FROM below.   --
      $smtp->mail('[b]ENTEREMAILADDRESSFROM@DOMAINNAME.COM[/b]');

      #  -- Enter email TO below --
      $smtp->to('[b]ENTEREMAILADDRESSTOMAILTO@DOMAINNAME.COM[/b]');

      $smtp->data();

      #This part creates the SMTP headers you see
      $smtp->datasend("To: [b]Test\@DOMAINNAME.com[/b]\n");
      $smtp->datasend("From: A Test Account <[b]TEST\@DOMAINNAME.com[/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("\n");

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

      $smtp->datasend("\n");
      $smtp->dataend();

      $smtp->quit;
l8rgator is offline   Reply With Quote
Old 06-23-2007, 11:37 AM   #3
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,470
Thanks: 0
Thanked 35 Times in 30 Posts
Rep Power: 7
pradeep will become famous soon enough
Send a message via Yahoo to pradeep

Re: Sending Mails with Perl using Net::SMTP


Normally SMTP uses PLAIN authentication, why are you base64 encoding it?? There are many authentication mechanisms!
__________________
Vote for the Most Entertaining Member of 2008

To err is human,to detect is divine!
pradeep is offline   Reply With Quote
Old 06-23-2007, 11:51 AM   #4
Newbie Member
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
l8rgator is on a distinguished road

Re: Sending Mails with Perl using Net::SMTP


Only because PLAIN didn't work, and this did.

I'm open to suggestions.
l8rgator is offline   Reply With Quote
Old 06-23-2007, 12:08 PM   #5
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,470
Thanks: 0
Thanked 35 Times in 30 Posts
Rep Power: 7
pradeep will become famous soon enough
Send a message via Yahoo to pradeep

Re: Sending Mails with Perl using Net::SMTP


There a method auth ( USERNAME, PASSWORD ), try using this instead, it'll automatically select the appropriate authentication mechanism!
__________________
Vote for the Most Entertaining Member of 2008

To err is human,to detect is divine!
pradeep is offline   Reply With Quote
Old 06-23-2007, 09:56 PM   #6
Newbie Member
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
l8rgator is on a distinguished road

Re: Sending Mails with Perl using Net::SMTP


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???
l8rgator is offline   Reply With Quote
Old 01-21-2009, 12:56 PM   #7
Newbie Member
 
Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
rgermano is on a distinguished road

Re: Sending Mails with Perl using Net::SMTP


This code only seems to work. In case you have a negative response from the server, eg. $smtp->response() will still return ok
rgermano is offline   Reply With Quote
Old 02-24-2009, 12:17 PM   #8
Go4Expert Member
 
Join Date: Feb 2009
Location: UK
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 0
Lizapotter is on a distinguished road

Re: Sending Mails with Perl using Net::SMTP


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.


#!/usr/bin/perl
$Mailer = '/sbin/sendmail -t';
$Email = $ENV{QUERY_STRING};
open MAIL,"|$Mailer";
print MAIL <<THE_EMAIL;
From: me\@mydomain.com
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.


Yeah!
THE_EMAIL
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.
__________________
Watch PC TV
Lizapotter is offline   Reply With Quote
Old 11-07-2009, 08:22 AM   #9
Contributor
 
Join Date: Apr 2009
Posts: 57
Thanks: 4
Thanked 0 Times in 0 Posts
Rep Power: 2
rahul_mawana is on a distinguished road

Re: Sending Mails with Perl using Net::SMTP


Today mostly SMTP server require SSL authntication so u will have to use
Net::SMTP_auth instead of
Net::SMTP
.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
rahul_mawana is offline   Reply With Quote
Old 02-17-2010, 05:25 PM   #10
Newbie Member
 
Join Date: Feb 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
espi3030 is on a distinguished road

Re: Sending Mails with Perl using Net::SMTP


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,

espi3030
espi3030 is offline   Reply With Quote
Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
XML parsing in Perl pradeep Perl 4 05-27-2010 05:45 PM
Sending Emails in ASP.NET decodec ASP / ASP.NET 2 03-16-2009 12:33 PM
Sending e-mail in ASP with CDOSYS pradeep ASP / ASP.NET 4 08-08-2008 02:05 PM
PERL resources coderzone Perl 3 08-01-2006 11:53 PM
Paths in Perl pradeep Perl 0 10-17-2005 10:16 PM

 

All times are GMT +5.5. The time now is 05:29 AM.