1
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)
4

1 回答 1

1

主机 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来跟踪这个。

于 2013-05-22T19:38:29.887 回答