Django-将PDF附加到由视图生成的电子邮件中 [英] Django - Attach PDF to an e-mail which was generated by a view

查看:87
本文介绍了Django-将PDF附加到由视图生成的电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图,该视图生成PDF文件并将其作为HtmlResponse返回。然后合适的URL显示该文件。我希望能够将该pdf附加到电子邮件中。哪种方法是最好的方法?

I have a view which generates a PDF file and returns it as a HtmlResponse. Then the suitable URL shows that file. I want to be able to attach that pdf to an e-mail. Which is the best way to do this?

任何提示或示例都值得赞赏!

Any tips or examples are appreciated!

推荐答案

您可以使用 EmailMessage.attach()

You can use EmailMessage.attach()

假设您有方法 create_pdf 创建PDF,您应该可以执行以下操作:

Assuming you have a method create_pdf that creates your PDF, you should be able to do something like this:

pdf_content = create_pdf()
email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
        ['to@example.com'])
email.attach('document.pdf', pdf_content, 'application/pdf')
email.send()

这篇关于Django-将PDF附加到由视图生成的电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆