email: set an explicit Message-ID on outbound mail so a send is traceable

This commit is contained in:
inference-bot committed 2026-11-04 05:19:43 +00:00
1 parent 75959e6045
commit ced8c4f082
1 file changed
+5 -1
+5 -1
View File
@@ -23,6 +23,7 @@ import smtplib
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.utils import make_msgid
# --- Key encryption at rest (inlined from keycrypto.py; see repo history) --- # --- Key encryption at rest (inlined from keycrypto.py; see repo history) ---
# Member chat keys are encrypted with AES-256-GCM using a key-encryption key # Member chat keys are encrypted with AES-256-GCM using a key-encryption key
@@ -1124,6 +1125,9 @@ def send_email(to: str, subject: str, text_body: str, html_body: str | None = No
msg["To"] = to msg["To"] = to
msg["Subject"] = subject msg["Subject"] = subject
msg["Reply-To"] = MAIL_REPLY_TO msg["Reply-To"] = MAIL_REPLY_TO
# Explicit Message-ID so a send is traceable in the mail system and can be
# named in a report without reading the mailbox.
msg["Message-ID"] = make_msgid(domain=(MAIL_FROM.split("@")[-1] if "@" in MAIL_FROM else "inference.coop"))
msg.attach(MIMEText(text_body, "plain", "utf-8")) msg.attach(MIMEText(text_body, "plain", "utf-8"))
if html_body: if html_body:
msg.attach(MIMEText(html_body, "html", "utf-8")) msg.attach(MIMEText(html_body, "html", "utf-8"))
@@ -1132,7 +1136,7 @@ def send_email(to: str, subject: str, text_body: str, html_body: str | None = No
if SMTP_USERNAME: if SMTP_USERNAME:
s.login(SMTP_USERNAME, SMTP_PASSWORD) s.login(SMTP_USERNAME, SMTP_PASSWORD)
s.sendmail(MAIL_FROM, [to], msg.as_string()) s.sendmail(MAIL_FROM, [to], msg.as_string())
logger.info("Sent email to %s: %s", to, subject) logger.info("Sent email to %s: %s (message-id %s)", to, subject, msg["Message-ID"])
return True return True
except Exception as e: except Exception as e:
logger.warning("Failed to send email to %s: %s", to, e) logger.warning("Failed to send email to %s: %s", to, e)