通过电子邮件发送PDF

时间:2018-04-10 04:04:20

标签: python django python-3.x python-2.7 django-views

我使用djago easy pdf在我的应用程序中显示pdf,但现在我想通过EmailMessage通过电子邮件发送文件,但我不知道如何做到这一点。

这是我发送html的代码的一部分:

email_body = render_to_string(
    'mails/supplier_receipt_html.html',
    {'data': data, }
)
msg = email_body
headers = {'Reply-To': "contacto@comuna18.com"}
TO = 'mauricio.munguia@comuna18.com'
mail = EmailMessage(subject, msg, 'contacto@comuna18.com', [TO], headers=headers)
mail.content_subtype = "html"
mail.send()

1 个答案:

答案 0 :(得分:2)

您可以使用this

所以你会有类似的东西:

from django.core.files.base import ContentFile
from django.core.mail import EmailMultiAlternatives

msg = EmailMultiAlternatives('Your Subject','', "<from_email@x.com>", ["to_email@x.com", ])
html_content = "<p>Email with PDF file!</p>"
file_to_be_sent = ContentFile("your_pdf.pdf")
msg.attach_alternative(html_content, "text/html")
msg.attach("Your_Pdf.pdf", file_to_be_sent, "application/pdf")
msg.send()