-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding notifications and foundations for branded emails
- Loading branch information
Sascha Dobbelaere
committed
Nov 28, 2023
1 parent
5016320
commit 27f2661
Showing
5 changed files
with
92 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from django.core.mail import EmailMessage, EmailMultiAlternatives | ||
from django.conf import settings | ||
from django.utils.html import strip_tags | ||
from django.template.loader import render_to_string | ||
|
||
from premailer import transform | ||
from collections import namedtuple | ||
from get_absolute_url.helpers import generate_absolute_url | ||
|
||
import re | ||
|
||
|
||
EmailAttachment = namedtuple('EmailAttachment', 'name, content') | ||
|
||
|
||
def textify(html): | ||
# Remove html tags and continuous whitespaces | ||
text_only = re.sub('[ \t]+', ' ', strip_tags(html)) | ||
# Strip single spaces in the beginning of each line | ||
return text_only.replace('\n ', '\n').strip() | ||
|
||
|
||
def send_branded_mail(subject, html, to_email, from_email=None, fail_silently=True, attachment=None, **kwargs): | ||
''' | ||
Send a branded email with all signatures in place. | ||
''' | ||
|
||
if not from_email: | ||
from_email = settings.DEFAULT_FROM_EMAIL | ||
|
||
text = textify(html) | ||
html = transform(html, base_url=generate_absolute_url()) | ||
|
||
mail = EmailMultiAlternatives(subject, text, settings.EMAIL_HOST_USER, [to_email]) | ||
mail.attach_alternative(html, "text/html") | ||
|
||
if attachment: | ||
if not isinstance(attachment, EmailAttachment): | ||
raise ValueError(f'Attachment should be an EmailAttachment instance, not {type(attachment)}') | ||
|
||
extension = attachment.name.split('.')[-1] | ||
|
||
if extension in ['xlsx']: | ||
content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | ||
elif extension in ['pdf']: | ||
content_type = 'application/pdf' | ||
else: | ||
raise ValueError('Unkown content type.') | ||
|
||
mail.attach(attachment.name, attachment.content, content_type) | ||
|
||
# Lets override fail_silently for dev-env | ||
if settings.DEBUG: | ||
fail_silently = False | ||
|
||
return mail.send(fail_silently=fail_silently) | ||
|
||
# return send_mail(subject, text, from_email, [to_email], html_message=html, fail_silently=fail_silently, **kwargs) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
from django.db import models | ||
|
||
# Create your models here. | ||
from core import models |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.dispatch import receiver | ||
from core.models.multi_tenant import MultiTenantUser | ||
from core.signals import registered, invite_sent, \ | ||
invite_accepted, disabled, enabled | ||
|
||
from notifications.flows.email import send_welcome_email_flow, \ | ||
send_user_invite_email_flow | ||
|
||
|
||
@receiver(registered, sender=MultiTenantUser) | ||
def notifications__email__welcome(sender, instance, **kwargs): | ||
send_welcome_email_flow(user=instance) | ||
|
||
|
||
@receiver(invite_sent, sender=MultiTenantUser) | ||
def notifications__email__invite(sender, instance, **kwargs): | ||
send_user_invite_email_flow(user=instance) |
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
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