From 2c1ecdff64891bcb80cfab82af3a19e1e8abb128 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 22 Mar 2017 18:29:03 +0100 Subject: [PATCH] Finished mail sending --- pp_lib/cfg_app.py | 47 +++++++++++++++++++++++++++++++++++++++- pp_lib/global_version.py | 2 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/pp_lib/cfg_app.py b/pp_lib/cfg_app.py index 759c624..9c9b0c7 100644 --- a/pp_lib/cfg_app.py +++ b/pp_lib/cfg_app.py @@ -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): """ diff --git a/pp_lib/global_version.py b/pp_lib/global_version.py index 90aa139..9b2b2b2 100644 --- a/pp_lib/global_version.py +++ b/pp_lib/global_version.py @@ -9,7 +9,7 @@ __author__ = 'Frank Brehm ' __contact__ = 'frank.brehm@pixelpark.com' -__version__ = '0.3.1' +__version__ = '0.4.1' __license__ = 'LGPL3+' # vim: fileencoding=utf-8 filetype=python ts=4 -- 2.39.5