Skip to content

Commit

Permalink
Support unknown mime type for image attachments in html email (home-a…
Browse files Browse the repository at this point in the history
  • Loading branch information
esev authored Aug 21, 2020
1 parent f01634f commit ed68b15
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions homeassistant/components/smtp/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,29 @@ def _build_text_msg(message):
return MIMEText(message)


def _attach_file(atch_name, content_id):
"""Create a message attachment."""
try:
with open(atch_name, "rb") as attachment_file:
file_bytes = attachment_file.read()
except FileNotFoundError:
_LOGGER.warning("Attachment %s not found. Skipping", atch_name)
return None

try:
attachment = MIMEImage(file_bytes)
except TypeError:
_LOGGER.warning(
"Attachment %s has an unknown MIME type. " "Falling back to file",
atch_name,
)
attachment = MIMEApplication(file_bytes, Name=atch_name)
attachment["Content-Disposition"] = "attachment; " 'filename="%s"' % atch_name

attachment.add_header("Content-ID", f"<{content_id}>")
return attachment


def _build_multipart_msg(message, images):
"""Build Multipart message with in-line images."""
_LOGGER.debug("Building multipart email with embedded attachment(s)")
Expand All @@ -228,26 +251,9 @@ def _build_multipart_msg(message, images):
for atch_num, atch_name in enumerate(images):
cid = f"image{atch_num}"
body_text.append(f'<img src="cid:{cid}"><br>')
try:
with open(atch_name, "rb") as attachment_file:
file_bytes = attachment_file.read()
try:
attachment = MIMEImage(file_bytes)
msg.attach(attachment)
attachment.add_header("Content-ID", f"<{cid}>")
except TypeError:
_LOGGER.warning(
"Attachment %s has an unknown MIME type. "
"Falling back to file",
atch_name,
)
attachment = MIMEApplication(file_bytes, Name=atch_name)
attachment["Content-Disposition"] = (
"attachment; " 'filename="%s"' % atch_name
)
msg.attach(attachment)
except FileNotFoundError:
_LOGGER.warning("Attachment %s not found. Skipping", atch_name)
attachment = _attach_file(atch_name, cid)
if attachment:
msg.attach(attachment)

body_html = MIMEText("".join(body_text), "html")
msg_alt.attach(body_html)
Expand All @@ -263,15 +269,9 @@ def _build_html_msg(text, html, images):
alternative.attach(MIMEText(html, ATTR_HTML, _charset="utf-8"))
msg.attach(alternative)

for atch_num, atch_name in enumerate(images):
for atch_name in images:
name = os.path.basename(atch_name)
try:
with open(atch_name, "rb") as attachment_file:
attachment = MIMEImage(attachment_file.read(), filename=name)
attachment = _attach_file(atch_name, name)
if attachment:
msg.attach(attachment)
attachment.add_header("Content-ID", f"<{name}>")
except FileNotFoundError:
_LOGGER.warning(
"Attachment %s [#%s] not found. Skipping", atch_name, atch_num
)
return msg

0 comments on commit ed68b15

Please sign in to comment.