]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding sendmail methods
authorFrank Brehm <frank@brehm-online.com>
Sun, 27 Mar 2022 13:52:52 +0000 (15:52 +0200)
committerFrank Brehm <frank@brehm-online.com>
Sun, 27 Mar 2022 13:52:52 +0000 (15:52 +0200)
lib/pp_admintools/mail_app.py
lib/pp_admintools/mail_config.py

index d618437161d01b3c3ab4cdf215f42ae4511e74a4..daa264d5788a628d5ca48ba190e2b0a9e51fc5cc 100644 (file)
@@ -10,10 +10,13 @@ from __future__ import absolute_import
 # Standard modules
 import logging
 import copy
+import pipes
 
 from email.mime.text import MIMEText
 from email import charset
 
+from subprocess import Popen, PIPE
+
 import smtplib
 
 # Own modules
@@ -32,7 +35,7 @@ from .xlate import XLATOR
 from .mail_config import MailConfigError, MailConfiguration
 from .mail_config import VALID_MAIL_METHODS, MAX_PORT_NUMBER
 
-__version__ = '0.1.1'
+__version__ = '0.2.1'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -250,6 +253,63 @@ class BaseMailApplication(FbConfigApplication):
         if self.verbose > 2:
             LOG.debug(_("Got command line arguments:") + '\n' + pp(self.args))
 
+    # -------------------------------------------------------------------------
+    def send_mail(self, subject, body):
+
+        mail = MIMEText(body, 'plain', 'utf-8')
+        mail['Subject'] = subject
+        mail['From'] = self.cfg.mail_from
+        mail['To'] = ', '.join(self.cfg.mail_recipients)
+        mail['Reply-To'] = self.cfg.reply_to
+        mail['X-Mailer'] = self.cfg.xmailer
+        if self.mail_cc:
+            mail['Cc'] = ', '.join(self.mail_cc)
+
+        if self.verbose > 1:
+            LOG.debug(_("Mail to send:") + '\n' + 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.cfg.mail_server, self.cfg.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())
+
 
 # =============================================================================
 if __name__ == "__main__":
index 10c011edfe2644169b49a3d95f00569348936129..d650762f2e70c537bae0d08e796175216d3280a0 100644 (file)
@@ -27,11 +27,13 @@ from fb_tools.common import is_sequence
 from fb_tools.multi_config import MultiConfigError, BaseMultiConfig
 from fb_tools.multi_config import DEFAULT_ENCODING
 
+from . import __version__ as GLOBAL_VERSION
+
 from .mailaddress import MailAddress
 
 from .xlate import XLATOR
 
-__version__ = '0.1.2'
+__version__ = '0.1.3'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -65,7 +67,7 @@ class MailConfiguration(BaseMultiConfig):
 
     default_reply_to = 'solution@pixelpark.com'
 
-    default_mail_server = 'prd-mail.pixelpark.com'
+    default_mail_server = 'localhost'
 
     current_user_name = pwd.getpwuid(os.getuid()).pw_name
     current_user_gecos = pwd.getpwuid(os.getuid()).pw_gecos
@@ -111,6 +113,9 @@ class MailConfiguration(BaseMultiConfig):
             ensure_privacy=ensure_privacy, initialized=False,
         )
 
+        self.xmailer = "{a} (Admin Tools version {v})".format(
+            a=self.appname, v=GLOBAL_VERSION)
+
         if initialized:
             self.initialized = True