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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veralee
    New Member
    • Jul 2008
    • 7

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

    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.
  • jain236
    New Member
    • Jul 2007
    • 36

    #2
    Hi you can try this one to send mail
    Code:
     use Mail::Sender;
    sub sendEmail
    {    
        my $mailmsg =shift;    
        my $sender = new Mail::Sender {from => ''}; 
       
    $sender->MailMsg(
    {   
        smtp => '',
        to      => [EMAIL],
        subject => "",
        msg => " "
    });
    
    }

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Originally posted by jain236
      Hi you can try this one to send mail
      Code:
       use Mail::Sender;
      sub sendEmail
      {    
          my $mailmsg =shift;    
          my $sender = new Mail::Sender {from => ''}; 
         
      $sender->MailMsg(
      {   
          smtp => '',
          to      => [EMAIL],
          subject => "",
          msg => " "
      });
      
      }
      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

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        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:

        Code:
        $smtp->auth(uname, pwd);
        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

        Comment

        • veralee
          New Member
          • Jul 2008
          • 7

          #5
          Thanks lovely people. Well here's the code I'm working with:

          Code:
          #!/usr/bin/perl
          
          use Net::SMTP;
          
          print "content-type: text/html\n\n <center>start, using: mailhost<br>";
          
          &domail;
          
          print "done</center>";
          
          sub domail {
          	print "in domail<br>";
          	$smtp = Net::SMTP->new(mailhost);
          	unless($smtp) {
          		print "smtp failed<br>";
          		exit;
          	}
          	unless( $smtp->auth('uname', 'upass') ) { # ** <- fails here
          		print "auth failed<br>";
          		exit;
          	}
          	
          	$smtp->mail("fromaddr");
          	$smtp->recipient("toaddr");
          
          	$smtp->data;
          	$smtp->datasend("From: fromaddr");
          	$smtp->datasend("To: toaddr");
          	$smtp->datasend("Subject: This is a test");
          	$smtp->datasend("\n");
          	$smtp->datasend("blahblah");
          	$smtp->dataend;
          	$smtp->quit;
          }
          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.

          Comment

          • veralee
            New Member
            • Jul 2008
            • 7

            #6
            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.

            Code:
            #!/usr/bin/perl
            
            use Net::SMTP;
            
            print "content-type: text/html\n\n";
            &domail; # send the mail - normally pass info but here just call it
            print "done";
            
            sub domail {
            	$smtp = Net::SMTP->new('mail.host.com'); 
            	unless($smtp) {
            		print "smtp failed<br>";
            		exit;
            	}
            
            	$smtp->auth('user_name', 'user_password');
            	
            	$smtp->mail('me@here.com');
            	$smtp->recipient('them@there.com');
            
            	$smtp->data;
            	$smtp->datasend('From: sender name <me@here.com>'."\n");
            	$smtp->datasend('To: recip name <them@there.com>'."\n");
            	$smtp->datasend("Subject: This is a test\n");
            	$smtp->datasend("\n");
            	$smtp->datasend("blahblah etc");
            	$smtp->dataend;
            	$smtp->quit;
            }
            Thanks to all who considered my problem.

            Comment

            Working...