gpt4 book ai didi

python - Twisted 和 smtp tls 客户端

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:25 24 4
gpt4 key购买 nike

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 以确保它不是我的系统或身份验证等。它工作正常。

说实话,我并不完全理解twisted,所以我可能做了一些愚蠢的事情。上面的代码与其他地方的示例类似,应该可以工作。顺便说一句,我不调用reactor.stop(),因为我只是在测试后按ctrl-c。有什么线索吗?

更新完成:我像这样调用上面的方法

sendmail('username','password',from, to,StringIO.StringIO(mail)).addCallbacks(self.delivered, self.failed)

最佳答案

主机 email-smtp.us-east-1.amazonaws.com 端口 587 使用未加密的 ESMTP(或许可以将其称为“TCP”)。它支持通过 STARTTLS 命令协商加密 ESMTP。

手动测试一下,我发现它按预期工作。

您粘贴的日志中的错误(TLS 已处于事件状态)表明您有一个已协商 TLS 的连接(因为 STARTTLS 是通过 TCP 连接使用的,或者是因为您连接了到不同的服务器,其中 TLS 在连接开始时自动协商)。

服务器拒绝运行 TLS over TLS over TCP,这可能是明智的。但是,从您粘贴的代码中,我看不出 TLS 如何协商两次。也许如果您可以提供有关上下文的更多详细信息,答案就会变得清晰。

可能遇到http://tm.tl/3989 。如果是这种情况,升级到 Twisted 13.0.0 或更高版本将解决该问题。但是,我不明白为什么会出现这种情况,因为我不明白您的代码如何协商 TLS 两次。

实际上,经过进一步调查,您似乎正在经历 http://tm.tl/3989引入的回归。 。我已提交http://tm.tl/6524来跟踪这个。

关于python - Twisted 和 smtp tls 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16698223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com