Send branded HTML emails and harden SMTP2GO webhooks.
Fix campaign stuck on sending under async queue, and stop UUID ValidationError when SMTP2GO tests send "Headers Unavailable".
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMultiAlternatives
|
||||
from django.template.loader import get_template
|
||||
|
||||
from contacts.models import Channel
|
||||
from messaging.services import one_click_unsubscribe_url, preferences_url
|
||||
from public.email_branding import email_brand_context, plain_text_to_email_html
|
||||
|
||||
# Reported back on SMTP2GO webhooks when this header is selected in webhook settings.
|
||||
MONICA_MESSAGE_HEADER = "X-Monica-Message-Id"
|
||||
@@ -28,15 +30,20 @@ def send_email(message) -> str:
|
||||
one_click_path = one_click_unsubscribe_url(str(contact.pk), Channel.EMAIL)
|
||||
prefs_url = f"{site}{prefs_path}" if site else prefs_path
|
||||
one_click_url = f"{site}{one_click_path}" if site else one_click_path
|
||||
body_with_unsub = (
|
||||
f"{body}\n\n---\n"
|
||||
f"Manage preferences: {prefs_url}\n"
|
||||
f"Unsubscribe from email: {one_click_url}"
|
||||
|
||||
ctx = email_brand_context(
|
||||
title=subject,
|
||||
content=body,
|
||||
content_html=plain_text_to_email_html(body),
|
||||
prefs_url=prefs_url,
|
||||
one_click_url=one_click_url,
|
||||
)
|
||||
text_content = get_template("emails/marketing_email.txt").render(ctx)
|
||||
html_content = get_template("emails/marketing_email.html").render(ctx)
|
||||
|
||||
email = EmailMultiAlternatives(
|
||||
subject=subject,
|
||||
body=body_with_unsub,
|
||||
body=text_content,
|
||||
from_email=settings.DEFAULT_FROM_EMAIL,
|
||||
to=[contact.email],
|
||||
headers={
|
||||
@@ -45,6 +52,7 @@ def send_email(message) -> str:
|
||||
MONICA_MESSAGE_HEADER: str(message.pk),
|
||||
},
|
||||
)
|
||||
email.attach_alternative(html_content, "text/html")
|
||||
email.send(fail_silently=False)
|
||||
# Placeholder until SMTP2GO webhook supplies the real email_id.
|
||||
return f"smtp-{message.pk}"
|
||||
|
||||
Reference in New Issue
Block a user