The sending of an attachment in pdf format under django.

250 views
Skip to first unread message

Bienvenue Emmanuel

unread,
Apr 26, 2018, 7:09:34 AM4/26/18
to Django users
Hello ! I hope you are well...
I am working on project of registration in a school where I must recover, put the information entered by the user in a template and be able to translate this template into pdf in the end send the generated PDF file as an attachment of a mail that I send back to the user. Until then the other processes work, but after sending the email when I consult my mailbox on my phone I find the mail sent with the attachment on the other hand when I consult my mailbox on my PC or even with a browser (even on) I can not find the attachment. I have never encountered this kind of hard-to-understand error. Help me please.

def pdf_genarete(request):

form = MyModelForm(request.POST or None)

if form.is_valid():

nom = form.cleaned_data.get("nom")
email = form.cleaned_data.get("email")
obj = MyModel.objects.create(nom=nom, email=email)
print(obj.nom)
print(obj.email)

context = {"models_instance": obj}

pdf = render_to_pdf("pdfapp/template_pdf.html", context)
filename = "mypdf_{}.pdf".format(obj.order_id)
obj.pdf.save(filename, BytesIO(pdf.content))
# print(obj.pdf.name, obj.pdf.size)

subject = "Thank you"
from_email = settings.EMAIL_HOST_USER
to_email = [obj.email]
body = "Votre inscription"

email_pdf = EmailMultiAlternatives(

subject = subject,
body =body,
from_email = from_email,
to=to_email,
)

instance_attach = obj.pdf.read()
# print(instance.pdf.name, instance.pdf.size)
email_pdf.attach_alternative(instance_attach, "application/pdf")
# import pdb;pdb.set_trace()

email_pdf.send()

return redirect(reverse("home"))

return render(request, "pdfapp/formulaire.html", {"form": form})

Jason

unread,
Apr 27, 2018, 7:22:27 AM4/27/18
to Django users
That suggests its an issue with your email reader.  What are you using, gmail or something else?
Reply all
Reply to author
Forward
0 new messages