有什么方法可以在烧瓶邮件中命名附加的pdf文件?

时间:2019-11-08 12:04:36

标签: python-3.x flask ubuntu-14.04 flask-mail

我正在使用flask-mail发送包含pdf文档作为附件的邮件, 问题是,当我在电子邮件中收到该文件时,该文件的名称竟然是“ noname”(不带任何扩展名),即使该文件是可下载且可读的,在某些特殊情况下(可能是十分之一),该文件也无法被打开(可能是因为未设置扩展名)。

def send_mail_flask_to_user(self, doc_name, document_id, email,approve):
    app.config.update(self.mail_settings)
    mail = Mail(app)
    with app.app_context():
        msg = Message(sender=app.config.get(
            "MAIL_USERNAME"), recipients=[email])
        msg.subject = '{} Coriolis Tech'.format(doc_name)
        msg.body=""" Dear {},
                     your request for {} has be approved and generated,please find the attachment for the same""".format(email.split('.')[0],doc_name)
        working_dir=os.getcwd()
        link = "https://docs.google.com/document/d/{}/".format(document_id)
        if (approve==True):
            with open(working_dir+"/generated_docs/"+document_id+".pdf",'rb') as fh:
                msg.attach(working_dir+"/"+document_id+".pdf","application/pdf",fh.read())
        elif(approve==False):
            msg.body=""" Your {} is rejected ,contact Division of Human Resource Coriolis Technologies to know more""".format(doc_name)
        mail.send(msg)

此问题仅在Ubuntu 14.04上仍然存在,因为它在Windows计算机上可以正常工作Screenshot of received email

1 个答案:

答案 0 :(得分:0)

filename参数必须是string.extension(例如:“ something.pdf”)以命名将要接收的附件 例如:您要附加的“ abcdcdhhcbdhbcvh.pdf”文件在附件中将被命名为“ something.pdf” 因此,将msg.attach(working_dir+"/"+document_id+".pdf","application/pdf",fh.read())行替换为msg.attach(filename="something.pdf",disposition="attachment",content_type="application/pdf",data=fh.read())