【问题标题】:Catching SMTP exceptions in Python, on Amazon AWS在 Amazon AWS 上的 Python 中捕获 SMTP 异常
【发布时间】:2012-11-02 14:30:11
【问题描述】:

我正在使用 Python 和 smtplib 通过 Amazon SES 发送电子邮件。如何捕获特定的发送错误?

例如,Amazon SES 可能会告诉我“此地址已列入黑名单”或“您已超出您的费率”或“您已超出您的数量配额”,我想对这些消息采取行动。

我有一个捕捉黑名单的 sn-p(我认为),如下所示。我真的不知道如何调试这些东西,因为异常只会出现在重型环境中,如果我触发异常,我担心亚马逊会占用我的配额。

try:
    msg = EmailMultiAlternatives(subject, plain, from_address, [to_address])
    msg.attach_alternative(html, "text/html")
    msg.send()
except smtplib.SMTPResponseException as e:
    error_code,error_msg = e.smtp_code, e.smtp_error
    if error_code==554 and error_msg=='Message rejected: Address blacklisted.':
        # do appropriate action for blacklisting
    else:
        # do appropriate action for throttling
    else:
        # log any other SMTP exceptions

【问题讨论】:

  • 您尝试过 Amason SES 沙盒吗?
  • 还没有。我是那种“先写文档,后写代码”的程序员之一。

标签: python amazon-web-services smtp


【解决方案1】:

您可以使用Amazon SES Mailbox Simulator 生成黑名单错误。

向 blacklist@simulator.amazonses.com 发送电子邮件,以验证您的应用程序是否正确处理了由此产生的错误。如果您使用邮箱模拟器地址,您的退回统计信息不会受到影响。无论您的 Amazon SES 账户是处于沙盒模式还是生产模式,您都可以运行这些测试。

邮箱模拟器目前不允许您测试限制或配额异常。但是,您的异常处理代码应该足以处理这些异常。我建议您使用 find() 检查异常字符串,以适应对错误消息的任何添加。

if error_code == 554 and error_msg.find('Address blacklisted') >= 0:
    # handle blacklisting
else: 
    ...

作为参考,以下是您可以检查的一些 SMTP 响应:

  • 黑名单是“554 邮件被拒绝:地址被列入黑名单”
  • 未验证地址为“554 邮件被拒绝:电子邮件地址未验证。”
  • Exceeded Send Rate 是“454 Throttling failure: Maximum sent rate exceeded.”
  • Exceeded Quota 为“454 Throttling failure: Daily message quota exceeded.”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-10
    • 2018-06-17
    • 2011-12-26
    • 1970-01-01
    • 2013-06-04
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多