Skip to content

Instantly share code, notes, and snippets.

@stephenmcd
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenmcd/9338962 to your computer and use it in GitHub Desktop.
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
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