Using Perl To Send Email Via Esmtp

liunx

Guest
Greetings, <br /><br /> I've been working on a Perl script to send email from my home computer to an email account using my domain's SMTP server. The script is:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->#!/usr/bin/perl -w<br /><br />use Net::SMTP;<br />use Net::SMTP::SSL;<br /><br />my $server_name = "mail.****";<br /><br />$smtp = Net::SMTP::SSL->new($server_name, Timeout => 30, Debug =>1, Port=>465);<br />$smtp->auth('username+****', 'password');<br /><br />my $mail_from = "user\@****";<br />my $mail_to = "target\@targetdomain.com";<br /><br />$smtp->mail( $mail_from );<br />$smtp->recipient( $mail_to );<br />$smtp->data();<br />$smtp->datasend("To:  target\@targetdomain.com");<br />$smtp->datasend("From:  user\@****");<br />$smtp->datasend("Subject: Test from Perl\n");<br />$smtp->datasend("\n");<br />$smtp->datasend("This is a test message from Perl.\n");<br />$smtp->dataend();<br />$smtp->quit();<!--c2--></div><!--ec2--><br />When run, the following output is generated:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><<< 220-server29.totalchoicehosting.com ESMTP Exim 4.44 #1 Sun, 30 Jan 2005 11:17:24 -0600 <br /><<< 220-We do not authorize the use of this system to transport unsolicited, <br /><<< 220 and/or bulk e-mail.<br />>>> EHLO localhost.localdomain<br /><<< 250-server29.totalchoicehosting.com Hello localhost.localdomain [xx.xxx.xxx.xxx]<br /><<< 250-SIZE 52428800<br /><<< 250-PIPELINING<br /><<< 250-AUTH PLAIN LOGIN<br /><<< 250 HELP<br />>>> AUTH PLAIN c2NvdHQrc2NvdHRhbmRqdWxpZWFuZGVyc29uLmNvbQBzY290dCtzY290dGFuZGp1bGllYW5kZXJzb24uY29tA<br />Z1enp5bnV0cw==<br /><<< 235 Authentication succeeded<br />>>> MAIL FROM:<user@****><br /><<< 250 OK<br />>>> RCPT TO:<[email protected]><br /><<< 250 Accepted<br />>>> DATA<br /><<< 354 Enter message, ending with "." on a line by itself<br />>>> To:  [email protected]<br />>>> From:  user@****<br />>>> Subject: Test from Perl<br />>>> This is a test message from Perl.<br />>>> .<br /><<< 550 Administrative prohibition<br />>>> QUIT<br /><<< 221 server29.totalchoicehosting.com closing connection<!--c2--></div><!--ec2--><br /><br />Any ideas why exim is closing the connection? I appear to have authenticated properly with the system. Also, I can use SSL on port 465 and send email successfully using MS Outlook. Your help is appreciated---thanks!<!--content-->
Anderson, I don't think I have welcomed you to the family, so... welcome aboard! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/cool.gif" style="vertical-align:middle" emoid="B)" border="0" alt="cool.gif" /><br /><br />As for the problem you're experiencing, that's probably due to the fact that TCH sets the mail servers' security to not allow spammers to use them as relays. I'm not 100% sure about this but logging in via SMTP should be enough to prevent that but I do know that sometimes, ISPs force people to log in to their POP3 accounts before allowing them to use SMTP to send messages, so perhaps if you login into your POP account before logging into SMTP will do the trick.<br /><br />Also, here's a suggestion: Although completely possible, it's not usual to use Perl under a Windows environment (and even less to code scripts), so assuming you're using some form of Unix operating system, why don't you install Sendmail and use it to send e-mail?<!--content-->
Welcome to the forums, Anderson. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Welcome to the forums <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Welcome to the forums Anderson <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/cool2.gif" style="vertical-align:middle" emoid=":cool2:" border="0" alt="cool2.gif" /><!--content-->
Raul, <br /><br /> Thanks for the quick reply! I've been a very happy TCH customer for about a year and a half now.<br /><br /> Your point regarding using Sendmail is well taken---I am using this script on Linux and I was looking for a quick solution (and I didn't want to learn or have to worry about Sendmail).<br /><br /> I will try to log into my POP account first and them send mail. Thanks for your help and I'll let you know how it goes.<!--content-->
Depending on your Linux distribution, there may be some pre-configured packages available. For instance, I use Fedora Core and it has Sendmail all ready to go. I use it as the default mail delivery method most of the time.<br /><br />Also, using it is really really simple, there are lots of tutorials around on the web, so if you decide to go that way, it won't be that hard, don't worry <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br />Anyway, good luck and let me know how it goes <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
I found an error in my script which prevented me from sending mail through TCH:<br /><br />Original Code:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$smtp->datasend("To:  target\@targetdomain.com");<br />$smtp->datasend("From:  user\@****");<br />$smtp->datasend("Subject: Test from Perl\n");<!--c2--></div><!--ec2--><br /><br />Each header must be followed by a newline.<br /><br />Correct Code:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$smtp->datasend("To:  target\@targetdomain.com\n");<br />$smtp->datasend("From:  user\@****\n");<br />$smtp->datasend("Subject: Test from Perl\n");<!--c2--></div><!--ec2--><br /><br />You can also do this in the From header<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$smtp->datasend("From:  \"Your Name\" <user\@****>\n");<!--c2--></div><!--ec2--><br />to display your name instead of your email address.<br /><br />Headers followed by newlines is a must for TCH but I was able to send email (albeit a little jumbled...) through my ISP's SMTP server. This script makes use of <a href="http://www.perldoc.com/perl5.6.1/lib/Net/SMTP.html" target="_blank">Net::SMTP</a> which has all sorts of other features like CC, BCC, and receipt request.<!--content-->
Thank you for the update. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
<img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/kicking.gif" style="vertical-align:middle" emoid=":dance:" border="0" alt="kicking.gif" /> Welcome to the Family Anderson <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/kicking.gif" style="vertical-align:middle" emoid=":dance:" border="0" alt="kicking.gif" /> <br /><br />and your new home!<br />glad you worked out and yes thanks for the update,<br />you never know when it might help someone else in your family.<br /><br />We really are like family here.<br />So if you need anything,<br />just ask your new family!<br />We love to help <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Nice work, Anderson <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/thumbup1.gif" style="vertical-align:middle" emoid=":thumbup1:" border="0" alt="thumbup1.gif" /><br /><br />And thanks for the update <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br />By the way, I forgot to write this joke on my previous post:<br />So you're using Linux... I see you have chosen to take the red pill, Mr. Anderson. Good choice. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/tongue.gif" style="vertical-align:middle" emoid=":p" border="0" alt="tongue.gif" /><!--content-->
 
Top