Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify mail was sent from unit tests

Tags:

I have some unit tests in my Django 1.2.4 app. One of the tests needs to verify that emails are sent under certain conditions. How can I do this?

Ideally it'd be programmatic, but it'd also be nice to print the email body out to the console.

like image 247
Nick Heiner Avatar asked Jan 10 '11 21:01

Nick Heiner


1 Answers

The latest Django testing documentation has an Email Services section.

Then you just do something like:

from django.core import mail  print mail.outbox[0].body 

The HTML version is handled via attached alternative mimetypes, which you can access (if you attached one) such as

content, mimetype = mail.outbox[0].alternatives[0] 

Outbox objects are EmailMessages, further documented here

like image 197
Dominique Guardiola Avatar answered Oct 04 '22 02:10

Dominique Guardiola