Net::SMTP Can't get AUTH working...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Here is the script I am trying to get auth working was
working with an open relay mail server on our internal network but I
have to have it authenticate against our ISP now so I added the auth();
to the $mail-> section of the script.  Thanks!

#!/usr/bin/perl -w

use strict;
use Net::SMTP;
use Getopt::Long;
use Data::Dumper;

# args are:
# -s [subject]
# [0] = recipient
# STDIN = message

my $mailhost    = '';
my $from        = '';
my $subject     = undef;
my $authemail   = '';
my $authpw      = 'passwordtoauth';

my $result = GetOptions(
        "subject=s"     => $subject,
        "help:+"        => \&usage,);

sub usage {
        print "<message> \| $0 -s <subject> <recipient>\n";
        print "(acts like, and is a replacement for 'mail -s')\n";
        print "\n";

my $recipient   = $ARGV[0];
my $date        = scalar(localtime);
my $message     = undef;

while(<STDIN>) {
        $message .= $_;

# check to make sure we have everything we need..
if($date and $subject and $message) {
} else {
        if(!$date) {
                die "Could not create valid date\n";
        elsif(!$subject) {
                die "Did not find mail Subject line\n";
        elsif(!$message) {
                die "No message found to send\n";

sub send_mail {
        my $mail = eval { Net::SMTP->new(
                Host    => $mailhost,
                Timeout => 30,
                Hello   => 'this.server',
        if($@) {
                die "$0 failed with message:\n$@";

        # compose and send mail...
        $mail->datasend("From: $from\n");
        $mail->datasend("To: $recipient\n");
        $mail->datasend("Subject: $subject\n");
        $mail->datasend("Date: $date\n\n");


exit 0;

Re: Net::SMTP Can't get AUTH working...

Quoted text here. Click to load it

I couldn't spot anything wrong with that. But let's simplify it a little. In
a situation where I need to auth(),  I find the following works fine:

use warnings;
use strict;
my $ok;
my $to = '';
my $subject = 'Test subject';
my $body = <<'EOC';

This is a test.


use Net::SMTP;
my $relay="";
my $smtp=Net::SMTP->new($relay);
die "Could not open connection: $!" if (! defined $smtp);

$smtp->auth("my_username", "my_password");

$smtp->datasend("To: $to\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("Subject: $subject\n");

$ok = $smtp->dataend();
if($ok) {print "All seems well\n"}
else {print "dataend() returned false\n"}


If you change $to, $from and $relay to appropriate values - and also provide
correct arguments to auth(), then what do you get ?

Like I said, for me (having amended all of those items to appropriate
values) it works fine. It prints out "All seems well" and the message is
sent. However, if I comment out the '$smtp->auth(...);' then it prints
"dataend() returned false" and the message does not get sent.


Re: Net::SMTP Can't get AUTH working...

Thanks for helping out. I feel like there is something missing in this
problem.  I tried your script, and sure enough, it returned "All seems
well" but the message still did not get sent.
Sisyphus wrote:

Quoted text here. Click to load it

I tried commenting out "$smtp->auth(..." and the script still returned
"All seems well."  This does not make sense.

I setup the account in outlook as a regular POP account, with
'Authenticate with my outgoing SMTP server" and have the same values
and that works. Where is the disconnect here?


Re: Net::SMTP Can't get AUTH working...

Quoted text here. Click to load it

It makes no sense at all to me.
I couldn't find the dataend() documentation in perldoc ... but I'm sure I
read somewhere that if it returns true then that means everything worked,
otherwise something failed.

Turn on debugging in the constructor:
my $smtp=Net::SMTP->new($relay, Debug => 1);

Perhaps that might throw up something that solves the puzzle, as it should
show you the full client-server conversation.

In my case, the mail server that needs authentication is the
mail server. However, if  the email I'm sending is destined for an address, authentication is not needed. It's only when the email
goes to somewhere else that authentication is needed.

But as to how dataend() can return true and the email be not sent .... beats


Re: Net::SMTP Can't get AUTH working...

amattina wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

put in the line
          Debug => 1
here to get a lot of information printed to the console, which will
hopefully include the reason your script is unable to send mails.


Re: Net::SMTP Can't get AUTH working...

Christian Winter wrote:
Quoted text here. Click to load it

Good call.  Here is the output...with a little sanitation =D

/usr/bin/printf "Frontier Email test" | /usr/local/bin/
-s "Frontier Email Test #1!"
Net::SMTP>>> Net::SMTP(2.29)
Net::SMTP>>>   Net::Cmd(2.26)
Net::SMTP>>>     Exporter(5.58)
Net::SMTP>>>   IO::Socket::INET(1.29)
Net::SMTP>>>     IO::Socket(1.29)
Net::SMTP>>>       IO::Handle(1.25)
Net::SMTP=GLOB(0x96dcf1c)<<< ESMTP
Net::SMTP=GLOB(0x96dcf1c)<<< 220-. FrontierNet does not authorize the
use of its computers
Net::SMTP=GLOB(0x96dcf1c)<<< 220-.   and computer networks to accept,
transmit, or distribute
Net::SMTP=GLOB(0x96dcf1c)<<< 220-.   unsolicited bulk e-mail. Effective
Net::SMTP=GLOB(0x96dcf1c)<<< 220-.   FrontierNet may no longer accept
connections from IP
Net::SMTP=GLOB(0x96dcf1c)<<< 220-.   addresses which have no
reverse-DNS (PTR record) assigned.
Net::SMTP=GLOB(0x96dcf1c)<<< 220-.
Net::SMTP=GLOB(0x96dcf1c)<<< 220-. For more information, visit:
Net::SMTP=GLOB(0x96dcf1c)<<< 220-.
Net::SMTP=GLOB(0x96dcf1c)<<< 220
Net::SMTP=GLOB(0x96dcf1c)>>> EHLO
Net::SMTP=GLOB(0x96dcf1c)<<< 250-PIPELINING
Net::SMTP=GLOB(0x96dcf1c)<<< 250-SIZE 10240000
Net::SMTP=GLOB(0x96dcf1c)<<< 250-ETRN
Net::SMTP=GLOB(0x96dcf1c)<<< 250-AUTH LOGIN PLAIN
Net::SMTP=GLOB(0x96dcf1c)<<< 250-AUTH=LOGIN PLAIN
Net::SMTP=GLOB(0x96dcf1c)<<< 250 8BITMIME
Net::SMTP=GLOB(0x96dcf1c)<<< 250 Ok
Net::SMTP=GLOB(0x96dcf1c)<<< 554 Error: The message could not be sent
because your e-mail program needs to identify itself to the sending
mail server. Please see for
instructions on how to correct this setting in your e-mail program.
Net::SMTP=GLOB(0x96dcf1c)>>> DATA
Net::SMTP=GLOB(0x96dcf1c)<<< 554 Error: no valid recipients
Net::SMTP=GLOB(0x96dcf1c)>>> From:
Net::SMTP=GLOB(0x96dcf1c)>>> To:
Net::SMTP=GLOB(0x96dcf1c)>>> Subject: Frontier Email Test #1!
Net::SMTP=GLOB(0x96dcf1c)>>> Date: Fri Nov  3 12:07:19 2006
Net::SMTP=GLOB(0x96dcf1c)>>> Frontier Email test
Net::SMTP=GLOB(0x96dcf1c)>>> .
Net::SMTP=GLOB(0x96dcf1c)<<< 221 Error: Forbidden command. Goodbye.
Net::SMTP=GLOB(0x96dcf1c)>>> QUIT
Net::SMTP: Unexpected EOF on command channel at
/usr/local/bin/ line 76

How can I get this to work with the perl script?

Re: Net::SMTP Can't get AUTH working...

amattina wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

Between those last two lines you should normally see something like
Net::SMTP=GLOB(0x96dcf1c)>>> AUTH LOGIN
Net::SMTP=GLOB(0x96dcf1c)<<< 334 VXNlcm5hbWU6
Net::SMTP=GLOB(0x96dcf1c)>>> eW91cnVzZXJuYW1l
Net::SMTP=GLOB(0x96dcf1c)<<< 334 UGFzc3dvcmQ6
Net::SMTP=GLOB(0x96dcf1c)>>> eW91cnBhc3N3b3Jk
Net::SMTP=GLOB(0x96dcf1c)<<< 235 Authentication successful

(the exact dialog may vary, depending on the mechanisms the
server supports for authentication), but it should end in either
"235 Authentication successful" or "535 Error: authentication failed".

Do you have both MIME::Base64 and Authen::SASL modules installed,
which are needed to do SMTP auth?

You'll see if one is missing if you modify your auth line to:

        or die "Error authenticating.: " .
           $mail->code . ' ' . $mail->message;

If you get back "Error authenticating: 500 Need MIME::Base64 and
Authen::SASL todo auth" installing both (or whichever is missing)
should fix your problem.

The code() and message() methods are inherited from Net::Cmd,
just in case you're wondering where the heck I found those ;)


Re: Net::SMTP Can't get AUTH working...

Christian Winter wrote:
Quoted text here. Click to load it

Had cpan install both of those and then everything cranked though.

Thanks Chris! Great work.

Site Timeline