Added retry logic if the SMTP connection is disconnected by the server.
This commit is contained in:
parent
f0991d63d1
commit
b2999ae325
1 changed files with 13 additions and 2 deletions
|
@ -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()
|
Loading…
Add table
Add a link
Reference in a new issue