问题
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