add journald communication capability
[logparse.git] / logparse / mail.py
index 9e840a43fde9d92cbb848db0f6004d5b00340a2d..26a402e44eacd14b5d353e34623c9808016becdf 100644 (file)
@@ -9,7 +9,7 @@ import logging
 logger = logging.getLogger(__name__)
 
 from os.path import isfile
-from premailer import transform
+import premailer
 from email.mime.text import MIMEText
 import subprocess
 
@@ -24,19 +24,20 @@ def mailprep(htmlin, stylesheet):
     return htmlout
 
 
-def sendmail(mailbin, body, recipient, subject, *sender):
+def sendmail(mailbin, body, recipient, subject, html=True, sender=""):
     logger.debug("Sending email")
-    msg = MIMEText(body)
+    msg = MIMEText(body, 'html' if html else 'plain')
     if sender:
         msg["From"] = sender
     msg["To"] = recipient
+    msg["Content-type"] = "text/html: te: text/html" if html else "text/plain: te: text/plain"
     msg["Subject"] = subject
-    mailproc = subprocess.Popen([mailbin, '--debug-level=10', '-t'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+    mailproc = subprocess.Popen([mailbin, "--debug-level=" + str(10 if logging.root.level == logging.DEBUG else 0), "-t"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
     logger.debug("Compiled message and opened process")
     try:
         stdout = mailproc.communicate(msg.as_bytes(), timeout=15)
         logger.debug("sendmail output: {}".format(stdout))
-        logger.info("Sent email")
+        logger.info("Sent email to {0}".format(recipient))
         return 0
 #    except TimeoutExpired:
 #        mailproc.kill()