Perl TLS SMTP发送在身份验证步骤失败 [英] Perl TLS SMTP send failing on authentication step

查看:77
本文介绍了Perl TLS SMTP发送在身份验证步骤失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Debian buster 服务器上通过 Perl 使用经过身份验证的 SMTP 发送邮件.这是我的代码:

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    = 'joeuser@example.com' ;
my $to      = 'joeuser@example.com' ;
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;
}

输出如下:

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

'

但是,请注意,在同一台计算机上运行的以下 Python 代码有效:

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())

为什么 Perl 代码不起作用?这个 Stackoverflow 问题 没有帮助.(为什么不直接使用 Python 代码?所需的 Perl 代码是更大的 Perl 项目的一部分,所以我需要在 Perl 中发送电子邮件.)

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.)

推荐答案

最终对我有用的是使用 use Authen::SASL qw(Perl):

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        = 'myname@example.com';
my $to          = 'afriend@example.net';
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;

这就引出了一个问题:为什么 使用 Authen::SASL; 不起作用.

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

这篇关于Perl TLS SMTP发送在身份验证步骤失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆