Skip to content

Commit 3055e53

Browse files
authored
Merge pull request #50 from pythonkr/fix/email-render-html-body
fix: 이메일 발송 시 body를 HTML 렌더링된 결과로 전송하도록 수정
2 parents 48b302e + 4d1e5a2 commit 3055e53

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

app/notification/models/email.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import ClassVar
1+
from typing import Any, ClassVar
22

33
from core.external_apis.smtp_email import EmailClient, email_client
44
from django.db import models
@@ -17,6 +17,12 @@ class EmailNotificationTemplate(NotificationTemplateBase):
1717
class EmailNotificationHistorySentTo(NotificationHistorySentToBase):
1818
history = models.ForeignKey("EmailNotificationHistory", on_delete=models.PROTECT, related_name="sent_to_list")
1919

20+
@property
21+
def payload(self) -> dict[str, Any]:
22+
rendered = self.render()
23+
rendered["body"] = self.render_as_html()
24+
return rendered
25+
2026

2127
class EmailNotificationHistoryQuerySet(
2228
NotificationHistoryQuerySet["EmailNotificationHistory", EmailNotificationTemplate],

app/notification/test/history_send_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ def test_history_send_parameters_uses_rendered_payload(system_user):
126126
assert params["template_code"] == "render"
127127

128128

129+
@pytest.mark.django_db
130+
def test_email_payload_body_is_html_rendered(system_user):
131+
# 이메일 발송 시 payload["body"]는 HTML 템플릿으로 렌더링된 결과여야 함.
132+
tpl = EmailNotificationTemplate.objects.create(
133+
code="html-body",
134+
title="t",
135+
sent_from="a@b.c",
136+
data='{"title":"안녕 {{ name }}","body":"본문 {{ name }}"}',
137+
created_by=system_user,
138+
updated_by=system_user,
139+
)
140+
history = _create_history(tpl, context={"name": "길동"})
141+
sent_to = history.sent_to_list.get()
142+
payload = sent_to.payload
143+
144+
# title은 plain text
145+
assert payload["title"] == "안녕 길동"
146+
assert not payload["title"].strip().startswith("<")
147+
148+
# body는 HTML 렌더링 결과
149+
assert payload["body"].strip().startswith("<")
150+
assert "길동" in payload["body"]
151+
152+
129153
@pytest.mark.django_db
130154
def test_history_template_code_property_returns_template_code(email_template):
131155
history = _create_history(email_template)

0 commit comments

Comments
 (0)