1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Sending Mails with Perl using Net::SMTP

Discussion in 'Perl' started by pradeep, Jan 15, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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:
      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.
     
  2. l8rgator

    l8rgator New Member

    Joined:
    Jun 23, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Pet Sitter
    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:
    #!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;
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Normally SMTP uses PLAIN authentication, why are you base64 encoding it?? There are many authentication mechanisms!
     
  4. l8rgator

    l8rgator New Member

    Joined:
    Jun 23, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Pet Sitter
    Only because PLAIN didn't work, and this did.

    I'm open to suggestions.
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    There a method auth ( USERNAME, PASSWORD ), try using this instead, it'll automatically select the appropriate authentication mechanism!
     
  6. l8rgator

    l8rgator New Member

    Joined:
    Jun 23, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Pet Sitter
    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???
     
  7. rgermano

    rgermano New Member

    Joined:
    Jan 21, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    This code only seems to work. In case you have a negative response from the server, eg. $smtp->response() will still return ok
     
  8. Lizapotter

    Lizapotter New Member

    Joined:
    Feb 24, 2009
    Messages:
    17
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Web Design London
    Location:
    UK
    Home Page:
    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.
     
    shabbir likes this.
  9. rahul_mawana

    rahul_mawana New Member

    Joined:
    Apr 4, 2009
    Messages:
    62
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  10. espi3030

    espi3030 New Member

    Joined:
    Feb 17, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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
     

Share This Page