【问题标题】:Sending email using Python Script [closed]使用 Python 脚本发送电子邮件 [关闭]
【发布时间】:2020-06-12 18:24:45
【问题描述】:

我正在尝试编写一个可以自动向特定帐户发送电子邮件的功能。当我运行我的代码时,我收到错误消息:

Email could not send
Traceback (most recent call last):
  File "email.py", line 1, in <module>
    import smtplib
  File "C:\Users\User\Anaconda3\lib\smtplib.py", line 47, in <module>
    import email.utils
ModuleNotFoundError: No module named 'email.utils'; 'email' is not a package

这是我写的代码:

import smtplib
import logindetails
def send_email(subject, msg):
    try:
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login(logindetails.EMAIL_ADDRESS, logindetails.PASSWORD)
        message = 'Subject: {}\n\n{}'.format(subject, msg)
        server.sendmail(logindetails.EMAIL_ADDRESS, logindetails.EMAIL_ADDRESS, message)
        server.quit()
        print('Email sent successfully')
    except:
        print('Email could not send')

subject = 'Testing'
msg = 'Hello there how are you today'
send_email(subject, msg)

【问题讨论】:

  • File "email.py", line 1, in &lt;module&gt; 你的脚本被称为email.py 这使得smtplib.py 无法导入真正的email.py :)

标签: python smtplib


【解决方案1】:

很可能在模块搜索路径(包括当前工作目录)中您自己的模块之一实际上称为email。这将导致 Python 取而代之的是选择该模块,并且它将隐藏标准库中的 email 模块,从而导致导入错误。

将该模块重命名为其他名称,这样就可以了。

【讨论】:

  • 不只是将您的文件名重命名为 email.py 以外的其他名称
猜你喜欢
  • 2020-11-03
  • 1970-01-01
  • 2015-01-16
  • 2018-06-26
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 2013-09-16
相关资源
最近更新 更多