SMTP AUTH扩展麻烦与Python

GPX*_*GPX 2 python email authentication smtp smtplib

我正在尝试编写一个简单的Python脚本,通过我公司的SMTP服务器发送电子邮件.我正在使用以下代码.

#! /usr/local/bin/python

import sys,re,os,datetime
from smtplib import SMTP

#Email function
def sendEmail(message):
        sender="SENDERID@COMPANY.com"
        receivers=['REVEIVER1@COMPANY.com','RECEIVER2@COMPANY.com']
        subject="Daily Report - " + datetime.datetime.now().strftime("%d %b %y")
        header="""\
                From: %s
                To: %s
                Subject: %s

                %s""" % (sender, ", ".join(receivers), subject, message)
        smtp = SMTP()
        smtp.set_debuglevel(1)
        smtp.connect('X.X.X.X')
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()
        try:
                smtp.login('SENDERID@COMPANY.com', '********')
                smtp.sendmail(sender,receivers,header)
                smtp.quit()
        except Exception, e:
                print e

#MAIN
sendEmail("HAHHAHAHAHAH!!!")
Run Code Online (Sandbox Code Playgroud)

运行此程序会产生此结果.

connect: ('X.X.X.X', 25)
connect: ('X.X.X.X', 25)
reply: '220 COMPANY.com [ESMTP Server] service ready;ESMTP Server; 05/25/11 15:59:27\r\n'
reply: retcode (220); Msg: COMPANY.com [ESMTP Server] service ready;ESMTP Server; 05/25/11 15:59:27
connect: COMPANY.com [ESMTP Server] service ready;ESMTP Server; 05/25/11 15:59:27
send: 'ehlo SERVER1.COMPANY.com\r\n'
reply: '250-COMPANY.com\r\n'
reply: '250-SIZE 15728640\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 STARTTLS\r\n'
reply: retcode (250); Msg: COMPANY.com
SIZE 15728640
8BITMIME
STARTTLS
send: 'STARTTLS\r\n'
reply: '220 Ready to start TLS\r\n'
reply: retcode (220); Msg: Ready to start TLS
send: 'ehlo SERVER2.COMPANY.com\r\n'
reply: '250-COMPANY.com\r\n'
reply: '250-SIZE 15728640\r\n'
reply: '250 8BITMIME\r\n'
reply: retcode (250); Msg: COMPANY.com
SIZE 15728640
8BITMIME
send: 'quit\r\n'
reply: '221 [ESMTP Server] service closing transmission channel\r\n'
reply: retcode (221); Msg: [ESMTP Server] service closing transmission channel
ERROR: Could not send email! Check the reason below.
SMTP AUTH extension not supported by server.
Run Code Online (Sandbox Code Playgroud)

如何开始调试此"服务器不支持的SMTP AUTH扩展".错误?

PS:我知道SMTP的详细信息和凭据是正确的,因为我有一个工作的Java类,其中包含确切的详细信息.

Tho*_*ers 8

您获得的错误意味着您正在与之通话的SMTP服务器声称不支持身份验证.如果查看调试输出,您将看到对您的EHLOs的任何响应都没有包含必要的声明AUTH.如果它(正确)支持身份验证,其中一个响应将是这样的:

250 AUTH GSSAPI DIGEST-MD5 PLAIN
Run Code Online (Sandbox Code Playgroud)

(至少在响应EHLO之后STARTTLS.)因为没有包含该响应,smtplib假定服务器将无法处理该AUTH命令,并拒绝发送它.如果您确定您的SMTP服务器确实支持该AUTH命令,即使它没有通告它,您也可以AUTH通过明确地将其添加到功能集中偷偷地说服它支持的smtplib .您需要知道支持哪种身份验证方案,然后您可以执行以下操作:

smtp.starttls()
smtp.ehlo()
# Pretend the SMTP server supports some forms of authentication.
smtp.esmtp_features['auth'] = 'LOGIN DIGEST-MD5 PLAIN'
Run Code Online (Sandbox Code Playgroud)

...但当然使SMTP服务器按照规范运行会是一个更好的主意:)