Friday, April 27, 2018

Re: The sending of an attachment in pdf format under django.

That suggests its an issue with your email reader.  What are you using, gmail or something else?

On Thursday, April 26, 2018 at 7:09:34 AM UTC-4, Bienvenue Emmanuel wrote:
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})

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/19e984ac-02bf-482e-ae6c-a9ed2ef0d42b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment