from OpenSSL.SSL import SSLv3_METHOD, TLSv1_METHOD
from twisted.mail.smtp import ESMTPSenderFactory
from twisted.python.usage import Options, UsageError
from twisted.internet.ssl import ClientContextFactory
from twisted.internet.defer import Deferred
from twisted.internet import reactor
def sendmail(
authenticationUsername, authenticationSecret,
fromAddress, toAddress,
messageFile,
smtpHost="email-smtp.us-east-1.amazonaws.com", smtpPort=587
):
"""
@param authenticationUsername: The username with which to authenticate.
@param authenticationSecret: The password with which to authenticate.
@param fromAddress: The SMTP reverse path (ie, MAIL FROM)
@param toAddress: The SMTP forward path (ie, RCPT TO)
@param messageFile: A file-like object containing the headers and body of
the message to send.
@param smtpHost: The MX host to which to connect.
@param smtpPort: The port number to which to connect.
@return: A Deferred which will be called back when the message has been
sent or which will errback if it cannot be sent.
"""
# Create a context factory which only allows SSLv3 and does not verify
# the peer's certificate.
contextFactory = ClientContextFactory()
contextFactory.method = TLSv1_METHOD
resultDeferred = Deferred()
senderFactory = ESMTPSenderFactory(
authenticationUsername,
authenticationSecret,
fromAddress,
toAddress,
messageFile,
resultDeferred,
contextFactory=contextFactory,heloFallback=True
)
reactor.connectTCP(smtpHost, smtpPort, senderFactory)
return resultDeferred
注意我已尝试过SSLv3和TLSv1,因此您将同时导入,但这不是问题。我一直得到的错误就是这个。回溯:
2013-05-23 01:19:17+0800 [ESMTPSender,client] SMTP Client retrying server. Retry: 5
2013-05-23 01:19:20+0800 [ESMTPSender,client] SMTP Client retrying server. Retry: 4
2013-05-23 01:19:22+0800 [ESMTPSender,client] SMTP Client retrying server. Retry: 3
2013-05-23 01:19:25+0800 [ESMTPSender,client] SMTP Client retrying server. Retry: 2
2013-05-23 01:19:28+0800 [ESMTPSender,client] SMTP Client retrying server. Retry: 1
2013-05-23 01:19:30+0800 [ESMTPSender,client] Failed to deliver mail [Failure instance: Traceback (failure with no frames): <class 'twisted.mail.smtp.TLSError'>: 454 Could not complete the SSL/TLS handshake
2013-05-23 01:19:30+0800 [ESMTPSender,client] <<< 250-AUTH PLAIN LOGIN
2013-05-23 01:19:30+0800 [ESMTPSender,client] <<< 250 Ok
2013-05-23 01:19:30+0800 [ESMTPSender,client] >>> STARTTLS
2013-05-23 01:19:30+0800 [ESMTPSender,client] <<< 454 TLS not available due to temporary reason: TLS already active
2013-05-23 01:19:30+0800 [ESMTPSender,client]
2013-05-23 01:19:30+0800 [ESMTPSender,client] ]
2013-05-23 01:19:30+0800 [ESMTPSender,client] Stopping factory <twisted.mail.smtp.ESMTPSenderFactory instance at 0x2119950>
Amazon Ses支持包装器和TLS,但在不同的端口上。与GMail的行为方式相似。
我试图完全删除ContextFactory。错误是一样的。
我尝试过smtplib以确保它不是我的系统或身份验证等。它工作正常。
说实话,我并不完全明白扭曲,所以我可能会做一些愚蠢的事情。上面的代码与其他地方的示例类似,应该可以使用。顺便说一句,我没有调用reactor.stop(),因为我只是在测试后ctrl-c。有线索吗?
完成更新: 我这样称呼上面的方法
sendmail('username','password',from, to,StringIO.StringIO(mail)).addCallbacks(self.delivered, self.failed)
答案 0 :(得分:1)
主机email-smtp.us-east-1.amazonaws.com端口587说未加密的ESMTP(可能称之为“TCP”)。它支持通过STARTTLS命令协商加密的ESMTP。
手动测试,我发现它按预期工作。
您粘贴的日志中的错误( TLS已激活)表明您已经有一个已经协商过TLS的连接(因为STARTTLS是通过TCP连接使用的,或者是因为您连接了到连接开始时自动协商TLS的其他服务器。
服务器拒绝通过TCP在TLS上运行TLS,这可能是明智的。但是,从您粘贴的代码中,我看不出TLS如何协商两次。也许如果您可以包含有关上下文的更多详细信息,答案将变得清晰。
可能您遇到http://tm.tl/3989。如果是这种情况,升级到Twisted 13.0.0或更高版本将解决此问题。但是,我不知道这是怎么回事,因为我看不到你的代码如何协商TLS两次。
实际上,在进一步调查中,您似乎正在经历http://tm.tl/3989的引入的回归。我已提交http://tm.tl/6524来跟踪此事。