Skip to content

Commit

Permalink
lib.mail: Move MIMEText transformation to Mail class
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Sep 28, 2024
1 parent fab32b3 commit e1a0e13
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pycroft/lib/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ class Mail:
body_html: str | None = None
reply_to: str | None = None

@property
def body_plain_mime(self) -> MIMEText:
return MIMEText(self.body_plain, "plain", _charset="utf-8")

@property
def body_html_mime(self) -> MIMEText | None:
if not self.body_html:
return None
return MIMEText(self.body_html, "html", _charset="utf-8")


class MailTemplate:
template: str
Expand Down Expand Up @@ -75,12 +85,9 @@ def compose_mail(mail: Mail, from_: str, default_reply_to: str | None) -> MIMEMu
msg["To"] = str(Header(mail.to_address))
msg["Subject"] = mail.subject
msg["Date"] = formatdate(localtime=True)

msg.attach(MIMEText(mail.body_plain, 'plain', _charset='utf-8'))

if mail.body_html is not None:
msg.attach(MIMEText(mail.body_html, 'html', _charset='utf-8'))

msg.attach(mail.body_plain_mime)
if (html := mail.body_html_mime) is not None:
msg.attach(html)
if reply_to := mail.reply_to or default_reply_to:
msg["Reply-To"] = reply_to

Expand Down

0 comments on commit e1a0e13

Please sign in to comment.