Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions homeassistant/components/notify/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
from email.mime.application import MIMEApplication
import email.utils
import os

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
from homeassistant.components.notify import (
ATTR_TITLE, ATTR_TITLE_DEFAULT, ATTR_DATA, PLATFORM_SCHEMA,
BaseNotificationService)
from homeassistant.const import (
CONF_USERNAME, CONF_PASSWORD, CONF_PORT, CONF_TIMEOUT,
CONF_SENDER, CONF_RECIPIENT)
import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util

_LOGGER = logging.getLogger(__name__)

Expand All @@ -42,10 +43,10 @@
# pylint: disable=no-value-for-parameter
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RECIPIENT): vol.All(cv.ensure_list, [vol.Email()]),
vol.Required(CONF_SENDER): vol.Email(),
vol.Optional(CONF_SERVER, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
vol.Optional(CONF_SENDER): vol.Email(),
vol.Optional(CONF_STARTTLS, default=DEFAULT_STARTTLS): cv.boolean,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
Expand Down Expand Up @@ -75,11 +76,11 @@ def get_service(hass, config, discovery_info=None):


class MailNotificationService(BaseNotificationService):
"""Implement the notification service for E-Mail messages."""
"""Implement the notification service for E-mail messages."""

def __init__(self, server, port, timeout, sender, starttls, username,
password, recipients, sender_name, debug):
"""Initialize the service."""
"""Initialize the SMTP service."""
self._server = server
self._port = port
self._timeout = timeout
Expand Down Expand Up @@ -142,11 +143,11 @@ def send_message(self, message="", **kwargs):

if data:
if ATTR_HTML in data:
msg = _build_html_msg(message, data[ATTR_HTML],
images=data.get(ATTR_IMAGES))
msg = _build_html_msg(
message, data[ATTR_HTML], images=data.get(ATTR_IMAGES))
else:
msg = _build_multipart_msg(message,
images=data.get(ATTR_IMAGES))
msg = _build_multipart_msg(
message, images=data.get(ATTR_IMAGES))
else:
msg = _build_text_msg(message)

Expand All @@ -167,8 +168,7 @@ def _send_email(self, msg):
mail = self.connect()
for _ in range(self.tries):
try:
mail.sendmail(self._sender, self.recipients,
msg.as_string())
mail.sendmail(self._sender, self.recipients, msg.as_string())
break
except smtplib.SMTPServerDisconnected:
_LOGGER.warning(
Expand Down Expand Up @@ -210,7 +210,7 @@ def _build_multipart_msg(message, images):
msg.attach(attachment)
attachment.add_header('Content-ID', '<{}>'.format(cid))
except TypeError:
_LOGGER.warning("Attachment %s has an unkown MIME type. "
_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; '
Expand All @@ -226,8 +226,8 @@ def _build_multipart_msg(message, images):


def _build_html_msg(text, html, images):
"""Build Multipart message with in-line images and rich html (UTF-8)."""
_LOGGER.debug("Building html rich email")
"""Build Multipart message with in-line images and rich HTML (UTF-8)."""
_LOGGER.debug("Building HTML rich email")
msg = MIMEMultipart('related')
alternative = MIMEMultipart('alternative')
alternative.attach(MIMEText(text, _charset='utf-8'))
Expand All @@ -242,6 +242,6 @@ def _build_html_msg(text, html, images):
msg.attach(attachment)
attachment.add_header('Content-ID', '<{}>'.format(name))
except FileNotFoundError:
_LOGGER.warning('Attachment %s [#%s] not found. Skipping',
_LOGGER.warning("Attachment %s [#%s] not found. Skipping",
atch_name, atch_num)
return msg