mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +00:00
Added retry logic if the SMTP connection is disconnected by the server.
This commit is contained in:
parent
f0991d63d1
commit
b2999ae325
@ -140,15 +140,20 @@ class MailNotificationService(BaseNotificationService):
|
|||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
self.recipient = recipient
|
self.recipient = recipient
|
||||||
|
self.tries = 2
|
||||||
|
|
||||||
|
self.connect()
|
||||||
|
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
self.mail = smtplib.SMTP(self._server, self._port)
|
self.mail = smtplib.SMTP(self._server, self._port)
|
||||||
self.mail.ehlo_or_helo_if_needed()
|
self.mail.ehlo_or_helo_if_needed()
|
||||||
if self.starttls == 1:
|
if self.starttls == 1:
|
||||||
self.mail.starttls()
|
self.mail.starttls()
|
||||||
self.mail.ehlo()
|
self.mail.ehlo()
|
||||||
|
|
||||||
self.mail.login(self.username, self.password)
|
self.mail.login(self.username, self.password)
|
||||||
|
|
||||||
|
|
||||||
def send_message(self, message="", **kwargs):
|
def send_message(self, message="", **kwargs):
|
||||||
""" Send a message to a user. """
|
""" Send a message to a user. """
|
||||||
|
|
||||||
@ -160,4 +165,10 @@ class MailNotificationService(BaseNotificationService):
|
|||||||
msg['From'] = self._sender
|
msg['From'] = self._sender
|
||||||
msg['X-Mailer'] = 'HomeAssistant'
|
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…
x
Reference in New Issue
Block a user