skip to Main Content

I am trying to send mail using authenticated SMTP via Perl on a Debian buster server. Here is my code:

use strict ;
use warnings ;
use autodie ;
use Net::SMTP; # version 3.11
use Authen::SASL;

my $smtp_server = 'smtp.example.com' ;
my $principal   = 'myusername' ;

print "Password: ";
my $password = <STDIN>;
chomp($password);

my $from    = '[email protected]' ;
my $to      = '[email protected]' ;
my $subject = 'Test message' ;
my $body    = "The body of the e-mail message.";

my $mailer = Net::SMTP->new($smtp_server, 'SSL' => 1, 'Port' => 465, 'Debug' => 1);

if (!$mailer) { die $@ ; }

if (!$mailer->auth($principal, $password)) {
    warn "error authenticating; status is '" . $mailer->status() . "'n";
    warn "error authenticating; message is '" . $mailer->message() . "'n";
    exit 1;
}

Here is the output:

Password: secretsecretsecret
Net::SMTP::_SSL>>> Net::SMTP::_SSL
Net::SMTP::_SSL>>>   IO::Socket::SSL(2.060)
Net::SMTP::_SSL>>>     IO::Socket::IP(0.39)
Net::SMTP::_SSL>>>       IO::Socket(1.39)
Net::SMTP::_SSL>>>         IO::Handle(1.39)
Net::SMTP::_SSL>>>           Exporter(5.73)
Net::SMTP::_SSL>>>   Net::SMTP(3.11)
Net::SMTP::_SSL>>>     Net::Cmd(3.11)
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 220 smtp.example.com ESMTP Postfix
Net::SMTP::_SSL=GLOB(0x562cc320f080)>>> EHLO localhost.localdomain
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 250-smtp.example.com
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 250-PIPELINING
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 250-SIZE 51200000
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 250-ETRN
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 250-AUTH PLAIN LOGIN GSSAPI
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 250-ENHANCEDSTATUSCODES
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 250-8BITMIME
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 250-DSN
Net::SMTP::_SSL=GLOB(0x562cc320f080)<<< 250 CHUNKING
error authenticating; status is '2'
error authenticating; message is 'smtp.example.com
PIPELINING
SIZE 51200000
ETRN
AUTH PLAIN LOGIN GSSAPI
ENHANCEDSTATUSCODES
8BITMIME
DSN
CHUNKING

'

Note, however, the following Python code running on the same computer works:

#!/usr/bin/python3                                                                                             
username    = 'joeuser'
principal   = 'myusername'
smtp_server = 'smtp.example.com'
  
import smtplib
from email.mime.text import MIMEText
from getpass import getpass

conn = smtplib.SMTP_SSL(smtp_server, 465)
password = getpass()
conn.login(principal, password)

usermail = username + '@example.com'
content="""                                                                                                                        
Test message.                                                                                                                       
"""
msg = MIMEText(content, 'plain')
msg['Subject'] = 'Testing authenticated mail send using ' + principal
msg['From']    = usermail
conn.sendmail(usermail, usermail, msg.as_string())

Why does the Perl code not work? This Stackoverflow question did not help. (Why not just use the Python code? The desired Perl code is part of a larger Perl project, so I need to do the e-mail send in Perl.)

2

Answers


  1. Chosen as BEST ANSWER

    What finally worked for me was using use Authen::SASL qw(Perl):

    use strict ;
    use warnings ;
    
    use Net::SMTP
    #use Authen::SASL;         # This did NOT work.
    use Authen::SASL qw(Perl); # This DID work.
    
    my $smtp_server = 'smtp.example.com';
    my $principal   = 'myusername';
    my $password    = 'secretsecretsecret';
    
    my $from        = '[email protected]';
    my $to          = '[email protected]';
    my $subject     = 'Hello friend';
    my $body        = 'Give me a call when you can.';
    
    my $mailer = Net::SMTP->new($smtp_server, 'SSL' => 1, 'Port' => 465, 'Debug' => 1);
    if (!$mailer) { die $@ ; }
    
    if (!$mailer->auth($principal, $password)) {
        print "error authenticating; status is '" . $mailer->status() . "'n";
        print "error authenticating; message is '" . $mailer->message() . "n'";
        exit 1;                                                                                                                  
    }
    
    $mailer->mail($from) ;
    $mailer->to($to) ;
    $mailer->data();
    $mailer->datasend("To: $to");
    $mailer->datasend("n");
    $mailer->datasend("Subject: $subject");
    $mailer->datasend("n");
    $mailer->datasend($body);
    $mailer->dataend();
    $mailer->quit;
    

    This begs the question as to why use Authen::SASL; did not work.


  2. if (!$mailer->auth($principal, $password)) {
        die "error authenticating: $!" ;
    }
    

    In general looking at $! is not the correct way to debug problems when errors are returned by Net::SMTP. Instead status and message should be checked.

    My educated guess is that status will return 500 and message will return:

    Need MIME::Base64 and Authen::SASL todo auth 
    

    This would mean that you very likely have not installed Authen::SASL which is needed for doing authentication. This module is only an optional prerequisite of Net::SMTP since it is only needed when authentication should be done. See also the documentation for the auth method:

    Attempt SASL authentication. Requires Authen::SASL module.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search