Last active
June 14, 2021 14:10
-
-
Save jonashaag/69ed72632e0780899755b592492756dd to your computer and use it in GitHub Desktop.
Fast Django development email backend (socket.getfqdn() slowness)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.mail.backends.locmem import EmailBackend | |
class FastEmailBackend(FastMailBackendMixin, EmailBackend): | |
pass | |
# settings.py: | |
EMAIL_BACKEND = 'yourapp.fast_email_backend.FastEmailBackend' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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