【发布时间】:2021-05-30 19:07:58
【问题描述】:
所以我使用https://realpython.com/python-send-email/#sending-a-plain-text-email
我正在使用这段代码
import smtplib, ssl
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "my@gmail.com" # Enter your address
receiver_email = "your@gmail.com" # Enter receiver address
password = input("Type your password and press enter: ")
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)
我已经更改了一些我需要的东西的密码和邮件,但是当我发送电子邮件时,我收到了这个错误
File "main.py", line 32, in sendmail
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
TypeError: __init__() got an unexpected keyword argument 'context'
当我在一个普通的 .py 文件中尝试此代码时,它可以工作,但是当我将它与 bottle.py 一起使用时,它给了我这个错误,我不认为它是因为其他包,任何帮助都会非常感激
【问题讨论】:
-
你试过只用
context而不是context=context -
如果我这样做,我会得到这个imgur.com/MXN25U6
-
山姆的回答是正确的。使用
context而不是context=context。您的后续错误是您的代码的一个新问题,您应该提出一个新问题。 (当你这样做时,请在该问题中包含整个堆栈跟踪。) -
这里正确的做法是根本不传递
context参数,因为SMTP_SSL直到Python 3.3 才使用上下文,而提问者使用的是Python 2.7。