views:

68

answers:

4

Hi folks,

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.


Tips would be great! Thanks :)

+4  A: 

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!

lazerscience
+1  A: 

Patching SMTPLib for testing purposes can help test sending mails without sending them.

pyfunc
+1  A: 

For any project that doesn't require sending attachments, I use django-mailer, which has the benefit of all outbound emails ending up in a queue until I trigger their sending, and even after they've been sent, they are then logged - all of which is visible in the Admin, making it easy to quickly check what you emailing code is trying to fire off into the intertubes.

stevejalim
Further to that, the Message objects created by django-mailer mean you can prod them (and inspect their contents) in unit tests too (I know that there's outbound mailbox support in the test suite for a dummy mailbox, but using django-mailer doesn't send mail unless the management command sends it, which means you can't use that mailbox object)
stevejalim
+1  A: 

Django test framework has some built in helpers 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.',
            '[email protected]', ['[email protected]'],
            fail_silently=False)
        self.assertEquals(len(mail.outbox), 1)
        self.assertEquals(mail.outbox[0].subject, 'Subject here')
rebus