From: Frank Brehm Date: Wed, 22 Mar 2017 17:01:52 +0000 (+0100) Subject: Generating of a mail to send X-Git-Tag: 0.1.2~229 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=d2a1b9166faa7b082b2db07bb507f83269075873;p=pixelpark%2Fadmin-tools.git Generating of a mail to send --- diff --git a/pp_lib/cfg_app.py b/pp_lib/cfg_app.py index 793c05f..759c624 100644 --- a/pp_lib/cfg_app.py +++ b/pp_lib/cfg_app.py @@ -20,6 +20,14 @@ import textwrap import datetime import copy import json +import socket +import pwd + +from email import encoders +from email.mime.text import MIMEText +from email import charset + +import smtplib # Third party modules import six @@ -44,7 +52,7 @@ from .mailaddress import MailAddress from .app import PpApplication -__version__ = '0.5.2' +__version__ = '0.5.3' LOG = logging.getLogger(__name__) VALID_MAIL_METHODS = ('smtp', 'sendmail') @@ -75,7 +83,13 @@ class PpConfigApplication(PpApplication): default_mail_server = 'mx.pixelpark.net' - whitespace_re = re.compile(r'(?:[,;]+|\s*[,;]*\s+)') + current_user_name = pwd.getpwuid(os.getuid()).pw_name + current_user_gecos = pwd.getpwuid(os.getuid()).pw_gecos + default_mail_from = MailAddress(current_user_name, socket.getfqdn()) + + whitespace_re = re.compile(r'(?:[,;]+|\s*[,;]*\s+)+') + + charset.add_charset('utf-8', charset.SHORTEST, charset.QP) # ------------------------------------------------------------------------- def __init__( @@ -95,10 +109,13 @@ class PpConfigApplication(PpApplication): self.log_cfg_files = [] self.mail_recipients = copy.copy(self.default_mail_recipients) + self.mail_from = '{n} <{m}>'.format( + n=self.current_user_gecos, m=self.default_mail_from) self.mail_cc = copy.copy(self.default_mail_cc) self.reply_to = self.default_reply_to - self.mail_server = self.default_mail_server self.mail_method = 'smtp' + self.mail_server = self.default_mail_server + self.smtp_port = 25 super(PpConfigApplication, self).__init__( appname=appname, verbose=verbose, version=version, base_dir=base_dir, @@ -490,6 +507,8 @@ class PpConfigApplication(PpApplication): self.mail_cc = [] if v: tokens = self.whitespace_re.split(v) + if self.verbose > 1: + LOG.debug("CC addresses:\n{}".format(pp(tokens))) for token in tokens: if MailAddress.valid_address(token): if token not in self.mail_cc: @@ -512,11 +531,6 @@ class PpConfigApplication(PpApplication): tokens[0]) LOG.error(msg) - if 'mail_server' in section: - v = section['reply_to'].strip() - if v: - self.mail_server = v - if 'mail_method' in section: v = section['mail_method'].strip().lower() if v: @@ -527,6 +541,24 @@ class PpConfigApplication(PpApplication): section['mail_method']) LOG.error(msg) + if 'mail_server' in section: + v = section['reply_to'].strip() + if v: + self.mail_server = v + + if 'smtp_port' in section: + v = section['smtp_port'] + port = self.smtp_port + try: + port = int(v) + except (ValueError, TypeError) as e: + msg = "Found invalid SMTP port number {!r} in configuration.".format(v) + LOG.error(msg) + if port <= 0: + msg = "Found invalid SMTP port number {!r} in configuration.".format(port) + LOG.error(msg) + self.smtp_port = port + self.perform_config() # ------------------------------------------------------------------------- @@ -541,6 +573,24 @@ class PpConfigApplication(PpApplication): pass + # ------------------------------------------------------------------------- + def send_mail(self, subject, body): + + xmailer = "{a} (Admin Tools version {v})".format( + a=self.appname, v=__global_version__) + + mail = MIMEText(body, 'plain', 'utf-8') + mail['Subject'] = subject + mail['From'] = self.mail_from + mail['To'] = ', '.join(self.mail_recipients) + mail['Reply-To'] = self.reply_to + mail['X-Mailer'] = xmailer + if self.mail_cc: + mail['Cc'] = ', '.join(self.mail_cc) + + if self.verbose > 1: + LOG.debug("Mail to send:\n{}".format(mail.as_string(unixfrom=True))) + # ------------------------------------------------------------------------- def post_init(self): """ diff --git a/pp_lib/test_home_app.py b/pp_lib/test_home_app.py index 7806b8c..04021e3 100644 --- a/pp_lib/test_home_app.py +++ b/pp_lib/test_home_app.py @@ -32,7 +32,7 @@ from .common import pp, terminal_can_colors, to_bytes, to_bool from .cfg_app import PpCfgAppError, PpConfigApplication -__version__ = '0.4.2' +__version__ = '0.4.3' LOG = logging.getLogger(__name__) @@ -140,6 +140,7 @@ class PpTestHomeApp(PpConfigApplication): self.read_exclude_dirs() self.read_passwd_homes() self.check_homes() + self.send_results() # ------------------------------------------------------------------------- def read_exclude_dirs(self): @@ -257,6 +258,25 @@ class PpTestHomeApp(PpConfigApplication): self.unnecessary_dirs.sort(key=str.lower) + # ------------------------------------------------------------------------- + def send_results(self): + + if not self.unnecessary_dirs: + LOG.debug("No unnecessary home directories, nothing to inform.") + return + + subject = 'Nicht benötigte Home-Verzeichnisse' + body = textwrap.dedent('''\ + Die folgenden Home-Verzeichnisse befinden sich weder + in der lokalen passwd-Datei, im LDAP oder in der exclude-Liste. + Sie können damit archiviert und gelöscht werden.''') + body += '\n\n' + for home in self.unnecessary_dirs: + body += ' - ' + home + '\n' + + self.send_mail(subject, body) + + # ============================================================================= if __name__ == "__main__":