Added retry logic if the SMTP connection is disconnected by the server.

This commit is contained in:
Todd Ingarfield 2015-09-21 18:54:30 -05:00
parent f0991d63d1
commit b2999ae325

View file

@ -140,15 +140,20 @@ class MailNotificationService(BaseNotificationService):
self.username = username
self.password = password
self.recipient = recipient
self.tries = 2
self.connect()
def connect(self):
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. """
@ -160,4 +165,10 @@ class MailNotificationService(BaseNotificationService):
msg['From'] = self._sender
msg['X-Mailer'] = 'HomeAssistant'
self.mail.sendmail(self._sender, self.recipient, msg.as_string())
for attempt in range(self.tries):
try:
self.mail.sendmail(self._sender, self.recipient, msg.as_string())
break
except smtplib.SMTPException as e:
_LOGGER.warning('SMTP Exception sending mail: %s' % e)
self.connect()