From c147f844c536bee5a55142c5290887c09bf8f207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Poulain?= Date: Mon, 24 Aug 2020 18:56:58 +0200 Subject: [PATCH] ajoute un mode dry-run --- mailing.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/mailing.py b/mailing.py index a016574..a8108f1 100755 --- a/mailing.py +++ b/mailing.py @@ -52,6 +52,11 @@ ma.add_argument( help="Template of the mail to be sent", ) +parser.add_argument( + "-d", "--dry-run", action="store_true", help="Load data but don't send anything.", +) + + parser.add_argument( "-a", "--attachement", @@ -130,7 +135,7 @@ def qencode_subject(message): return message.replace(subjectstr, qsubjectstr, 1) -def send_message(message, to, attachment): +def send_message(message, to, attachment, args): lines = message.split("\n") @@ -188,11 +193,12 @@ def send_message(message, to, attachment): % (subjectvalue, fromvalue, dests[0], repr(dests)) ) - # sending the mail to its recipients using the local mailer via SMTP - server = smtplib.SMTP("localhost", "25") - server.set_debuglevel(1) - server.send_message(msg) - server.quit() + if not args.dry_run: + # sending the mail to its recipients using the local mailer via SMTP + server = smtplib.SMTP("localhost", "25") + server.set_debuglevel(1) + server.send_message(msg) + server.quit() if __name__ == "__main__": @@ -228,7 +234,7 @@ if __name__ == "__main__": body = replace_values(bodytemplate, values) # send message - send_message(body, recipient, attachment) + send_message(body, recipient, attachment, args) # pause every 10 mails for 5 secs so that the MTA doesn't explode counter = counter + 1