Django - Attach PDF to an e-mail which was generated by a view

ぐ巨炮叔叔 提交于 2019-12-13 06:00:56

问题


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!


回答1:


You can use EmailMessage.attach()

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


来源:https://stackoverflow.com/questions/31695742/django-attach-pdf-to-an-e-mail-which-was-generated-by-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!