Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

SMTP and Authentication

by mdog (Pilgrim)
on Mar 07, 2001 at 11:01 UTC ( [id://62677]=perlquestion: print w/replies, xml ) Need Help??

mdog has asked for the wisdom of the Perl Monks concerning the following question:

OK...So I love Net::SMTP and want to be able to use with this ISP that I have that requires that you authenticate before you send mail. The only thing is that that I can't find anywhere where one would enter a username and pass to use Net::SMTP. Everything that I see looks like:
use Net::SMTP; $smtp = Net::SMTP->new('mailhost'); $smtp->mail($ENV{USER}); $smtp->to('postmaster'); $smtp->data(); $smtp->datasend("To: postmaster\n"); $smtp->datasend("\n"); $smtp->datasend("A simple test message\n"); $smtp->dataend(); $smtp->quit;
Does anyone know of a way to pass the username/pass or another module that allows you to? Thanks!

Replies are listed 'Best First'.
Re: SMTP and Authentication
by saucepan (Scribe) on Mar 07, 2001 at 12:17 UTC
    How does your ISP require you to authenticate -- is it SMTP AUTH or POP-before-SMTP or something even more exotic? If it's POP-before-SMTP, use Net::POP3 to log into the POP server once per session, before submitting any outgoing mail (you shouldn't need to actually download any messages).

    If it's SMTP AUTH it looks like you might be out of luck, unless you feel like adding this functionality yourself (which actually doesn't look that difficult).

      If they only used POP-before-SMTP! Alas, it is SMTP AUTH. If anyone has battled with this before and already solved this problem, could they please post any relevant code? Otherwise, I'll take a look at tackling it myself. Thanks!
      MY ANSWER IS BELOW. THIS WORKS.
      ===============================


      use Net::SMTP;
      use MIME::Base64;

      $user = encode_base64($username);
      $pass = encode_base64($password);
      chomp($user);
      chomp($pass);

      $smtp->command('AUTH LOGIN');
      $smtp->command("$user");
      $smtp->command("$pass");

        I was struggling with the same thing and have tried your code. I am getting strange results: sometimes I am getting encouraging codes back such as 250 ("ok") and 354 ("go ahead") but often I get 502 ("unimplemented (#5.5.1)"). There seems to be no pattern in a series of 10 emails sent out through the same connection - codes seem random and some of the email is delivered, while most is not. Any ideas? thanks. (See below for the Debug output result after sending 2 messages -both undelivered)
        Net::SMTP: Net::SMTP(2.13) Net::SMTP: Net::Cmd(2.12) Net::SMTP: Export +er(5.562) Net::SMTP: IO::Socket::INET(1.25) Net::SMTP: IO::Socket(1.2 +6) Net::SMTP: IO::Handle(1.21) Net::SMTP=GLOB(0x104834)<<< 220 www.my +isp.com ESMTP Net::SMTP=GLOB(0x104834)>>> EHLO www2.myisp.net Net::SMTP=GLOB(0x104834)<<< 250-www.myisp.com Net::SMTP=GLOB(0x104834)<<< 250-PIPELINING Net::SMTP=GLOB(0x104834)<<< 250 8BITMIME Net::SMTP=GLOB(0x104834)>>> AUTH LOGIN Net::SMTP=GLOB(0x104834)>>> ZGVsZWdhXzE= Net::SMTP=GLOB(0x104834)>>> am93b2JvMTI= Net::SMTP=GLOB(0x104834)>>> MAIL FROM: Net::SMTP=GLOB(0x104834)<<< 502 unimplemented (#5.5.1) Net::SMTP=GLOB(0x104834)>>> RCPT TO: Net::SMTP=GLOB(0x104834)<<< 354 go ahead Net::SMTP=GLOB(0x104834)>>> DATA Net::SMTP=GLOB(0x104834)<<< 250 ok 1019918963 qp 4227 Net::SMTP=GLOB(0x104834)>>> To: myself@hotmail.com Net::SMTP=GLOB(0x104834)>>> From: theprogram@mydomain.com Net::SMTP=GLOB(0x104834)>>> Subject: first test Net::SMTP=GLOB(0x104834)>>> Net::SMTP=GLOB(0x104834)>>> testing... Net::SMTP=GLOB(0x104834)>>> . Net::SMTP=GLOB(0x104834)<<< 502 unimplemented (#5.5.1) Net::SMTP=GLOB(0x104834)>>> AUTH LOGIN Net::SMTP=GLOB(0x104834)>>> ZGVsZMdhXzE= Net::SMTP=GLOB(0x104834)>>> am93b3JvMTI= Net::SMTP=GLOB(0x104834)>>> MAIL FROM: Net::SMTP=GLOB(0x104834)<<< 502 unimplemented (#5.5.1) Net::SMTP=GLOB(0x104834)>>> RCPT TO: Net::SMTP=GLOB(0x104834)<<< 502 unimplemented (#5.5.1) Net::SMTP=GLOB(0x104834)>>> DATA Net::SMTP=GLOB(0x104834)<<< 502 unimplemented (#5.5.1) Net::SMTP=GLOB(0x104834)>>> To: myself@hotmail.com Net::SMTP=GLOB(0x104834)>>> From: theprogram@mydomain.com Net::SMTP=GLOB(0x104834)>>> Subject: second test Net::SMTP=GLOB(0x104834)>>> Net::SMTP=GLOB(0x104834)>>> Hi, this is another test Net::SMTP=GLOB(0x104834)>>> . Net::SMTP=GLOB(0x104834)<<< 250 ok Net::SMTP=GLOB(0x104834)>>> QUIT Net::SMTP=GLOB(0x104834)<<< 250 ok
      I think I have the solution for you. I found out that SMTP AUTH isn't as secure as people would like you to believe. Check out my page to see my code and you can also check to see if it works with your server before downloading.

      http://www.krkeegan.com/smtp_auth

      Kevin Robert Keegan
Re: SMTP and Authentication
by grinder (Bishop) on Mar 07, 2001 at 18:18 UTC

    I'm not sure I follow, but if you want to send mail, just use Mail::Sendmail, which, in spite of the misleading name, does not require sendmail(1) and is pure Perl to boot.

    From TFM:

    SYNOPSIS use Mail::Sendmail; %mail = ( To => 'you@there.com', From => 'me@here.com', Message => "This is a very short message" ); sendmail(%mail) or die $Mail::Sendmail::error; print "OK. Log says:\n", $Mail::Sendmail::log;
Re: SMTP and Authentication
by Dragonfly (Priest) on Mar 08, 2001 at 10:41 UTC
    Another alternative that might work well for you, if you're simply trying to send mail from your script, is the Mail::Mailer module, which is quite simple to use and will autodetect your MTA (Message Transfer Agent) automagically.

    It's pretty slick and has worked very well for me; some of my scripts periodically poll different services on my OpenBSD boxes and kick out a report to me (using Mail::Mailer) if there is something I should know about.

    Interestingly, if Mail::Mailer can't detect Sendmail, mail, Mail, or mailx on your system, it will use the aforementioned Net::SMTP module to deliver your message. ;-)

      I wish that they were on some flavor of Unix...would make my life much easier. They are on Windows 2000 and I am probably going to have resort to using Blat or trying to add authentication to Net::SMTP. Thanks for the advice, though, will have remember that module.
Re: SMTP and Authentication
by Jenda (Abbot) on Mar 31, 2003 at 12:26 UTC

    Just for reference. Mail::Sender supports these authentication protocols

    • LOGIN
    • PLAIN
    • CRAM-MD5
    • NTLM

    If you want to find out what protocols are supported by the mail server you may use this:

    use Mail::Sender; my $sender = new Mail::Sender {smtp => 'localhost'}; die "Error: $Mail::Sender::Error\n" unless ref $sender; print join(', ', $sender->QueryAuthProtocols()),"\n";

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

Re: SMTP and Authentication
by Anonymous Monk on Mar 09, 2001 at 14:24 UTC
    My answer has little to do with Perl, but I ran into the same
    problem a while ago with a customers ISP and their mail list.
    The ISP wanted to "authorize" the sender before it would send
    (this ISP was XO if you are interested)

    I couldn't figure out a way either without resulting to a clumbsy
    "expect" routine or something of that nature. What I found though
    was if I checked for mail first, and logged in that way, then
    I was authorized on the send directly afterwards. So, that's
    the way the script is written, it checks for mail,
    with useing account name and password first and then starts sending
    the weekly news letter.

    your milage may vary.
      Yeah, that means that the way that ISP is doing authentication is through a process called: POP-before-SMTP. There is another form of authentication called SMTP AUTH that won't work with this... Hopefully, this info will help someone else, though. Thanks!
        If your ISP is using POP-before-SMTP as a way of closing their SMTP servers to unauthorized relaying, you may have a slight latency problem if the POP server isn't on the same box as the SMPT server. My ISP (pair.com) usually takes 2-3 seconds, but sometimes takes upwards of 10 seconds to relay the right message from their POP to their SMPT server.

        It takes all of one line of script to work around this. The annoyance was mostly in discovering this behavior.

Re: SMTP and Authentication
by krkeegan (Novice) on Nov 02, 2001 at 06:46 UTC
    I think I have the solution for you. I found out that SMTP AUTH isn't as secure as people would like you to believe. Check out my page to see my code and you can also check to see if it works with your server before downloading.

    http://www.krkeegan.com/smtp_auth

    Kevin Robert Keegan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://62677]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-25 15:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found