Last active
June 13, 2017 19:05
-
-
Save alexphelps/8eabe2f54b85a31308f8e0c372d322eb to your computer and use it in GitHub Desktop.
Premailer Example
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
import json | |
from premailer import Premailer | |
from django.conf import settings | |
from django.core.mail import EmailMultiAlternatives | |
from django.dispatch import receiver, Signal | |
from django.template import loader | |
@receiver(welcome_notification) | |
def send_welcome_notificatoin(sender, user_id, **kwargs): | |
user = get_object_or_404(User, pk=user_id) | |
user_email = user.email | |
context_dict = { | |
'user': user, | |
} | |
subject_template_name = 'email/welcome-notification-subject.txt' | |
subject = loader.render_to_string( | |
subject_template_name, context_dict | |
) | |
subject = ''.join(subject.splitlines()) | |
from_email = settings.SUPPORT_FROM_EMAIL | |
template_url = 'email/welcome-notification-email.html' | |
html_template = get_template(template_url) | |
html_content = Premailer( | |
html_template.render(context_dict), | |
base_url=settings.SITE_URL, | |
remove_classes=False, | |
strip_important=False | |
).transform() | |
email = EmailMultiAlternatives( | |
subject, | |
'', | |
from_email, | |
[user_email] | |
) | |
email.attach_alternative(html_content, 'text/html') | |
email.send() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment