core/homeassistant/components/notify/smtp.py

123 lines
3.5 KiB
Python
Raw Normal View History

2015-06-03 18:39:16 +00:00
"""
2015-10-07 21:28:04 +00:00
homeassistant.components.notify.smtp
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.helpers import validate_config
from homeassistant.components.notify import (
DOMAIN, ATTR_TITLE, BaseNotificationService)
_LOGGER = logging.getLogger(__name__)
def get_service(hass, config):
""" Get the mail notification service. """
2015-11-09 06:21:02 +00:00
if not validate_config({DOMAIN: config},
{DOMAIN: ['server', 'port', 'sender', 'username',
'password', 'recipient']},
_LOGGER):
2015-06-03 18:39:16 +00:00
return None
2015-11-09 06:15:34 +00:00
smtp_server = config['server']
port = int(config['port'])
username = config['username']
password = config['password']
starttls = int(config['starttls'])
2015-06-03 18:39:16 +00:00
server = None
try:
server = smtplib.SMTP(smtp_server, port)
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()
try:
server.login(username, password)
2015-11-09 06:15:34 +00:00
except (smtplib.SMTPException, smtplib.SMTPSenderRefused):
_LOGGER.exception("Please check your settings.")
2015-06-03 18:39:16 +00:00
return None
except smtplib.socket.gaierror:
_LOGGER.exception(
"SMTP server not found. "
"Please check the IP address or hostname of your SMTP server.")
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'])
2015-06-03 18:39:16 +00:00
# pylint: disable=too-few-public-methods, too-many-instance-attributes
class MailNotificationService(BaseNotificationService):
""" Implements notification service for E-Mail messages. """
# pylint: disable=too-many-arguments
def __init__(self, server, port, sender, starttls, username,
password, recipient):
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.tries = 2
2015-09-24 15:47:19 +00:00
self.mail = None
2015-06-03 18:39:16 +00:00
self.connect()
def connect(self):
2015-09-24 15:47:19 +00:00
""" Connect/Authenticate to SMTP Server """
2015-06-03 18:39:16 +00:00
self.mail = smtplib.SMTP(self._server, self._port)
self.mail.ehlo_or_helo_if_needed()
if self.starttls == 1:
self.mail.starttls()
self.mail.ehlo()
self.mail.login(self.username, self.password)
def send_message(self, message="", **kwargs):
""" Send a message to a user. """
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:
2015-09-24 15:47:19 +00:00
self.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')
2015-09-24 15:47:19 +00:00
self.connect()