By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,374 Members | 2,014 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,374 IT Pros & Developers. It's quick & easy.

how to - Net::SMTP with user name & password

P: 7
I'm in dire need to use Net::SMTP. The server no longer uses "sendmail". I found an example of using Net::SMTP but I have to also include a user name and password for the server. The host provided that, and I discovered that the method call is, auth ( USERNAME, PASSWORD ). But I don't know where to put that call. The host isn't into Perl and I'm on my own about this.

Could someone be so extremely kind as to show me a simple example of sending email (no attachment) with Net::SMTP along with providing the user name and password in the code? I have a little understanding of basic Perl and "sendmail" made life easy. But now... egads... the good old days are gone.

Thanks to any kind soul.
Jul 23 '08 #1
Share this Question
Share on Google+
5 Replies


P: 36
Hi you can try this one to send mail
Expand|Select|Wrap|Line Numbers
  1.  use Mail::Sender;
  2. sub sendEmail
  3. {    
  4.     my $mailmsg =shift;    
  5.     my $sender = new Mail::Sender {from => ''}; 
  6.  
  7. $sender->MailMsg(
  8. {   
  9.     smtp => '',
  10.     to      => [email],
  11.     subject => "",
  12.     msg => " "
  13. });
  14.  
  15. }    
  16.  
  17.  
Jul 23 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
Hi you can try this one to send mail
Expand|Select|Wrap|Line Numbers
  1.  use Mail::Sender;
  2. sub sendEmail
  3. {    
  4.     my $mailmsg =shift;    
  5.     my $sender = new Mail::Sender {from => ''}; 
  6.  
  7. $sender->MailMsg(
  8. {   
  9.     smtp => '',
  10.     to      => [email],
  11.     subject => "",
  12.     msg => " "
  13. });
  14.  
  15. }    
  16.  
  17.  
Unfortunately, since the OP specified that they have a username and password setup for accessing the email, this doesn't solve their problem. They are actually on the right path.

Regards,

Jeff
Jul 23 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
You should take a look at the CPAN documentation for Net::SMTP as it is pretty good, but in my opinion, does lack examples of where to put stuff, I understand that.

Personally, I would define my new constructor and then right after that put the auth line, as so:

Expand|Select|Wrap|Line Numbers
  1. $smtp->auth(uname, pwd);
  2.  
Also, here is a page with examples of how to use this module as well.

If I were you, I would write it up and try to get it to work. If you have issues, post your code here (enclosed in code tags) and we will try and help you get it working.

Regards,

Jeff
Jul 23 '08 #4

P: 7
Thanks lovely people. Well here's the code I'm working with:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Net::SMTP;
  4.  
  5. print "content-type: text/html\n\n <center>start, using: mailhost<br>";
  6.  
  7. &domail;
  8.  
  9. print "done</center>";
  10.  
  11. sub domail {
  12.     print "in domail<br>";
  13.     $smtp = Net::SMTP->new(mailhost);
  14.     unless($smtp) {
  15.         print "smtp failed<br>";
  16.         exit;
  17.     }
  18.     unless( $smtp->auth('uname', 'upass') ) { # ** <- fails here
  19.         print "auth failed<br>";
  20.         exit;
  21.     }
  22.  
  23.     $smtp->mail("fromaddr");
  24.     $smtp->recipient("toaddr");
  25.  
  26.     $smtp->data;
  27.     $smtp->datasend("From: fromaddr");
  28.     $smtp->datasend("To: toaddr");
  29.     $smtp->datasend("Subject: This is a test");
  30.     $smtp->datasend("\n");
  31.     $smtp->datasend("blahblah");
  32.     $smtp->dataend;
  33.     $smtp->quit;
  34. }
  35.  
For the 'new' call I really am using "mailhost", and that seems to work, although I have yet to hear from the host if that's right. But I do get a value returned for $smpt on that. I removed actual username and password and email addresses here, but used valid info for the test.

Where it fails is after the attempt to do the authorization. The returned web page looks like this,

start, using: mailhost
in domail
auth failed
I'm not sure if I need to do the auth() after getting $smtp or maybe it should be,
Net::SMTP auth()

Thanks for this link, it was some help. CPAN is pretty frail in the realm of examples.
Jul 24 '08 #5

P: 7
Eureka!!!!

My bad. I was checking the return value for the auth() method... and there isn't any. I should have read the CPAN more closely. Odd that there is no ok/fail indication of authendication. Anyway, in case anyone else needs to know, here's what works.

This is in a very crude form.. no checks and not passing anything to the sub, just "hard wired" for the test.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use Net::SMTP;
  4.  
  5. print "content-type: text/html\n\n";
  6. &domail; # send the mail - normally pass info but here just call it
  7. print "done";
  8.  
  9. sub domail {
  10.     $smtp = Net::SMTP->new('mail.host.com'); 
  11.     unless($smtp) {
  12.         print "smtp failed<br>";
  13.         exit;
  14.     }
  15.  
  16.     $smtp->auth('user_name', 'user_password');
  17.  
  18.     $smtp->mail('me@here.com');
  19.     $smtp->recipient('them@there.com');
  20.  
  21.     $smtp->data;
  22.     $smtp->datasend('From: sender name <me@here.com>'."\n");
  23.     $smtp->datasend('To: recip name <them@there.com>'."\n");
  24.     $smtp->datasend("Subject: This is a test\n");
  25.     $smtp->datasend("\n");
  26.     $smtp->datasend("blahblah etc");
  27.     $smtp->dataend;
  28.     $smtp->quit;
  29. }
  30.  
Thanks to all who considered my problem.
Jul 24 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.