0

How to send an email with pdf or attachment and html using django

from django.core.mail import EmailMessage

email_template =render_to_string('accounts/email.html')

send_mail=EmailMessage('Your Enrollment: ',email_template,  [settings.EMAIL_HOST_USER, Email] , [Email])
send_mail.attach_file('static/pdf/ENROLLMENT-PROCEDURE-2021-.pdf')
send_mail.send()
print(send_mail)

this is the email I received

enter image description here

user14823468
  • 101
  • 13
  • 1
    Try `send_mail.send(fail_silently=False)` to make sure that exceptions raised while sending the message won't be quashed. More details in https://docs.djangoproject.com/en/3.2/topics/email/#emailmessage-objects – jonathadv May 04 '21 at 12:26

1 Answers1

0

you can send an email with an attachment without saving it in the database, see Django EmailMessage.attach() description. To send a PDF as an attachment simply add it to an email (Sending emails with attachment in django):

mail.attach('my_pdf_filename.pdf', my_pdf.read(), 'application/pdf')
Gajanan
  • 454
  • 4
  • 11