python - 使用 Python 脚本发送电子邮件
问题描述
我正在尝试编写一个自动向特定帐户发送电子邮件的功能。当我运行我的代码时,我收到错误消息:
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)
解决方案
很可能在模块搜索路径(包括当前工作目录)中您自己的模块之一实际上被称为email
. 这将导致 Python 取而代之的是选择该模块,并且它将email
从标准库中隐藏该模块,从而导致该导入错误。
将该模块重命名为其他名称,您应该会很好。
推荐阅读
- angular - 除非它是数组中的最后一项,否则选定的属性不起作用
- c# - 在 Razor 页面中调用 OnGet 中的方法
- excel - 使用 VBA 在 Excel 中更新查询
- python - 无法安装 pip 包(未找到元数据)
- python - 如何在 tox 中创建特定于架构的环境
- javascript - 在动态位置生成下拉菜单
- python - 我们如何仅在特定行中替换 NaN?
- algorithm - 有没有办法在这里摆脱第二个while循环?
- regex - Powershell w/split 以查找多个模式匹配
- matlab - 如何在 Matlab 中对 2 个图形之间的区域进行着色?