Skip to content

Instantly share code, notes, and snippets.

@jonashaag
Last active June 14, 2021 14:10
Show Gist options
  • Save jonashaag/69ed72632e0780899755b592492756dd to your computer and use it in GitHub Desktop.
Save jonashaag/69ed72632e0780899755b592492756dd to your computer and use it in GitHub Desktop.
Fast Django development email backend (socket.getfqdn() slowness)
from django.core.mail.message import make_msgid
class FastMailBackendMixin:
"""Work around slow development email backend due to slow socket.getfqdn() call.
This simply uses "example.com" instead of your local machine's hostname.
"""
def send_messages(self, messages):
for message in messages:
if 'message-id' not in message.extra_headers:
message.extra_headers['Message-ID'] = make_msgid(domain='example.com')
return super().send_messages(messages)
from django.core.mail.backends.locmem import EmailBackend
class FastEmailBackend(FastMailBackendMixin, EmailBackend):
pass
# settings.py:
EMAIL_BACKEND = 'yourapp.fast_email_backend.FastEmailBackend'
from django.test import TestCase, override_settings
@override_settings(EMAIL_BACKEND='yourapp.fast_email_backend.FastEmailBackend')
class XYZTest(TestCase):
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment