Perl Net::SMTP force auth method

Net::SMTP version 2.31_1 (newest pre 3.00 in libnet) overwrites mechanism in Authen::SASL with mechanism listed in EHLO reply.

Perl Net::SMTP force auth method

Net::SMTP version 2.31_1 (newest pre 3.00 in libnet) overwrites mechanism in Authen::SASL with mechanism listed in EHLO reply.

use Net::SMTP::SSL;
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);
}

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 find my UGLY fix.

use Net::SMTP::SSL;
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);
}

sub auth {
  my ($self, $username, $password) = @_;

  ...

  my $mechanisms = $self->supports('AUTH', 500, ["Command unknown: 'AUTH'"]);
  return unless defined $mechanisms;

  my $sasl;

  if (ref($username) and UNIVERSAL::isa($username, 'Authen::SASL')) {
    $sasl = $username;
    $sasl->mechanism($mechanisms); # <= HERE Authen::SASL mechanism overwrite 
  }