From 9a2c84ee8a9fedae3d4b5b65c2389729aecac797 Mon Sep 17 00:00:00 2001 From: Jesse Osiecki Date: Tue, 7 Feb 2017 03:43:03 -0500 Subject: [PATCH] Added error checking to the MIMEImage encoding for smtp.py (#5753) * Added error checking to the MIMEImage encoding for smtp.py Added fallback to file attachment rather than inline image for images without a known MIME * PEP8 reqs to fix previous commit --- homeassistant/components/notify/smtp.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/notify/smtp.py b/homeassistant/components/notify/smtp.py index 45b477cdfb8..89a71db3cf4 100644 --- a/homeassistant/components/notify/smtp.py +++ b/homeassistant/components/notify/smtp.py @@ -10,6 +10,7 @@ from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage import email.utils +from email.mime.application import MIMEApplication import voluptuous as vol @@ -179,9 +180,19 @@ def _build_multipart_msg(message, images): body_text.append('
'.format(cid)) try: with open(atch_name, 'rb') as attachment_file: - attachment = MIMEImage(attachment_file.read()) - msg.attach(attachment) - attachment.add_header('Content-ID', '<{}>'.format(cid)) + file_bytes = attachment_file.read() + try: + attachment = MIMEImage(file_bytes) + msg.attach(attachment) + attachment.add_header('Content-ID', '<{}>'.format(cid)) + except TypeError: + _LOGGER.warning('Attachment %s has an unkown 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)