python 使用python脚本发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2482160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:45:12  来源:igfitidea点击:

Send an email using python script

pythonemail

提问by Nimmy

Today I needed to send email from a Python script. As always I searched Google and found the following script that fits to my need.

今天我需要从 Python 脚本发送电子邮件。和往常一样,我在 Google 上搜索并找到了适合我需要的以下脚本。

import smtplib

SERVER = "localhost"

FROM = "[email protected]"
TO = ["[email protected]"] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

But when I tried to run the program, I got the following error message:

但是当我尝试运行该程序时,我收到以下错误消息:

Traceback (most recent call last):
  File "C:/Python26/email.py", line 1, in <module>
    import smtplib
  File "C:\Python26\lib\smtplib.py", line 46, in <module>
    import email.utils
  File "C:/Python26/email.py", line 24, in <module>
    server = smtplib.SMTP(SERVER)
AttributeError: 'module' object has no attribute 'SMTP'

How can i solve this problem? Any one can help me?

我怎么解决这个问题?任何人都可以帮助我吗?

Thanks in advance, Nimmy.

提前致谢,尼米。



changed the name to emailsendin .py. But I got the following error

将名称更改为 emailsendin .py。但我收到以下错误

Traceback (most recent call last):
  File "C:\Python26\emailsending.py", line 24, in <module>
    server = smtplib.SMTP(SERVER)
  File "C:\Python26\lib\smtplib.py", line 239, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python26\lib\smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python26\lib\smtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "C:\Python26\lib\socket.py", line 512, in create_connection
    raise error, msg
error: [Errno 10061] No connection could be made because the target machine actively refused it

回答by Chris B.

You've named your module the same as one of Python's internal modules. When you import smtplib, it tries to import email, and finds your module instead of the internal one. When two modules import one another, only the variables in each module visible before the both import statements will be visible to one another. Renaming your module will fix the problem.

您已将模块命名为与 Python 的内部模块之一相同的名称。当您 import 时smtplib,它会尝试 importemail并找到您的模块而不是内部模块。当两个模块相互导入时,只有在两个导入语句之前可见的每个模块中的变量才会相互可见。重命名您的模块将解决问题。

You can see this, although it's slightly obscure, in the stack trace. The import email.utilsline from smtplib.pyis calling yourmodule, in "c:/Python26/email.py".

您可以在堆栈跟踪中看到这一点,尽管它有点模糊。从import email.utilssmtplib.py调用您的模块,在“c:/Python26/email.py”中。

Another note: it's probably not a great idea to use your Python install directory as your working directory for Python code.

另一个注意事项:使用 Python 安装目录作为 Python 代码的工作目录可能不是一个好主意。

回答by Chris Ballance

Did you name your script email.py?

你给你的脚本命名了email.py吗?

If so, rename to something else and the naming conflict you're encountering should be solved.

如果是这样,请重命名为其他名称,您遇到的命名冲突应该可以解决。

回答by Ignacio Vazquez-Abrams

You have managed to shadow the stdlib emailmodule by calling your script email.py. Rename your script to something not in the stdliband try again.

您已经email通过调用您的脚本成功地隐藏了 stdlib模块email.py。将您的脚本重命名为不在stdlib 中的内容,然后重试。

回答by Murphy

Does your computer also have an open mail server listening on the default port? smptlib provides methods to connect to an smtp mail server, but if your computer is not currently running one, then you cannot send mail to it.

您的计算机是否也有在默认端口上侦听的开放邮件服务器?smptlib 提供了连接到 smtp 邮件服务器的方法,但如果您的计算机当前没有运行该服务器,则您无法向其发送邮件。