From b2999ae325aa5e2246ea7e1a19e1aab99c209c46 Mon Sep 17 00:00:00 2001 From: Todd Ingarfield Date: Mon, 21 Sep 2015 18:54:30 -0500 Subject: [PATCH 1/4] Added retry logic if the SMTP connection is disconnected by the server. --- homeassistant/components/notify/smtp.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/notify/smtp.py b/homeassistant/components/notify/smtp.py index 0530ac4072d..f4f0d4cf57b 100644 --- a/homeassistant/components/notify/smtp.py +++ b/homeassistant/components/notify/smtp.py @@ -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() \ No newline at end of file From a42347e6e73ca32527cc1305a1f9f7c46082f40a Mon Sep 17 00:00:00 2001 From: Todd Ingarfield Date: Thu, 24 Sep 2015 10:47:19 -0500 Subject: [PATCH 2/4] corrected formating and style issues --- homeassistant/components/notify/smtp.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/notify/smtp.py b/homeassistant/components/notify/smtp.py index f4f0d4cf57b..c939737daa8 100644 --- a/homeassistant/components/notify/smtp.py +++ b/homeassistant/components/notify/smtp.py @@ -141,11 +141,13 @@ class MailNotificationService(BaseNotificationService): self.password = password self.recipient = recipient self.tries = 2 + self.mail = None self.connect() - def connect(self): + """ Connect/Authenticate to SMTP Server """ + self.mail = smtplib.SMTP(self._server, self._port) self.mail.ehlo_or_helo_if_needed() if self.starttls == 1: @@ -153,7 +155,6 @@ class MailNotificationService(BaseNotificationService): self.mail.ehlo() self.mail.login(self.username, self.password) - def send_message(self, message="", **kwargs): """ Send a message to a user. """ @@ -165,10 +166,12 @@ class MailNotificationService(BaseNotificationService): msg['From'] = self._sender msg['X-Mailer'] = 'HomeAssistant' - for attempt in range(self.tries): + for _ in range(self.tries): try: - self.mail.sendmail(self._sender, self.recipient, msg.as_string()) + 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() \ No newline at end of file + except smtplib.SMTPException as err: + _LOGGER.warning('SMTP Exception sending mail: {0}:{1}' + .format(err.smtp_code, err.smtp_error)) + self.connect() From a89bfcf3428c91a1f8a5a7cf368938a31f901637 Mon Sep 17 00:00:00 2001 From: Todd Ingarfield Date: Thu, 24 Sep 2015 10:55:24 -0500 Subject: [PATCH 3/4] removed exception attributes --- homeassistant/components/notify/smtp.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/notify/smtp.py b/homeassistant/components/notify/smtp.py index c939737daa8..b66ade255f1 100644 --- a/homeassistant/components/notify/smtp.py +++ b/homeassistant/components/notify/smtp.py @@ -171,7 +171,6 @@ class MailNotificationService(BaseNotificationService): self.mail.sendmail(self._sender, self.recipient, msg.as_string()) break - except smtplib.SMTPException as err: - _LOGGER.warning('SMTP Exception sending mail: {0}:{1}' - .format(err.smtp_code, err.smtp_error)) + except smtplib.SMTPException: + _LOGGER.warning('SMTPException sending mail: retrying connection') self.connect() From b0b3c2f73f95dd111069257a48d047ce457ec1b5 Mon Sep 17 00:00:00 2001 From: Todd Ingarfield Date: Thu, 24 Sep 2015 11:20:25 -0500 Subject: [PATCH 4/4] formatting correction --- homeassistant/components/notify/smtp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/notify/smtp.py b/homeassistant/components/notify/smtp.py index b66ade255f1..ee2413eda30 100644 --- a/homeassistant/components/notify/smtp.py +++ b/homeassistant/components/notify/smtp.py @@ -172,5 +172,6 @@ class MailNotificationService(BaseNotificationService): msg.as_string()) break except smtplib.SMTPException: - _LOGGER.warning('SMTPException sending mail: retrying connection') + _LOGGER.warning('SMTPException sending mail: ' + 'retrying connection') self.connect()