Search code examples
pythondjangodjango-testing

Django: Test content of email sent


I'm sending out an email to users with a generated link, and I wanna write a test that verifies whether the link is correct, but I can't find a way to get the content of the email inside the test.

Is there a way to do that?

If it helps at all, this is how I'm sending the email:

content = template.render(Context({'my_link': my_link}))
subject = _('Email with link')
msg = EmailMultiAlternatives(subject=subject,
                             from_email='MyWebsite Team <[email protected]>',
                             to=[user.email, ])
msg.attach_alternative(content, 'text/html')
msg.send()

Solution

  • The docs have an entire section on testing emails.

    self.assertEqual(mail.outbox[0].subject, 'Email with link')