Wojciech Dyżewski Wojciech Dyżewski - 1 year ago 80
Perl Question

Perl Net::SMTP force auth method

I'm trying to send mail using Perl

Net::SMTP
with auth method other than default picked GSSAPI (e.g. force PLAIN).

I have tried:

my $smtp;
$smtp = Net::SMTP::SSL->new($host, Port => $port);
$smtp->auth($user, $passwd);


and replacing last line with:

$smtp->auth('PLAIN', $user, $passwd);


or passing
Authen::SASL
object with selected mechanism to
$smtp->auth()
. None of the above work - debug (and mail server logs) says it still tries
AUTH GSSAPI
.

Does anyone know how to correctly force auth method in
Net::SMTP
?

My Perl version is 5.20.2-3+deb8u8 from Debian 8, packages version:


  • Net::SMTP - 2.33

  • Net::SMTP::SSL - 1.01

  • Authen::SASL - 2.16


Answer Source

Net::SMTP version below 3.00

Net::SMTP version 2.31_1 (newest pre 3.00 in libnet) overwrites mechanism in Authen::SASL with mechanism listed in EHLO reply. Below please fing my UGLY fix.
Wait a day or two for suggestion of alternative fixes

use Net::SMTP;
use Authen::SASL;

my ( $host, $port, $user, $pass ) = ( '...', '...', '...', '...' );    # fill correct data

my $smtp = Net::SMTP::SSL->new( $host, Port => $port, Debug => 1 );
my $auth = Authen::SASL->new(
  mechanism => 'PLAIN LOGIN',
  callback  => { user => $user, pass => $passwd }
);
{
  no warnings 'redefine';
  my $count;
  local *Authen::SASL::mechanism = sub {
    my $self = shift;

    # Fix Begin
    # ignore first setting of mechanism
    if ( !$count++ && @_ && $Net::SMTP::VERSION =~ /^2\./ ) {
      return;
    }

    # Fix End
    @_
      ? $self->{mechanism} = shift
      : $self->{mechanism};
  };
  $smtp->auth($auth);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download