【问题标题】:Is there any way by which I can name the attached pdf file in flask mail?有什么方法可以在烧瓶邮件中命名附加的 pdf 文件?
【发布时间】:2019-11-08 12:04:36
【问题描述】:

我正在使用 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 机器上运行良好

【问题讨论】:

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


    【解决方案1】:

    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())

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 2022-10-06
      • 2018-07-05
      • 2023-03-07
      • 2019-11-27
      相关资源
      最近更新 更多