diff --git a/apprise/plugins/NotifyEmail.py b/apprise/plugins/NotifyEmail.py index e9b9a32b..381d4dc5 100644 --- a/apprise/plugins/NotifyEmail.py +++ b/apprise/plugins/NotifyEmail.py @@ -253,6 +253,8 @@ class NotifyEmail(NotifyBase): .strftime("%a, %d %b %Y %H:%M:%S +0000") email['X-Application'] = self.app_id + # bind the socket variable to the current namespace + socket = None try: self.logger.debug('Connecting to remote SMTP server...') socket = smtplib.SMTP( @@ -289,7 +291,8 @@ class NotifyEmail(NotifyBase): finally: # Gracefully terminate the connection with the server - socket.quit() + if socket is not None: # pragma: no branch + socket.quit() return True diff --git a/test/test_email_plugin.py b/test/test_email_plugin.py index f5068987..5c8eb859 100644 --- a/test/test_email_plugin.py +++ b/test/test_email_plugin.py @@ -290,3 +290,57 @@ def test_webbase_lookup(mock_smtp): assert obj.secure is True assert obj.port == 123 assert obj.smtp_host == 'smtp.l2g.com' + +@mock.patch('smtplib.SMTP') +def test_smtplib_init_fail(mock_smtplib): + """ + API: Test exception handling when calling smtplib.SMTP() + + """ + + from apprise.plugins import NotifyEmailBase + + obj = Apprise.instantiate( + 'mailto://user:pass@gmail.com', suppress_exceptions=False) + assert(isinstance(obj, plugins.NotifyEmail)) + + # Support Exception handling of smtplib.SMTP + mock_smtplib.side_effect = TypeError('Test') + + try: + obj.notify( + title='test', body='body', + notify_type=NotifyType.INFO) + + # We should have thrown an exception + assert False + + except TypeError: + # Exception thrown as expected + assert True + + except Exception: + # Un-Expected + assert False + + +@mock.patch('smtplib.SMTP') +def test_smtplib_send_okay(mock_smtplib): + """ + API: Test a successfully sent email + + """ + + from apprise.plugins import NotifyEmailBase + + obj = Apprise.instantiate( + 'mailto://user:pass@gmail.com', suppress_exceptions=False) + assert(isinstance(obj, plugins.NotifyEmail)) + + # Support an email simulation where we can correctly quit + mock_smtplib.starttls.return_value = True + mock_smtplib.login.return_value = True + mock_smtplib.sendmail.return_value = True + mock_smtplib.quit.return_value = True + + obj.notify(title='test', body='body', notify_type=NotifyType.INFO)