有什么方法可以在 Flask 邮件中命名附加的 pdf 文件?
Is there any way by which I can name the attached pdf file in 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 机器上工作正常
文件名参数必须是 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())
我正在使用 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 机器上工作正常
文件名参数必须是 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())