运行单元测试时,电子邮件作为EmailMessage对象存储在^{}的列表中,然后可以在测试类中执行任何检查。下面是django's docs中的一个示例。在from django.core import mail
from django.test import TestCase
class EmailTest(TestCase):
def test_send_email(self):
# Send message.
mail.send_mail('Subject here', 'Here is the message.',
'from@example.com', ['to@example.com'],
fail_silently=False)
# Test that one message has been sent.
self.assertEqual(len(mail.outbox), 1)
# Verify that the subject of the first message is correct.
self.assertEqual(mail.outbox[0].subject, 'Subject here')
或者,如果您只想在开发期间目视检查电子邮件的内容,可以将^{}设置为:
^{pr2}$
然后看看你的控制台。在
或者EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location
然后检查文件。在