Testing django mail and attachment return empty

Viewed 328

I'm trying to test mails with attachment, I'm attaching the files something like this:

# snippet of send_pdf_mail
mail = EmailMessage(
        subject=subject,
        body=message,
        from_email=from_email,
        to=recipient_list,
    )
    dynamic_template_data.update({'subject': subject})
    mail.content_subtype = 'html'
    mail.dynamic_template_data = dynamic_template_data
    mail.template_id = dynamic_template_id

    if attachment:
        attachment.open()
        mail.attach(basename(attachment.name), attachment.read(), guess_type(attachment.name)[0])
        attachment.close()
    return mail.send(fail_silently=False)

then my test is something like this:

    f = open('tests/test.pdf', 'rb')
    user.pdf.save('test.pdf', File(f))
    f.close()

    send_pdf_mail(user)
    self.assertEqual(len(mail.outbox), 1)
    self.assertEqual(mail.outbox[0].to[0], user.email)

But when I try to check if there are attachment via:

print(mail.outbox[0].attachments)

It returns an empty list so I'm not sure why but I tested the code and I can confirm that this indeed includes an attachment when sending an e-mail.

0 Answers
Related