Django - Attach PDF to an e-mail which was generat

2019-09-08 16:32发布

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条回答
Luminary・发光体
2楼-- · 2019-09-08 17:11

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()
查看更多
登录 后发表回答