python - 无法使用烧瓶邮件python将附件添加到邮件
问题描述
我正在使用烧瓶邮件通过 SMTP 发送邮件。邮件正在运行,但附件未添加到邮件中。我将文件路径存储在 db 中,然后获取文件路径并附加文件。下面是我的代码
msg = Message(
subject=mail_data['subject'],
recipients=mail_data['to'],
body=mail_data['content'],
sender=mail_data['fromEmail'],
cc=mail_data['cc'],
bcc=mail_data['bcc'],
html=None,
reply_to=None,
date=None,
charset=None,
extra_headers=None,
mail_options=None,
rcpt_options=None
)
for files in mail_data['attachment']:
msg.attach(files,mimetypes.guess_type(files))
mail.send(msg)
发送附件的格式是什么。附件是动态的,可以是任何类型的文件。
解决方案
我使用下面的代码修复了它
for files in mail_data['attachment']:
mime = magic.from_file(files, mime=True)
with open(files,'rb') as f:
msg.attach(filename=files, content_type=mime, data=f.read(), disposition=None, headers=None)
推荐阅读
- tensorflow - 超过使用限制后如何在 Google Colab 上再次使用 GPU?
- reactjs - 检查一个 React 组件是否被 forwardRef 包裹
- python - 带有 Docker 的 Tika 解析器 python 给出 RuntimeError:详细信息:无法启动 Tika 服务器
- python - 使用 BeautifulSoup 对 python 中的报纸文章进行精确的基于单词的网络抓取
- meteor - Meteor 在哪里存储它的节点副本?
- php - MediaLibrary Nova 关于 DB 中的表“Media”
- uml - 如何从功能程序中提取序列图?
- javascript - 创建带有变量字符串名称 Nodejs 的 Zip 文件
- python - 无法使用 tkinter 分隔列表框和按钮
- reactjs - 在外部包中使用主题提供者注入主题道具