【发布时间】:2020-06-18 03:47:53
【问题描述】:
我想使用 Python 发送电子邮件。
有sendmail (Sending mail via sendmail from python),还有https://docs.python.org/3/library/smtplib.html。 它建议基于https://docs.python.org/3/library/email.message.html 构建消息,并有几个示例https://docs.python.org/3/library/email.examples.html#email-examples 从文件中读取消息内容:
# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
# Create a text/plain message
msg = EmailMessage()
msg.set_content(fp.read())
我试过了
msg.set_content(b"test message sent locally")
但这会导致TypeError: set_bytes_content() missing 2 required positional arguments: 'maintype' and 'subtype'。 https://docs.python.org/3/library/email.message.html#email.message.EmailMessage.set_content 似乎需要上下文管理器?
如何使用字符串来构造消息体?
【问题讨论】: