发送的附件
with open(‘log.txt’, ‘rb’) as f:
send_att = f.read()
att = MIMEText(send_att, ‘text’, ‘utf-8’)
att[“Content-Type”] = ‘application/octet-stream’
att[“Content-Disposition”] = ‘attachment; filename=“log.txt”’
msg = MIMEMultipart()
msg[‘Subject’] = subject
msg.attach(att)
发送邮件
smtp = smtplib.SMTP()
smtp.connect(“smtp.126.com”)
smtp.login(“sender@126.com”, “a123456”)
smtp.sendmail(“sender@126.com”, “receiver@126.com”, msg.as_string())
smtp.quit()
首先,读取附件的内容。通过 MIMEText 类,定义发送邮件的正文、格式,以及编码;
Content-Type 指定附件内容类型;application/octet-stream 表示二进制流;Content-Disposition
指定显示附件的文件;attachment; filename="log.txt"指定附件的文件名。
然后,使用 MIMEMultipart 类定义邮件的主题,attach()指定附件信息。
最后,通过 smtplib 模块发送邮件,发送过程与第一个例子相同。
## 用 yagmail 发送邮件
yagmail 是 Python 的一个第三方库,可以让我们以非常简单的方法实现自动发送邮件
功能。
GitHub 项目地址: <https://github.com/kootenpv/yagmail>。
通过 pip 命令安

1704

被折叠的 条评论
为什么被折叠?



