SMTP AUTH扩展故障与Python [英] SMTP AUTH extension trouble with Python
问题描述
我试图写一个简单的Python脚本通过我公司的SMTP服务器发送电子邮件。我使用下面的一段code的。
I am trying to write a simple Python script to send emails through my company's SMTP server. I am using the following piece of code.
#! /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!!!")
运行这个程序,会产生这样的结果。
Running this program, yields this result.
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扩展。错误?
How do I start debugging this "SMTP AUTH extension not supported by server." error?
P.S:我知道SMTP细节和凭据是正确的,因为我有确切的细节工作的Java类。
P.S.: I know the SMTP details and credentials are correct, as I have a working Java class with the exact details.
推荐答案
您得到的错误意味着你在跟谁说话不声称支持认证SMTP服务器。如果你看调试输出,你会看到,没有任何反应到你的 EHLO
取值包含 AUTH $ C $必要的声明C>。如果确实(正确)的支持认证,响应的人会是这样的:
The error you get means the SMTP server you're talking to doesn't claim to support authentication. If you look at the debug output you'll see that none of the responses to your EHLO
s contain the necessary declaration for AUTH
. If it did (properly) support authentication, one of the responses would be something like:
250 AUTH GSSAPI DIGEST-MD5 PLAIN
(至少在效应初探到 EHLO
在 STARTTLS
)。因为这种反应不包括在内。假设的smtplib服务器将无法处理 AUTH
命令,将拒绝发送。如果您确定您的SMTP服务器不支持,即使不把它发布到 AUTH
命令,你的可以的,它支持悄悄说服的smtplib AUTH
通过它明确地增加了的功能集。你需要知道是什么样的身份验证方案的支持,然后你可以这样做:
(at least in reponse to the EHLO
after the STARTTLS
.) Because that response isn't included, smtplib assumes the server won't be able to handle the AUTH
command, and will refuse to send it. If you're certain your SMTP server does support the AUTH
command even though it doesn't advertise it, you can sneakily convince smtplib that it supports AUTH
by explicitly adding it to the set of features. You'll need to know which kind of authentication schemes are supported, and then you can do:
smtp.starttls()
smtp.ehlo()
# Pretend the SMTP server supports some forms of authentication.
smtp.esmtp_features['auth'] = 'LOGIN DIGEST-MD5 PLAIN'
...但当然使SMTP服务器的行为的,按照规范将是一个更好的主意:)
... but of course making the SMTP server behave according to spec would be a better idea :)
这篇关于SMTP AUTH扩展故障与Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!