Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Sending Mails with Perl using Net::SMTP (http://www.go4expert.com/articles/sending-mails-perl-using-netsmtp-t2556/)

pradeep 15Jan2007 16:19

Sending Mails with Perl using Net::SMTP
 
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.

l8rgator 23Jun2007 10:55

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;


pradeep 23Jun2007 11:37

Re: Sending Mails with Perl using Net::SMTP
 
Normally SMTP uses PLAIN authentication, why are you base64 encoding it?? There are many authentication mechanisms!

l8rgator 23Jun2007 11:51

Re: Sending Mails with Perl using Net::SMTP
 
Only because PLAIN didn't work, and this did.

I'm open to suggestions.

pradeep 23Jun2007 12:08

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!

l8rgator 23Jun2007 21:56

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???

rgermano 21Jan2009 13:56

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

Lizapotter 24Feb2009 13:17

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.

rahul_mawana 7Nov2009 09:22

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

espi3030 17Feb2010 18:25

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


All times are GMT +5.5. The time now is 21:52.