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.
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;
Normally SMTP uses PLAIN authentication, why are you base64 encoding it?? There are many authentication mechanisms!
There a method auth ( USERNAME, PASSWORD ), try using this instead, it'll automatically select the appropriate authentication mechanism!
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???
This code only seems to work. In case you have a negative response from the server, eg. $smtp->response() will still return ok
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.
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
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