stages/email: fix issue when sending emails to users with same display as email (#8850)
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
@ -605,7 +605,7 @@ class UserViewSet(UsedByMixin, ModelViewSet):
|
|||||||
email_stage: EmailStage = stages.first()
|
email_stage: EmailStage = stages.first()
|
||||||
message = TemplateEmailMessage(
|
message = TemplateEmailMessage(
|
||||||
subject=_(email_stage.subject),
|
subject=_(email_stage.subject),
|
||||||
to=[for_user.email],
|
to=[(for_user.name, for_user.email)],
|
||||||
template_name=email_stage.template,
|
template_name=email_stage.template,
|
||||||
language=for_user.locale(request),
|
language=for_user.locale(request),
|
||||||
template_context={
|
template_context={
|
||||||
|
|||||||
@ -481,7 +481,7 @@ class NotificationTransport(SerializerModel):
|
|||||||
}
|
}
|
||||||
mail = TemplateEmailMessage(
|
mail = TemplateEmailMessage(
|
||||||
subject=subject_prefix + context["title"],
|
subject=subject_prefix + context["title"],
|
||||||
to=[f"{notification.user.name} <{notification.user.email}>"],
|
to=[(notification.user.name, notification.user.email)],
|
||||||
language=notification.user.locale(),
|
language=notification.user.locale(),
|
||||||
template_name="email/event_notification.html",
|
template_name="email/event_notification.html",
|
||||||
template_context=context,
|
template_context=context,
|
||||||
|
|||||||
@ -30,7 +30,7 @@ class Command(TenantCommand):
|
|||||||
delete_stage = True
|
delete_stage = True
|
||||||
message = TemplateEmailMessage(
|
message = TemplateEmailMessage(
|
||||||
subject="authentik Test-Email",
|
subject="authentik Test-Email",
|
||||||
to=[options["to"]],
|
to=[("", options["to"])],
|
||||||
template_name="email/setup.html",
|
template_name="email/setup.html",
|
||||||
template_context={},
|
template_context={},
|
||||||
)
|
)
|
||||||
|
|||||||
@ -111,7 +111,7 @@ class EmailStageView(ChallengeStageView):
|
|||||||
try:
|
try:
|
||||||
message = TemplateEmailMessage(
|
message = TemplateEmailMessage(
|
||||||
subject=_(current_stage.subject),
|
subject=_(current_stage.subject),
|
||||||
to=[f"{pending_user.name} <{email}>"],
|
to=[(pending_user.name, email)],
|
||||||
language=pending_user.locale(self.request),
|
language=pending_user.locale(self.request),
|
||||||
template_name=current_stage.template,
|
template_name=current_stage.template,
|
||||||
template_context={
|
template_context={
|
||||||
|
|||||||
@ -39,6 +39,7 @@ class TestEmailStageSending(FlowTestCase):
|
|||||||
session = self.client.session
|
session = self.client.session
|
||||||
session[SESSION_KEY_PLAN] = plan
|
session[SESSION_KEY_PLAN] = plan
|
||||||
session.save()
|
session.save()
|
||||||
|
Event.objects.filter(action=EventAction.EMAIL_SENT).delete()
|
||||||
|
|
||||||
url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug})
|
url = reverse("authentik_api:flow-executor", kwargs={"flow_slug": self.flow.slug})
|
||||||
with patch(
|
with patch(
|
||||||
|
|||||||
@ -9,6 +9,7 @@ from unittest.mock import PropertyMock, patch
|
|||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.mail.backends.locmem import EmailBackend
|
from django.core.mail.backends.locmem import EmailBackend
|
||||||
|
from django.core.mail.message import sanitize_address
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
from authentik.core.tests.utils import create_test_admin_user, create_test_flow
|
from authentik.core.tests.utils import create_test_admin_user, create_test_flow
|
||||||
@ -19,6 +20,7 @@ from authentik.flows.planner import PLAN_CONTEXT_PENDING_USER, FlowPlan
|
|||||||
from authentik.flows.tests import FlowTestCase
|
from authentik.flows.tests import FlowTestCase
|
||||||
from authentik.flows.views.executor import SESSION_KEY_PLAN
|
from authentik.flows.views.executor import SESSION_KEY_PLAN
|
||||||
from authentik.stages.email.models import EmailStage, get_template_choices
|
from authentik.stages.email.models import EmailStage, get_template_choices
|
||||||
|
from authentik.stages.email.utils import TemplateEmailMessage
|
||||||
|
|
||||||
|
|
||||||
def get_templates_setting(temp_dir: str) -> dict[str, Any]:
|
def get_templates_setting(temp_dir: str) -> dict[str, Any]:
|
||||||
@ -89,3 +91,12 @@ class TestEmailStageTemplates(FlowTestCase):
|
|||||||
event.context["message"], "Exception occurred while rendering E-mail template"
|
event.context["message"], "Exception occurred while rendering E-mail template"
|
||||||
)
|
)
|
||||||
self.assertEqual(event.context["template"], "invalid.html")
|
self.assertEqual(event.context["template"], "invalid.html")
|
||||||
|
|
||||||
|
def test_template_address(self):
|
||||||
|
"""Test addresses are correctly parsed"""
|
||||||
|
message = TemplateEmailMessage(to=[("foo@bar.baz", "foo@bar.baz")])
|
||||||
|
[sanitize_address(addr, "utf-8") for addr in message.recipients()]
|
||||||
|
self.assertEqual(message.recipients(), ["foo@bar.baz"])
|
||||||
|
message = TemplateEmailMessage(to=[("some-name", "foo@bar.baz")])
|
||||||
|
[sanitize_address(addr, "utf-8") for addr in message.recipients()]
|
||||||
|
self.assertEqual(message.recipients(), ["some-name <foo@bar.baz>"])
|
||||||
|
|||||||
@ -25,8 +25,19 @@ def logo_data() -> MIMEImage:
|
|||||||
class TemplateEmailMessage(EmailMultiAlternatives):
|
class TemplateEmailMessage(EmailMultiAlternatives):
|
||||||
"""Wrapper around EmailMultiAlternatives with integrated template rendering"""
|
"""Wrapper around EmailMultiAlternatives with integrated template rendering"""
|
||||||
|
|
||||||
def __init__(self, template_name=None, template_context=None, language="", **kwargs):
|
def __init__(
|
||||||
super().__init__(**kwargs)
|
self, to: list[tuple[str]], template_name=None, template_context=None, language="", **kwargs
|
||||||
|
):
|
||||||
|
sanitized_to = []
|
||||||
|
# Ensure that all recipients are valid
|
||||||
|
for recipient_name, recipient_email in to:
|
||||||
|
if recipient_name == recipient_email:
|
||||||
|
sanitized_to.append(recipient_email)
|
||||||
|
else:
|
||||||
|
sanitized_to.append(f"{recipient_name} <{recipient_email}>")
|
||||||
|
super().__init__(to=sanitized_to, **kwargs)
|
||||||
|
if not template_name:
|
||||||
|
return
|
||||||
with translation.override(language):
|
with translation.override(language):
|
||||||
html_content = render_to_string(template_name, template_context)
|
html_content = render_to_string(template_name, template_context)
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user