首页 > 解决方案 > 无法使用烧瓶邮件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)

发送附件的格式是什么。附件是动态的,可以是任何类型的文件。

标签: pythonemailflasksmtpflask-mail

解决方案


我使用下面的代码修复了它

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) 

推荐阅读