我开始了一个小的Raspberry项目,并坚持发送一个简单的电子邮件。 Gmail smtp已设置完毕。代码在Thonny中运行良好(虽然它只为第一个接收器发送E-Mal而不是两者),但是当我从终端(python report.py)触发它时,我得到以下错误:
Traceback (most recent call last):
File "report.py", line 15, in <module>
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
TypeError: __init__() got an unexpected keyword argument 'context'
正如我上面提到的,来自Thonny它正在发挥作用。我打算通过Crontab计划触发一些这样的脚本。我设置了Crontab文件,但它什么也没做,我假设因为它每次都会出错。
提前感谢您的帮助!
report.py内容:
import smtplib, ssl
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "****@gmail.com"
receiver_email = "****@gmail.com,[email protected]"
password = "****"
message = """\
Subject: Hi there
This message is sent from Python."""
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
context
的smtplib.SMTP_SSL参数在Python 3.3中添加。
Thonny使用内置的Python3.7解释器,但许多类Unix操作系统的默认Python解释器是Python 2.x.使用Python 3显式调用脚本以防止错误:
python3 report.py