diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2024-03-01 14:46:22 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2024-12-21 19:43:13 +0200 |
| commit | f778686fece43cad8c2b08293e505ceeebe1890a (patch) | |
| tree | f0b001d4077e5b85cef23cc3906b8e15adadb82d | |
| parent | ef8c2c576043330a7a22d9549284003e57c284bb (diff) | |
Fix email integration
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | email_client.py | 22 |
2 files changed, 14 insertions, 13 deletions
@@ -37,8 +37,9 @@ You can also run the app without containerization: | EMAIL_PORT | SMTP port | | EMAIL_HOST_USER | SMTP host user | | EMAIL_HOST_PASSWORD | SMTP host password | -| EMAIL_USE_TLS | Use TLS with SMTP? | -| EMAIL_MESSAGE_FROM | Email message from header | +| EMAIL_USE_TLS | Use STARTTLS with SMTP? | +| EMAIL_HEADERS | Additional SMTP headers, format: `header1=foo,header2=bar` | +| EMAIL_MESSAGE_FROM | Email message from address | `EMAIL_` variables are only required if at least one of them is defined. diff --git a/email_client.py b/email_client.py index 0f0802f..acb6c9c 100644 --- a/email_client.py +++ b/email_client.py @@ -33,28 +33,28 @@ if email_enabled: email_host_password = os.environ["EMAIL_HOST_PASSWORD"] email_use_tls = os.environ["EMAIL_USE_TLS"].lower() in ["true", "1", "yes"] email_message_from = os.environ["EMAIL_MESSAGE_FROM"] + email_headers_raw = os.environ.get("EMAIL_HEADERS", "") + email_headers: dict[str, str] = {} + for header in email_headers_raw.split(","): + if not header: + continue + key, value = header.split("=", 1) + email_headers[key] = value print(f"SMTP email client enabled using email host {email_host}") def _actual_send_email(subject: str, body: str, recipient: str): if not email_enabled: return - - print("debug: subject:", subject) - print("debug: body:", body) - print("debug: recipient:", recipient) - print("debug: email_host:", email_host) - print("debug: email_port:", email_port) - print("debug: email_host_user:", email_host_user) - print("debug: email_host_password:", email_host_password) - print("debug: email_use_tls:", email_use_tls) - print("debug: email_message_from:", email_message_from) msg = MIMEMultipart() - msg['From'] = email_host_user + msg['From'] = email_message_from msg['To'] = recipient msg['Subject'] = subject msg.attach(MIMEText(body, 'plain')) + msg.add_header("Content-Type", "text/plain; charset=utf-8") + for key, value in email_headers.items(): + msg.add_header(key, value) with smtplib.SMTP(email_host, email_port) as server: if email_use_tls: |
