Python 2:SMTPServerDisconnected:连接意外关闭 [英] Python 2: SMTPServerDisconnected: Connection unexpectedly closed

查看:534
本文介绍了Python 2:SMTPServerDisconnected:连接意外关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Python发送电子邮件时遇到了一个小问题:

I have a small problem with sending an email in Python:

#me == my email address
#you == recipient's email address
me = "some.email@gmail.com"
you = "some_email2@gmail.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Alert"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
html = '<html><body><p>Hi, I have the following alerts for you!</p></body></html>'

# Record the MIME types of both parts - text/plain and text/html.
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('aspmx.l.google.com')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

所以在此之前,我的程序没有给我错误,但是也没有给我发送电子邮件。现在,python给了我一个错误:

So before now, my program didn't give me an error, but it also didn't send me an email. And now python gives me an error:

SMTPServerDisconnected: Connection unexpectedly closed

我该如何解决?

推荐答案

TLDR :切换到通过TLS进行身份验证的连接。

TLDR: switch to authenticated connection over TLS.

最有可能是gmail服务器在执行数据命令后拒绝了连接(在这个阶段,他们非常讨厌这样做:)。实际的消息很可能是这样的:

Most probably the gmail server rejected the connection after the data command (very nasty of them to do so at this stage :). The actual message is most probably this one:

    retcode (421); Msg: 4.7.0 [ip.octets.listed.here      15] Our system has detected an unusual rate of
    4.7.0 unsolicited mail originating from your IP address. To protect our
    4.7.0 users from spam, mail sent from your IP address has been temporarily
    4.7.0 rate limited. Please visit
    4.7.0  https://support.google.com/mail/answer/81126 to review our Bulk Email
    4.7.0 Senders Guidelines. qa9si9093954wjc.138 - gsmtp

我怎么知道?因为我已经尝试过:)和 s.set_debuglevel(1)一起打印了SMTP对话,所以您可以直接看到问题所在。

How do I know that? Because I've tried it :) with the s.set_debuglevel(1), which prints the SMTP conversation and you can see firsthand what's the issue.

您在这里有两个选择:


  1. 继续使用该中继; 由Google解释,它仅是未加密的gmail-to-gmail,并且您必须通过其过程取消将IP列入黑名单

  1. Continue using that relay; as explained by Google, it's unencrypted gmail-to-gmail only, and you have to un-blacklist your ip through their procedure

最简单的选择是使用身份验证切换到TLS

The most fool-proof option is to switch to TLS with authentication

以下是更改后的来源的样子:

Here's how the changed source looks like:

# skipped your comments for readability
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

me = "some.email@gmail.com"
my_password = r"your_actual_password"
you = "some.email2@gmail.com"

msg = MIMEMultipart('alternative')
msg['Subject'] = "Alert"
msg['From'] = me
msg['To'] = you

html = '<html><body><p>Hi, I have the following alerts for you!</p></body></html>'
part2 = MIMEText(html, 'html')

msg.attach(part2)

# Send the message via gmail's regular server, over SSL - passwords are being sent, afterall
s = smtplib.SMTP_SSL('smtp.gmail.com')
# uncomment if interested in the actual smtp conversation
# s.set_debuglevel(1)
# do the smtp auth; sends ehlo if it hasn't been sent already
s.login(me, my_password)

s.sendmail(me, you, msg.as_string())
s.quit()

现在,如果尝试欺骗系统并使用其他(非gmail)地址,这将是a)要求您连接到其他主机名(gmail的某些MX记录),然后b)停止您并以列入黑名单的ip为由关闭连接,以及c)进行DNS,DKIM和反向许多其他对策,以确保您实际上控制了在MAIL FROM:地址中显示的域。

Now, if try to 'cheat' the system and send with a different (non-gmail) address it's gonna a) require you to connect to a different hostname (some of the MX records for gmail), then b) stop you and close the connection on the grounds of blacklisted ip, and c) do reverse DNS, DKIM and lots of other countermeasures to make sure you're actually in control of the domain you presented in the MAIL FROM: address.

最后,还有选项3)-使用任何其他电子邮件中继服务,还有很多不错的方法:)

Finally, there's also option 3) - use any other email relaying service, there are tons of good ones :)

这篇关于Python 2:SMTPServerDisconnected:连接意外关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆