Python smtplib 名称或服务未知

Python smtplib Name or service not known

我在 python 中制作了一个简单的守护进程,它接收邮件队列并将它们发送给收件人。除了 smtplib 这实际上是最重要的部分外,一切都很好。

会发生什么?

当我 运行 脚本出现以下错误时:

root@vagrant-ubuntu-trusty-64:/mailer/tests# python daemon_run.py
[Errno -2] Name or service not known

根据我在互联网上的发现,当它无法连接到 SMTP 服务器时会发生此错误。大多数用户建议对邮递员进行修复,但我没有使用,因为我利用了 google 的服务。

密码

headers = "\r\n".join(["from: " + "my_email@gmail.com",
                        "subject: " + "Testing",
                        "to: " + "recipient@gmail.com",
                        "mime-version: 1.0",
                        "content-type: text/html"])

content = headers + "\r\n\r\n" + template_content

server = smtplib.SMTP('smtp.google.com', 587)
server.ehlo()
server.starttls()
server.login('my_email@gmail.com', 'pass')
server.sendmail('my_email@gmail.com', 'recipient@gmail.com', content)
server.close()

请注意,我在 PHPMailer 中使用了完全相同的登录详细信息,这确实有效。

有什么想法吗?

看来老打字错误又犯了。 Gmail 的 SMTP 是 smtp.gmail.com 而不是 smtp.google.com