我正在使用 Flask-mail 发送一封邮件,其中包含一个 pdf 文档作为附件,
问题是当我在电子邮件中收到它时,文档的名称原来是“noname”(没有任何扩展名),尽管该文档可下载且可读,但仍有一些异常(exception)情况(可能是十分之一)文档不能被打开(可能是因为未设置扩展名)。
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(eg: "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())
关于python-3.x - 有什么方法可以在 flask 邮件中命名附加的pdf文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58766257/