ajoute un mode dry-run

This commit is contained in:
François Poulain 2020-08-24 18:56:58 +02:00
parent 73b87797c1
commit c147f844c5
1 changed files with 13 additions and 7 deletions

View File

@ -52,6 +52,11 @@ ma.add_argument(
help="Template of the mail to be sent", 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( parser.add_argument(
"-a", "-a",
"--attachement", "--attachement",
@ -130,7 +135,7 @@ def qencode_subject(message):
return message.replace(subjectstr, qsubjectstr, 1) return message.replace(subjectstr, qsubjectstr, 1)
def send_message(message, to, attachment): def send_message(message, to, attachment, args):
lines = message.split("\n") lines = message.split("\n")
@ -188,11 +193,12 @@ def send_message(message, to, attachment):
% (subjectvalue, fromvalue, dests[0], repr(dests)) % (subjectvalue, fromvalue, dests[0], repr(dests))
) )
# sending the mail to its recipients using the local mailer via SMTP if not args.dry_run:
server = smtplib.SMTP("localhost", "25") # sending the mail to its recipients using the local mailer via SMTP
server.set_debuglevel(1) server = smtplib.SMTP("localhost", "25")
server.send_message(msg) server.set_debuglevel(1)
server.quit() server.send_message(msg)
server.quit()
if __name__ == "__main__": if __name__ == "__main__":
@ -228,7 +234,7 @@ if __name__ == "__main__":
body = replace_values(bodytemplate, values) body = replace_values(bodytemplate, values)
# send message # 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 # pause every 10 mails for 5 secs so that the MTA doesn't explode
counter = counter + 1 counter = counter + 1