我正在尝试编写一个功能,该功能可以自动将电子邮件发送到特定帐户。运行代码时,我收到错误消息:
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)
答案 0 :(得分:3)
在模块搜索路径(包括当前工作目录)中,您自己的模块之一很可能实际上称为email
。这将导致Python代之以选择该模块,并且将从标准库中隐藏email
模块,从而导致导入错误。
将该模块重命名为其他名称,您应该会很好。