Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Email Backend Setting Not Working

I have a unit test with a test-specific settings file, which includes:

EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/my/file/path'

This wasn't working, so I dropped into the debugger to check the settings in the middle of running my test:

ipdb> from django.conf import settings
ipdb> settings.EMAIL_BACKEND
'django.core.mail.backends.locmem.EmailBackend'
ipdb> settings.EMAIL_FILE_PATH
'/my/file/path'

The file path setting worked, but the backend setting didn't!

Does anyone know why?

What else could I check/configure?

Is this something for a bug report?

Django 1.11

like image 483
Han Lazarus Avatar asked Oct 24 '25 03:10

Han Lazarus


1 Answers

This is documented behaviour. Django replaces the regular email backend with a dummy one. You then access the "sent" emails in your tests with mail.outbox. See the docs for more info.

I believe you might be able to override the EMAIL_BACKEND for a single test or testcase with override_settings

from django.test import TestCase, override_settings

class MyTest(TestCase):
    @override_settings(EMAIL_BACKEND='django.core.mail.backends.filebased.EmailBackend')
    def test_email(self):
        ...
like image 83
Alasdair Avatar answered Oct 25 '25 17:10

Alasdair