【发布时间】:2017-03-15 03:46:37
【问题描述】:
我正在尝试捕获所有在 Python 中通过 smtplib 发送时退回的电子邮件。我查看了这个建议添加异常捕获器的similar post,但我注意到我的sendmail 函数即使对于假电子邮件地址也不会抛出任何异常。
这是我的send_email 函数,它使用smtplib。
def send_email(body, subject, recipients, sent_from="myEmail@server.com"):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sent_from
msg['To'] = ", ".join(recipients)
s = smtplib.SMTP('mySmtpServer:Port')
try:
s.sendmail(msg['From'], recipients, msg.as_string())
except SMTPResponseException as e:
error_code = e.smtp_code
error_message = e.smtp_error
print("error_code: {}, error_message: {}".format(error_code, error_message))
s.quit()
示例调用:
send_email("Body-Test", "Subject-Test", ["fakejfdklsa@jfdlsaf.com"], "myemail@server.com")
由于我将发件人设置为我自己,我可以在我的发件人收件箱中收到电子邮件退回报告:
<fakejfdklsa@jfdlsaf.com>: Host or domain name not found. Name service error
for name=jfdlsaf.com type=A: Host not found
Final-Recipient: rfc822; fakejfdklsa@jfdlsaf.com
Original-Recipient: rfc822;fakejfdklsa@jfdlsaf.com
Action: failed
Status: 5.4.4
Diagnostic-Code: X-Postfix; Host or domain name not found. Name service error
for name=jfdlsaf.com type=A: Host not found
有没有办法通过 Python 获取退回消息?
【问题讨论】:
-
你有解决办法吗?
-
也许使用 poplib 打开您的邮箱,您的退回报告将被发送到?
标签: python email smtp mime smtplib