]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Finished mail sending
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 22 Mar 2017 17:29:03 +0000 (18:29 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 22 Mar 2017 17:29:03 +0000 (18:29 +0100)
pp_lib/cfg_app.py
pp_lib/global_version.py

index 759c624d2343275c81b5ae6b15daca75a67b2623..9c9b0c77272944904f282542a53a0e55e35ee4c0 100644 (file)
@@ -22,6 +22,9 @@ import copy
 import json
 import socket
 import pwd
+import pipes
+
+from subprocess import Popen, PIPE
 
 from email import encoders
 from email.mime.text import MIMEText
@@ -52,7 +55,7 @@ from .mailaddress import MailAddress
 
 from .app import PpApplication
 
-__version__ = '0.5.3'
+__version__ = '0.5.4'
 LOG = logging.getLogger(__name__)
 
 VALID_MAIL_METHODS = ('smtp', 'sendmail')
@@ -591,6 +594,48 @@ class PpConfigApplication(PpApplication):
         if self.verbose > 1:
             LOG.debug("Mail to send:\n{}".format(mail.as_string(unixfrom=True)))
 
+        if self.mail_method == 'smtp':
+            self._send_mail_smtp(mail)
+        else:
+            self._send_mail_sendmail(mail)
+
+    # -------------------------------------------------------------------------
+    def _send_mail_smtp(self, mail):
+
+        with smtplib.SMTP(self.mail_server, self.smtp_port) as smtp:
+            if self.verbose > 2:
+                smtp.set_debuglevel(2)
+            elif self.verbose > 1:
+                smtp.set_debuglevel(1)
+
+            smtp.send_message(mail)
+
+    # -------------------------------------------------------------------------
+    def _send_mail_sendmail(self, mail):
+
+        # Searching for the location of sendmail ...
+        paths = (
+            '/usr/sbin/sendmail',
+            '/usr/lib/sendmail',
+        )
+        sendmail = None
+        for path in paths:
+            if os.path.isfile(path) and os.access(path, os.X_OK):
+                sendmail = path
+                break
+
+        if not sendmail:
+            msg = "Did not found sendmail executable."
+            LOG.error(msg)
+            return
+
+        cmd = [sendmail, "-t", "-oi"]
+        cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmd))
+        LOG.debug("Executing: {}".format(cmd_str))
+
+        p = Popen(cmd, stdin=PIPE, universal_newlines=True)
+        p.communicate(mail.as_string())
+
     # -------------------------------------------------------------------------
     def post_init(self):
         """
index 90aa1391570ef5a8ac44a526bb3d606e0f5cfefa..9b2b2b265b5e1ff0c880dfc21ec79b0c90317d7d 100644 (file)
@@ -9,7 +9,7 @@
 
 __author__ = 'Frank Brehm <frank.brehm@pixelpark.com>'
 __contact__ = 'frank.brehm@pixelpark.com'
-__version__ = '0.3.1'
+__version__ = '0.4.1'
 __license__ = 'LGPL3+'
 
 # vim: fileencoding=utf-8 filetype=python ts=4