Browse Source

Add the send_mail function to email the log contents

mario forzanini 2 years ago
parent
commit
c3c58a6798
1 changed files with 15 additions and 0 deletions
  1. 15 0
      lcmlog-server

+ 15 - 0
lcmlog-server

@@ -7,6 +7,7 @@ import os.path
 #from os import stat
 #from pwd import getpwuid
 import sys
+import shlex
 import pwd
 import logging
 import logging.handlers
@@ -218,6 +219,20 @@ def log_hash(name):
 		return hashlib.sha512((kind + user_name + date + tags + log).encode("utf-8")).hexdigest()
 
 
+# Email the log contents from USER_NAME@FROM_DOMAIN to TO_ADDRESS
+# Emails the log contents using the 'mail(1)' program. If REPLY_TO is
+# True add the 'Reply-to: ' header to the email
+def log_mail(kind, user_name, date, tags, log):
+	if REPLY_TO:
+                subprocess.run("printf " + shlex.quote(log) + " | mail -s \"Log" + kind + " " + date + " " + tags +
+                               "\" -r " + user + FROM_DOMAIN + " -a \"Reply-to:" TO_ADDRESS + " \" -- "
+		               + TO_ADDRESS, shell=True)
+	else:
+		subprocess.run("printf \"%s\n\" " + shlex.quote(log) + " | mail -s \"Log" | kind + " " + date + " " + tags +
+		               "\" -r " + user + FROM_DOMAIN + " -- " + TO_ADDRESS, shell=True)
+	return
+
+
 #------------------------------------------------------------------------------