Send PDF as an email's attachment ( Working with xhtml2pdf library)

807 views
Skip to first unread message

Walter Randazzo

unread,
Apr 30, 2021, 7:13:21 PM4/30/21
to Django users
Hi Dudes, Im working with pisa from xhtml2pdf LIB in order to get a pdf. That works OK, but i cant figure out how to attach the generated pdf to  an email  in order to send it . Anyone dealt with this? I would appreciate any help on this!

View:

  def get(self,request,*args,**kwargs):
        try:
            mailServer = smtplib.SMTP(settings.EMAIL_HOST,settings.EMAIL_PORT)
            print(mailServer.ehlo())
            mailServer.starttls()
            print(mailServer.ehlo())
            mailServer.login(settings.EMAIL_HOST_USER,settings.EMAIL_HOST_PASSWORD)
            print("conectando...")
            email_to=[]
            email_to.append("emailto...@gmail.com")
            subject="ADICRA - Comprobrante de Pago"
            template= get_template('pagos/invoice.html')
            context={
                'sale': PagosHead.objects.get(pk=self.kwargs['pk']),
                'comp': {'name':'ADICRA','ruc':'Av. Directorio Adicra 101','address':'C.A.B.A.'},
                'icon': '{}{}'.format(settings.STATIC_URL,'core/img/adicrareng.jpg'),
                'equis':'{}{}'.format(settings.STATIC_URL,'pagos/img/x.jpg'),            
                }
            html=template.render(context)
            response= HttpResponse(content_type='application/pdf')
            #response['Content-Disposition']='attachment; filename="pago.pdf"' #Para Descargar
            pisaStatus=pisa.CreatePDF(html, dest=response,link_callback=self.link_callback)
            mensaje = EmailMessage(subject, body=pdf, from_email=settings.EMAIL_HOST_USER, to=email_to)
            print("Acá antes del attach")
            mensaje.attach('pago.pdf', pisaStatus,'application/pdf')
            mensaje.content_subtype = "pdf"  
            mensaje.encoding = 'us-ascii'
            mensaje.send()
            #mailServer.sendmail(settings.EMAIL_HOST_USER,
             #       email_to,
              #      mensaje.as_string())
            print("Correo enviado correcamente")
            return response
        except Exception as e:
            print(e)
        return HttpResponse(reverse_lazy('pagos:list'))

Sreebash Kumar Das

unread,
Oct 7, 2021, 2:49:36 PM10/7/21
to Django users
Did you solve it? I need a similar solution.

MR INDIA

unread,
Oct 8, 2021, 9:44:49 AM10/8/21
to Django users

Luis Zárate

unread,
Oct 25, 2021, 3:34:19 PM10/25/21
to django...@googlegroups.com

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7366b7b6-ae23-44cd-bceb-b7b340ecbefcn%40googlegroups.com.


--
"La utopía sirve para caminar" Fernando Birri


Reply all
Reply to author
Forward
0 new messages