Testing email sending


Question

Any tips on testing email sending? Other than maybe creating a gmail account, especially for receiving those emails?

I would like to, maybe, store the emails locally, within a folder as they are sent.

1
72
9/17/2018 7:40:56 AM

Accepted Answer

You can use a file backend for sending emails which is a very handy solution for development and testing; emails are not sent but stored in a folder you can specify!

35
9/16/2010 4:10:54 PM

Django test framework has some built in helpers to aid you with testing e-mail service.

Example from docs (short version):

from django.core import mail
from django.test import TestCase

class EmailTest(TestCase):
    def test_send_email(self):
        mail.send_mail('Subject here', 'Here is the message.',
            'from@example.com', ['to@example.com'],
            fail_silently=False)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Subject here')

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon