【发布时间】: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