From: Calin Crisan Date: Sun, 21 Feb 2016 13:35:30 +0000 (+0200) Subject: sendmail: added support for custom From field X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=18ea0aece526b7c676adfe4dad86cbdf13ba1e3d;p=motioneye-debian sendmail: added support for custom From field --- diff --git a/motioneye/config.py b/motioneye/config.py index 7e718e0..97b8a9c 100644 --- a/motioneye/config.py +++ b/motioneye/config.py @@ -868,13 +868,14 @@ def motion_camera_ui_to_dict(ui, old_config=None): if ui['email_notifications_enabled']: emails = re.sub('\\s', '', ui['email_notifications_addresses']) - on_event_start.append("%(script)s '%(server)s' '%(port)s' '%(account)s' '%(password)s' '%(tls)s' '%(to)s' 'motion_start' '%%t' '%%Y-%%m-%%dT%%H:%%M:%%S' '%(timespan)s'" % { + on_event_start.append("%(script)s '%(server)s' '%(port)s' '%(account)s' '%(password)s' '%(tls)s' '%(from)s' '%(to)s' 'motion_start' '%%t' '%%Y-%%m-%%dT%%H:%%M:%%S' '%(timespan)s'" % { 'script': meyectl.find_command('sendmail'), 'server': ui['email_notifications_smtp_server'], 'port': ui['email_notifications_smtp_port'], 'account': ui['email_notifications_smtp_account'], 'password': ui['email_notifications_smtp_password'].replace(';', '\\;').replace('%', '%%'), 'tls': ui['email_notifications_smtp_tls'], + 'from': ui['email_notifications_from'], 'to': emails, 'timespan': ui['email_notifications_picture_time_span']}) @@ -1237,17 +1238,22 @@ def motion_camera_dict_to_ui(data): if len(e) < 10: continue - + + if len(e) < 16: + # backwards compatibility with older configs lacking "from" field + e.insert(-5, '') + ui['email_notifications_enabled'] = True - ui['email_notifications_smtp_server'] = e[-10] - ui['email_notifications_smtp_port'] = e[-9] - ui['email_notifications_smtp_account'] = e[-8] - ui['email_notifications_smtp_password'] = e[-7].replace('\\;', ';').replace('%%', '%') - ui['email_notifications_smtp_tls'] = e[-6].lower() == 'true' + ui['email_notifications_smtp_server'] = e[-11] + ui['email_notifications_smtp_port'] = e[-10] + ui['email_notifications_smtp_account'] = e[-9] + ui['email_notifications_smtp_password'] = e[-8].replace('\\;', ';').replace('%%', '%') + ui['email_notifications_smtp_tls'] = e[-7].lower() == 'true' + ui['email_notifications_from'] = e[-6] ui['email_notifications_addresses'] = e[-5] try: ui['email_notifications_picture_time_span'] = int(e[-1]) - + except: ui['email_notifications_picture_time_span'] = 0 diff --git a/motioneye/sendmail.py b/motioneye/sendmail.py index 1398e7e..ea09ed0 100644 --- a/motioneye/sendmail.py +++ b/motioneye/sendmail.py @@ -48,7 +48,7 @@ subjects = { } -def send_mail(server, port, account, password, tls, to, subject, message, files): +def send_mail(server, port, account, password, tls, _from, to, subject, message, files): conn = smtplib.SMTP(server, port, timeout=settings.SMTP_TIMEOUT) if tls: conn.starttls() @@ -56,8 +56,6 @@ def send_mail(server, port, account, password, tls, to, subject, message, files) if account and password: conn.login(account, password) - _from = 'motionEye on %s <%s>' % (socket.gethostname(), to[0]) - email = MIMEMultipart() email['Subject'] = subject email['From'] = _from @@ -139,6 +137,7 @@ def parse_options(parser, args): parser.add_argument('account', help='SMTP account name (username)') parser.add_argument('password', help='SMTP account password') parser.add_argument('tls', help='"true" to use TLS') + parser.add_argument('from', help='the email from field') parser.add_argument('to', help='the email recipient(s)') parser.add_argument('msg_id', help='the identifier of the message') parser.add_argument('thread_id', help='the id of the motion thread') @@ -156,6 +155,14 @@ def main(parser, args): # or otherwise media listing won't work signal.signal(signal.SIGCHLD,signal.SIG_DFL) + if len(args) == 12: + # backwards compatibility with older configs lacking "from" field + _from = 'motionEye on %s <%s>' % (socket.gethostname(), args[7].split(',')[0]) + args = args[:7] + [_from] + args[7:] + + if not args[7]: + args[7] = 'motionEye on %s <%s>' % (socket.gethostname(), args[8].split(',')[0]) + options = parse_options(parser, args) meyectl.configure_logging('sendmail', options.log_to_file) @@ -175,6 +182,7 @@ def main(parser, args): settings.LIST_MEDIA_TIMEOUT = settings.LIST_MEDIA_TIMEOUT_EMAIL camera_id = motionctl.thread_id_to_camera_id(options.thread_id) + _from = getattr(options, 'from') logging.debug('server = %s' % options.server) logging.debug('port = %s' % options.port) @@ -182,6 +190,7 @@ def main(parser, args): logging.debug('password = ******') logging.debug('server = %s' % options.server) logging.debug('tls = %s' % str(options.tls).lower()) + logging.debug('from = %s' % _from) logging.debug('to = %s' % options.to) logging.debug('msg_id = %s' % options.msg_id) logging.debug('thread_id = %s' % options.thread_id) @@ -196,7 +205,7 @@ def main(parser, args): def on_message(subject, message, files): try: send_mail(options.server, options.port, options.account, options.password, - options.tls, to, subject, message, files or []) + options.tls, _from, to, subject, message, files or []) logging.info('email sent') except Exception as e: diff --git a/motioneye/static/js/main.js b/motioneye/static/js/main.js index 0b80c8a..317c8b7 100644 --- a/motioneye/static/js/main.js +++ b/motioneye/static/js/main.js @@ -579,6 +579,13 @@ function initUI() { return true; }, ''); + makeCustomValidator($('#emailFromEntry'), function (value) { + if (value && !value.toLowerCase().match(new RegExp('^[a-z0-9\-\_\+\.\@\^\~\<>, ]+$'))) { + return 'enter a vaild email address'; + } + + return true; + }, ''); makeCustomValidator($('#emailAddressesEntry'), function (value) { if (!value.toLowerCase().match(new RegExp('^[a-z0-9\-\_\+\.\@\^\~\, ]+$'))) { return 'enter a list of comma-separated valid email addresses'; @@ -1541,6 +1548,7 @@ function cameraUi2Dict() { /* motion notifications */ 'email_notifications_enabled': $('#emailNotificationsEnabledSwitch')[0].checked, + 'email_notifications_from': $('#emailFromEntry').val(), 'email_notifications_addresses': $('#emailAddressesEntry').val(), 'email_notifications_smtp_server': $('#smtpServerEntry').val(), 'email_notifications_smtp_port': $('#smtpPortEntry').val(), @@ -1877,6 +1885,7 @@ function dict2CameraUi(dict) { /* motion notifications */ $('#emailNotificationsEnabledSwitch')[0].checked = dict['email_notifications_enabled']; markHideIfNull('email_notifications_enabled', 'emailNotificationsEnabledSwitch'); + $('#emailFromEntry').val(dict['email_notifications_from']); $('#emailAddressesEntry').val(dict['email_notifications_addresses']); $('#smtpServerEntry').val(dict['email_notifications_smtp_server']); $('#smtpPortEntry').val(dict['email_notifications_smtp_port']); diff --git a/motioneye/templates/main.html b/motioneye/templates/main.html index b2f0008..c994a81 100644 --- a/motioneye/templates/main.html +++ b/motioneye/templates/main.html @@ -820,6 +820,11 @@ ? + + From Address + + ? + Use TLS