mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Support unknown mime type for image attachments in html email (#38682)
This commit is contained in:
parent
f01634f75c
commit
ed68b15a31
@ -215,6 +215,29 @@ def _build_text_msg(message):
|
|||||||
return MIMEText(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):
|
def _build_multipart_msg(message, images):
|
||||||
"""Build Multipart message with in-line images."""
|
"""Build Multipart message with in-line images."""
|
||||||
_LOGGER.debug("Building multipart email with embedded attachment(s)")
|
_LOGGER.debug("Building multipart email with embedded attachment(s)")
|
||||||
@ -228,26 +251,9 @@ def _build_multipart_msg(message, images):
|
|||||||
for atch_num, atch_name in enumerate(images):
|
for atch_num, atch_name in enumerate(images):
|
||||||
cid = f"image{atch_num}"
|
cid = f"image{atch_num}"
|
||||||
body_text.append(f'<img src="cid:{cid}"><br>')
|
body_text.append(f'<img src="cid:{cid}"><br>')
|
||||||
try:
|
attachment = _attach_file(atch_name, cid)
|
||||||
with open(atch_name, "rb") as attachment_file:
|
if attachment:
|
||||||
file_bytes = attachment_file.read()
|
|
||||||
try:
|
|
||||||
attachment = MIMEImage(file_bytes)
|
|
||||||
msg.attach(attachment)
|
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)
|
|
||||||
|
|
||||||
body_html = MIMEText("".join(body_text), "html")
|
body_html = MIMEText("".join(body_text), "html")
|
||||||
msg_alt.attach(body_html)
|
msg_alt.attach(body_html)
|
||||||
@ -263,15 +269,9 @@ def _build_html_msg(text, html, images):
|
|||||||
alternative.attach(MIMEText(html, ATTR_HTML, _charset="utf-8"))
|
alternative.attach(MIMEText(html, ATTR_HTML, _charset="utf-8"))
|
||||||
msg.attach(alternative)
|
msg.attach(alternative)
|
||||||
|
|
||||||
for atch_num, atch_name in enumerate(images):
|
for atch_name in images:
|
||||||
name = os.path.basename(atch_name)
|
name = os.path.basename(atch_name)
|
||||||
try:
|
attachment = _attach_file(atch_name, name)
|
||||||
with open(atch_name, "rb") as attachment_file:
|
if attachment:
|
||||||
attachment = MIMEImage(attachment_file.read(), filename=name)
|
|
||||||
msg.attach(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
|
return msg
|
||||||
|
Loading…
x
Reference in New Issue
Block a user