Fix email header
diff --git a/staffeln/common/email.py b/staffeln/common/email.py
index 6aaaf9b..cf6e937 100644
--- a/staffeln/common/email.py
+++ b/staffeln/common/email.py
@@ -1,7 +1,9 @@
""" Email module with SMTP"""
import smtplib
+from email import utils
from email.header import Header
+from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from oslo_log import log
@@ -11,15 +13,23 @@
def send(smtp_profile):
"""Email send with SMTP"""
- dest_header = (
- smtp_profile["dest_email"]
- if isinstance(smtp_profile["dest_email"], str)
- else str(smtp_profile["dest_email"])
+ if isinstance(smtp_profile["dest_email"], str):
+ dest_header = smtp_profile["dest_email"]
+ elif isinstance(smtp_profile["dest_email"], list):
+ dest_header = ", ".join(smtp_profile["dest_email"])
+ else:
+ dest_header = str(smtp_profile["dest_email"])
+ msg = MIMEMultipart("alternative")
+ msg["Subject"] = Header(smtp_profile["subject"], "utf-8")
+ msg["From"] = "{} <{}>".format(
+ Header(smtp_profile["src_name"], "utf-8"), smtp_profile["src_email"]
)
- message = MIMEText(smtp_profile["content"], "html", "utf-8")
- message["From"] = Header(smtp_profile["src_name"], "utf-8")
- message["To"] = Header(dest_header, "utf-8")
- message["Subject"] = Header(smtp_profile["subject"], "utf-8")
+ msg["To"] = dest_header
+ msg["Message-id"] = utils.make_msgid()
+ msg["Date"] = utils.formatdate()
+ content = MIMEText(smtp_profile["content"], "html", "utf-8")
+ msg.attach(content)
+
try:
smtp_obj = smtplib.SMTP(
smtp_profile["smtp_server_domain"], smtp_profile["smtp_server_port"]
@@ -33,7 +43,7 @@
# SMTP Login
smtp_obj.login(smtp_profile["src_email"], smtp_profile["src_pwd"])
smtp_obj.sendmail(
- smtp_profile["src_email"], smtp_profile["dest_email"], message.as_string()
+ smtp_profile["src_email"], smtp_profile["dest_email"], msg.as_string()
)
# Email Sent
except smtplib.SMTPException as error: