From 3f8990b6be2a183263832b9c9987eb6c587b7cdb Mon Sep 17 00:00:00 2001 From: Calin Crisan Date: Sat, 2 Jul 2016 17:24:08 +0300 Subject: [PATCH] added a test email button --- motioneye/handlers.py | 67 +++++++++++++++++++++++++++++++++++ motioneye/static/js/main.js | 41 +++++++++++++++++++++ motioneye/templates/main.html | 5 +++ 3 files changed, 113 insertions(+) diff --git a/motioneye/handlers.py b/motioneye/handlers.py index aff2a63..b250b61 100644 --- a/motioneye/handlers.py +++ b/motioneye/handlers.py @@ -775,6 +775,73 @@ class ConfigHandler(BaseHandler): else: logging.warn('accessing %s failed: %s' % (service, result)) self.finish_json({'error': result}) + + elif utils.remote_camera(camera_config): + def on_response(result=None, error=None): + if result is True: + self.finish_json() + + else: + result = result or error + self.finish_json({'error': result}) + + remote.test(camera_config, data, on_response) + + elif what == 'email': + import sendmail + import tzctl + import smtplib + + if utils.local_motion_camera(camera_config): + logging.debug('testing notification email') + + try: + subject = sendmail.subjects['motion_start'] + message = sendmail.messages['motion_start'] + format_dict = { + 'camera': camera_config['@name'], + 'hostname': socket.gethostname(), + 'moment': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + } + if settings.LOCAL_TIME_FILE: + format_dict['timezone'] = tzctl.get_time_zone() + + else: + format_dict['timezone'] = 'local time' + + message = message % format_dict + subject = subject % format_dict + + old_timeout = settings.SMTP_TIMEOUT + settings.SMTP_TIMEOUT = 10 + sendmail.send_mail(data['smtp_server'], int(data['smtp_port']), data['smtp_account'], data['smtp_password'], data['smtp_tls'], + data['from'], [data['addresses']], subject=subject, message=message, files=[]) + settings.SMTP_TIMEOUT = old_timeout + + self.finish_json() + + except Exception as e: + if isinstance(e, smtplib.SMTPResponseException): + msg = e.smtp_error + + else: + msg = str(e) + + msg_lower = msg.lower() + if msg_lower.count('tls'): + msg = 'TLS might be required' + + elif msg_lower.count('authentication'): + msg = 'authentication error' + + elif msg_lower.count('name or service not known'): + msg = 'check SMTP server name' + + elif msg_lower.count('connection refused'): + msg = 'check SMTP port' + + logging.error('notification email test failed: %s' % msg, exc_info=True) + self.finish_json({'error': str(msg)}) elif utils.remote_camera(camera_config): def on_response(result=None, error=None): diff --git a/motioneye/static/js/main.js b/motioneye/static/js/main.js index 58768a0..9306b50 100644 --- a/motioneye/static/js/main.js +++ b/motioneye/static/js/main.js @@ -2494,6 +2494,46 @@ function doTestUpload() { }); } +function doTestEmail() { + var q = $('#emailAddressesEntry, #smtpServerEntry, #smtpPortEntry'); + var valid = true; + q.each(function() { + this.validate(); + if (this.invalid) { + valid = false; + } + }); + + if (!valid) { + return runAlertDialog('Make sure all the configuration options are valid!'); + } + + showModalDialog('', null, null, true); + + var data = { + what: 'email', + from: $('#emailFromEntry').val(), + addresses: $('#emailAddressesEntry').val(), + smtp_server: $('#smtpServerEntry').val(), + smtp_port: $('#smtpPortEntry').val(), + smtp_account: $('#smtpAccountEntry').val(), + smtp_password: $('#smtpPasswordEntry').val(), + smtp_tls: $('#smtpTlsSwitch')[0].checked + }; + + var cameraId = $('#cameraSelect').val(); + + ajax('POST', basePath + 'config/' + cameraId + '/test/', data, function (data) { + hideModalDialog(); /* progress */ + if (data.error) { + showErrorMessage('Notification email failed: ' + data.error + '!'); + } + else { + showPopupMessage('Notification email succeeded!', 'info'); + } + }); +} + function doDownloadZipped(cameraId, groupKey) { showModalDialog('', null, null, true); ajax('GET', basePath + 'picture/' + cameraId + '/zipped/' + groupKey + '/', null, function (data) { @@ -4470,6 +4510,7 @@ $(document).ready(function () { /* test buttons */ $('div#uploadTestButton').click(doTestUpload); + $('div#emailTestButton').click(doTestEmail); initUI(); beginProgress(); diff --git a/motioneye/templates/main.html b/motioneye/templates/main.html index ed87e3e..09cbe40 100644 --- a/motioneye/templates/main.html +++ b/motioneye/templates/main.html @@ -845,6 +845,11 @@ seconds ? + + +
Test Email
+ ? +
-- 2.39.5