core/homeassistant/components/notify/smtp.py

125 lines
3.8 KiB
Python
Raw Normal View History

2015-06-03 18:39:16 +00:00
"""
Mail (SMTP) notification service.
2015-06-03 18:39:16 +00:00
2015-10-13 20:56:12 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 17:33:11 +00:00
https://home-assistant.io/components/notify.smtp/
2015-06-03 18:39:16 +00:00
"""
import logging
import smtplib
from email.mime.text import MIMEText
from homeassistant.components.notify import (
2016-02-19 05:27:50 +00:00
ATTR_TITLE, DOMAIN, BaseNotificationService)
from homeassistant.helpers import validate_config
2015-06-03 18:39:16 +00:00
_LOGGER = logging.getLogger(__name__)
def get_service(hass, config):
2016-03-08 10:46:32 +00:00
"""Get the mail notification service."""
2015-11-09 06:21:02 +00:00
if not validate_config({DOMAIN: config},
{DOMAIN: ['recipient']},
2015-11-09 06:21:02 +00:00
_LOGGER):
2015-06-03 18:39:16 +00:00
return None
smtp_server = config.get('server', 'localhost')
port = int(config.get('port', '25'))
username = config.get('username', None)
password = config.get('password', None)
starttls = int(config.get('starttls', 0))
debug = config.get('debug', 0)
2015-06-03 18:39:16 +00:00
server = None
try:
server = smtplib.SMTP(smtp_server, port, timeout=5)
server.set_debuglevel(debug)
2015-06-03 18:39:16 +00:00
server.ehlo()
2015-11-09 06:15:34 +00:00
if starttls == 1:
2015-06-03 18:39:16 +00:00
server.starttls()
server.ehlo()
if username and password:
try:
server.login(username, password)
2015-06-03 18:39:16 +00:00
except (smtplib.SMTPException, smtplib.SMTPSenderRefused):
_LOGGER.exception("Please check your settings.")
return None
2015-06-03 18:39:16 +00:00
except smtplib.socket.gaierror:
_LOGGER.exception(
"SMTP server not found (%s:%s). "
"Please check the IP address or hostname of your SMTP server.",
smtp_server, port)
2015-06-03 18:39:16 +00:00
return None
except smtplib.SMTPAuthenticationError:
_LOGGER.exception(
"Login not possible. "
"Please check your setting and/or your credentials.")
return None
2015-11-09 06:15:34 +00:00
finally:
if server:
server.quit()
2015-06-03 18:39:16 +00:00
return MailNotificationService(
2015-11-09 06:15:34 +00:00
smtp_server, port, config['sender'], starttls, username, password,
config['recipient'], debug)
2015-06-03 18:39:16 +00:00
# pylint: disable=too-few-public-methods, too-many-instance-attributes
class MailNotificationService(BaseNotificationService):
2016-03-08 10:46:32 +00:00
"""Implement the notification service for E-Mail messages."""
2015-06-03 18:39:16 +00:00
# pylint: disable=too-many-arguments
def __init__(self, server, port, sender, starttls, username,
password, recipient, debug):
2016-03-08 10:46:32 +00:00
"""Initialize the service."""
2015-06-03 18:39:16 +00:00
self._server = server
self._port = port
self._sender = sender
2015-11-09 06:15:34 +00:00
self.starttls = starttls
2015-06-03 18:39:16 +00:00
self.username = username
self.password = password
self.recipient = recipient
self.debug = debug
self.tries = 2
def connect(self):
2016-03-08 10:46:32 +00:00
"""Connect/authenticate to SMTP Server."""
mail = smtplib.SMTP(self._server, self._port, timeout=5)
mail.set_debuglevel(self.debug)
mail.ehlo_or_helo_if_needed()
2015-06-03 18:39:16 +00:00
if self.starttls == 1:
mail.starttls()
mail.ehlo()
if self.username and self.password:
mail.login(self.username, self.password)
return mail
2015-06-03 18:39:16 +00:00
def send_message(self, message="", **kwargs):
2016-03-08 10:46:32 +00:00
"""Send a message to a user."""
mail = self.connect()
2015-06-03 18:39:16 +00:00
subject = kwargs.get(ATTR_TITLE)
msg = MIMEText(message)
msg['Subject'] = subject
msg['To'] = self.recipient
msg['From'] = self._sender
msg['X-Mailer'] = 'HomeAssistant'
2015-09-24 15:47:19 +00:00
for _ in range(self.tries):
try:
mail.sendmail(self._sender, self.recipient,
msg.as_string())
break
2015-09-24 15:55:24 +00:00
except smtplib.SMTPException:
2015-09-24 16:20:25 +00:00
_LOGGER.warning('SMTPException sending mail: '
'retrying connection')
mail.quit()
mail = self.connect()
mail.quit()