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

I want to write a script to send emails via my server's SMTP server. The server requires SMTP authentication.

But I've problem completing this SMTP authentication. When the script is ran, the server gave out an error message: 535 Incorrect authentication data.

Could you kindly help fix the problem?

Thanks.

Here's the script (only listing the SMTP authentication portion)
#!/usr/bin/perl use Net::SMTP_auth; $smtpserver = 'mail.myserver.com'; my $ServerAccount = "mailbox\@myserver.com"; my $ServerPwd = "mb128com"; my $smtp = Net::SMTP->new($smtpserver, Hello => "myserver.com", Debug + => 1); $smtp->auth('CRAM-MD5', '$ServerAccount', '$ServerPwd'); exit;

The followings is the output when the script is ran:

Net::SMTP>>> Net::SMTP(2.31)
Net::SMTP>>> Net::Cmd(2.29)
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(0x95167c0)<<< 220-server.myserver.com ESMTP Exim 4.69 #1 Wed, 16 Mar 2011 10:03:27 -0500
Net::SMTP=GLOB(0x95167c0)<<< 220-We do not authorize the use of this system to transport unsolicited,
Net::SMTP=GLOB(0x95167c0)<<< 220 and/or bulk e-mail.
Net::SMTP=GLOB(0x95167c0)>>> EHLO myserver.com
Net::SMTP=GLOB(0x95167c0)<<< 250-server.myserver.com Hello myserver.com
Net::SMTP=GLOB(0x95167c0)<<< 250-SIZE 52428800
Net::SMTP=GLOB(0x95167c0)<<< 250-PIPELINING
Net::SMTP=GLOB(0x95167c0)<<< 250-AUTH PLAIN LOGIN
Net::SMTP=GLOB(0x95167c0)<<< 250-STARTTLS
Net::SMTP=GLOB(0x95167c0)<<< 250 HELP
Net::SMTP=GLOB(0x95167c0)>>> AUTH PLAIN Q1JBTS1NRDUAQ1JBTS1NRDUAJFNlcnZlckFjY291bnQ=
Net::SMTP=GLOB(0x95167c0)<<< 535 Incorrect authentication data

Replies are listed 'Best First'.
Re: SMTP authentication problem
by toolic (Bishop) on Mar 16, 2011 at 17:20 UTC
    $smtp->auth('CRAM-MD5', '$ServerAccount', '$ServerPwd');
    I suspect you do not want single quotes around your variables. They prevent variable interpolation. Try this:
    $smtp->auth('CRAM-MD5', $ServerAccount, $ServerPwd);

    perlcritic can warn you of this common type of error:

    $ perlcritic -1 --verbose 6 893592.pl String *may* require interpolation at line 10, near ''CRAM-MD5', '$Ser +verAccount', '$ServerPwd''. (Severity: 1)
      Tried your suggestion. Sorry, it does not work.
Re: SMTP authentication problem
by rkrieger (Friar) on Mar 16, 2011 at 21:24 UTC

    Looking at your code, I get the impression you're trying to use CRAM-MD5 authentication. From the output you pasted, it would seem that your SMTP server only supports the PLAIN and LOGIN methods, though. That may be worth a look.

    That said, when I look at the Net::SMTP documentation, I only see two parameters for the auth() method: username and password. I suspect that's where your problem lies (apart from the earlier suggestion on interpolation).

    Lastly, you may want to use TLS when sending credentials, unless you have reason to trust the network(s) the packets travel over. I suppose that is of less immediate concern. For that, you could try e.g. Net::SMTP::SSL. That does SSL (typically port 465). I don't have a STARTTLS suggestion handy (which your mail server also appears to support).