Add 'from_name' field to SMTP settings and update email sending logic

- Introduced a new 'from_name' field in the SMTP settings to allow users to specify a sender name.
- Updated the dashboard template to include an input for the sender name.
- Modified the email sending logic to format the 'From' header with the sender name if provided, enhancing email presentation.
This commit is contained in:
smueller
2026-07-07 16:00:22 +02:00
parent 0e37dac816
commit f2cfe8de3e
3 changed files with 11 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ import smtplib
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formataddr
from typing import Sequence
from sqlalchemy.orm import Session
@@ -18,6 +19,7 @@ def get_smtp_settings(db: Session) -> dict[str, str]:
"username": get_config(db, "smtp.username", ""),
"password": get_config(db, "smtp.password", ""),
"from_email": get_config(db, "smtp.from_email", ""),
"from_name": get_config(db, "smtp.from_name", ""),
"reply_to": get_config(db, "smtp.reply_to", ""),
"use_tls": get_config(db, "smtp.use_tls", "true"),
"recipients": get_config(db, "smtp.recipients", ""),
@@ -90,7 +92,10 @@ def send_newsletter(
msg.attach(MIMEText(text_content, "plain", "utf-8"))
msg.attach(MIMEText(html_content, "html", "utf-8"))
msg["Subject"] = subject
msg["From"] = settings["from_email"]
if settings.get("from_name"):
msg["From"] = formataddr((settings["from_name"], settings["from_email"]))
else:
msg["From"] = settings["from_email"]
msg["To"] = ", ".join(recipients)
if settings.get("reply_to"):
msg["Reply-To"] = settings["reply_to"]