]> www.vanbest.org Git - motioneye-debian/commitdiff
sendmail: added support for custom From field
authorCalin Crisan <ccrisan@gmail.com>
Sun, 21 Feb 2016 13:35:30 +0000 (15:35 +0200)
committerCalin Crisan <ccrisan@gmail.com>
Sun, 21 Feb 2016 13:35:30 +0000 (15:35 +0200)
motioneye/config.py
motioneye/sendmail.py
motioneye/static/js/main.js
motioneye/templates/main.html

index 7e718e03c29a612d53f6bbb0b431ba7d6cef5c83..97b8a9c448beac0dcfd84fc6e84853fccf460240 100644 (file)
@@ -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
 
index 1398e7e914c4942819ab90d16b1945d469dfff7b..ea09ed0614a9dd15dbcdb6d21ddfb33f719a2749 100644 (file)
@@ -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:
index 0b80c8a0bf44ad540a8fbbf1d9553dbbbfdfa5fc..317c8b7dcc44bf4d5762867e60a073b4813bd164 100644 (file)
@@ -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']);
index b2f00085d5d38a85b5392e078c8de0f2b52c8d49..c994a8177f4a2b10688946bd2841af21838ae95a 100644 (file)
                         <td class="settings-item-value"><input type="password" class="styled notifications camera-config" id="smtpPasswordEntry"></td>
                         <td><span class="help-mark" title="enter your SMTP account password (for Gmail use your Google password or an app-specific generated password)">?</span></td>
                     </tr>
+                    <tr class="settings-item advanced-setting" depends="emailNotificationsEnabled motionDetectionEnabled" strip="true">
+                        <td class="settings-item-label"><span class="settings-item-label">From Address</span></td>
+                        <td class="settings-item-value"><input type="text" class="styled notifications camera-config" id="emailFromEntry" placeholder="email address..."></td>
+                        <td><span class="help-mark" title="set a custom From address, if your SMTP service requires one (the first destination email address will be used if left blank)">?</span></td>
+                    </tr>
                     <tr class="settings-item advanced-setting" depends="emailNotificationsEnabled motionDetectionEnabled">
                         <td class="settings-item-label"><span class="settings-item-label">Use TLS</span></td>
                         <td class="settings-item-value"><input type="checkbox" class="styled notifications camera-config" id="smtpTlsSwitch"></td>