0

I'm trying to send a message to an email address with a pdf attachment. The letter doesn't come on mail. What's wrong in my code, please tell me?

base_path = 'http://127.0.0.1:8000/'

t = render_to_string(
    'pdf_template_nds.html',
    {
        'payment': payment,
        'date_pdf': date_pdf,
        'count': count,
        'sum_pdf': sum_pdf,
        'sum_final': sum_final,
        'sum_final_capitalize': sum_final_capitalize,
        'sum_nds': sum_nds,
    }
)
HTML(string=t, base_url=base_path).write_pdf(
    f'nds.pdf',
    presentational_hints=True
)


email = EmailMultiAlternatives(
    'Subject here', 'Here is the message.', 'eliz.moon5@gmail.com',
    ['elizzz.ekzo@mail.ru'])
email.attach_file('nds.pdf')
email.send()
Ekzo
  • 103
  • 1
  • 9
  • Does this answer your question? [In Django, how do I save a file that has been uploaded in memory as an email attachment?](https://stackoverflow.com/questions/47205196/in-django-how-do-i-save-a-file-that-has-been-uploaded-in-memory-as-an-email-att) – ewokx Oct 09 '20 at 08:12
  • @ewong No, I don't get this file from request, I create it myself in the form handler – Ekzo Oct 09 '20 at 08:15
  • I don't think you're using the ```attach()``` method right. Please read https://docs.djangoproject.com/en/1.11/topics/email/#emailmessage-objects. The parameter should either be a email.MIMEBase.MIMEBase instance, or you need to have three parameters ```filename, content``` and ```mimetype``` – ewokx Oct 09 '20 at 08:24
  • @ewong email.attach('nds.pdf', content='application/pdf') I'm doing that, there is no error, but the message does not arrive at all – Ekzo Oct 09 '20 at 08:32
  • Please read the documentation. in your case, it should be ```email.attach('nds.pdf', content=open('nds.pdf', 'rb'), mimetype='application/pdf')``` Though, I think you can also use ```attach_file()``` – ewokx Oct 09 '20 at 08:34
  • @ewong Same. Strange, the letter doesn't come ... `email.attach_file('nds.pdf', mimetype='application/pdf'` – Ekzo Oct 09 '20 at 08:43
  • ```attach_file()``` has only one parameter and that is the filepath. – ewokx Oct 09 '20 at 08:44
  • @ewong I don't understand anything. The same, letter doesn't come – Ekzo Oct 09 '20 at 08:51

0 Answers0