简体   繁体   English

使用python脚本发送电子邮件

[英]Send an email using python script

Today I needed to send email from a Python script. 今天我需要从Python脚本发送电子邮件。 As always I searched Google and found the following script that fits to my need. 我一如既往地搜索谷歌,发现以下脚本符合我的需要。

import smtplib

SERVER = "localhost"

FROM = "sender@example.com"
TO = ["user@example.com"] # 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. 提前谢谢,Nimmy。


changed the name to emailsendin .py. 将名称更改为emailsendin .py。 But I got the following error 但是我得到了以下错误

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

You've named your module the same as one of Python's internal modules. 您已将模块命名为与Python内部模块相同的模块。 When you import smtplib , it tries to import email , and finds your module instead of the internal one. 当您导入smtplib ,它会尝试导入email ,并找到您的模块而不是内部模块。 When two modules import one another, only the variables in each module visible before the both import statements will be visible to one another. 当两个模块相互导入时,只有在两个import语句之前可见的每个模块中的变量才会彼此可见。 Renaming your module will fix the problem. 重命名模块将解决问题。

You can see this, although it's slightly obscure, in the stack trace. 您可以在堆栈跟踪中看到这一点,尽管它有点模糊。 The import email.utils line from smtplib.py is calling your module, in "c:/Python26/email.py". import email.utils从线smtplib.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代码的工作目录可能不是一个好主意。

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. 如果是这样,请重命名为其他内容,并且应该解决您遇到的命名冲突。

You have managed to shadow the stdlib email module by calling your script email.py . 您已设法通过调用脚本email.py stdlib email模块。 Rename your script to something not in the stdlib and try again. 将脚本重命名为不在stdlib中的内容,然后重试。

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邮件服务器的方法,但如果您的计算机当前没有运行,则无法向其发送邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM