From: Frank Brehm Date: Sun, 27 Mar 2022 13:52:52 +0000 (+0200) Subject: Adding sendmail methods X-Git-Tag: 0.4.1^2~34 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=5c19f3bf701a47898366d96fb394618b6be968c9;p=pixelpark%2Fpp-admin-tools.git Adding sendmail methods --- diff --git a/lib/pp_admintools/mail_app.py b/lib/pp_admintools/mail_app.py index d618437..daa264d 100644 --- a/lib/pp_admintools/mail_app.py +++ b/lib/pp_admintools/mail_app.py @@ -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__": diff --git a/lib/pp_admintools/mail_config.py b/lib/pp_admintools/mail_config.py index 10c011e..d650762 100644 --- a/lib/pp_admintools/mail_config.py +++ b/lib/pp_admintools/mail_config.py @@ -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