Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

Perl TLS SMTP send failing on authentication step

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() . "'
";
    warn "error authenticating; message is '" . $mailer->message() . "'
";
    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.)

question from:https://stackoverflow.com/questions/65854026/perl-tls-smtp-send-failing-on-authentication-step

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...