SMTP AUTH扩展麻烦与Python

时间:2011-05-25 10:34:31

标签: 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!!!")

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

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.

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

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

1 个答案:

答案 0 :(得分:8)

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

250 AUTH GSSAPI DIGEST-MD5 PLAIN

(至少在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'

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