Last active
August 29, 2015 13:56
-
-
Save stephenmcd/9338962 to your computer and use it in GitHub Desktop.
An email backend for Django that opens each HTML email sent in a local webbrowser
This file contains hidden or 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 tempfile import NamedTemporaryFile | |
import webbrowser | |
from django.core.mail import EmailMultiAlternatives | |
class BrowsableEmailBackend(BaseEmailBackend): | |
def send_messages(self, email_messages): | |
for message in email_messages: | |
for body, content_type in getattr(message, "alternatives", []): | |
if content_type == "text/html": | |
self.open(body) | |
def open(self, body): | |
temp = NamedTemporaryFile(delete=False) | |
temp.write(body) | |
temp.close() | |
webbrowser.open("file://" + temp.name) | |
# Usage - settings.py: | |
# EMAIL_BACKEND = "django_browsable_email_backend.BrowsableEmailBackend" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment