From a916cf4b2b78b264a417b82f15a1899202faf586 Mon Sep 17 00:00:00 2001 From: Frank Breedijk Date: Wed, 2 Jun 2021 14:32:06 +0200 Subject: [PATCH 1/2] Added a flag to not break off merge on error --- mailmerge/__main__.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/mailmerge/__main__.py b/mailmerge/__main__.py index d9648db..50fc9c2 100644 --- a/mailmerge/__main__.py +++ b/mailmerge/__main__.py @@ -51,6 +51,10 @@ type=click.IntRange(0, None), help="Limit the number of messages (1)", ) +@click.option( + "--fail-on-error/--no-fail-on-error", default=True, + help="Do (not) break of the run if there is an error", +) @click.option( "--resume", is_flag=False, default=1, type=click.IntRange(1, None), @@ -80,7 +84,7 @@ type=click.Choice(["colorized", "text", "raw"]), help="Output format (colorized).", ) -def main(sample, dry_run, limit, no_limit, resume, +def main(sample, dry_run, limit, no_limit, fail_on_error, resume, template_path, database_path, config_path, output_format): """ @@ -124,9 +128,23 @@ def main(sample, dry_run, limit, no_limit, resume, ">>> rate limit exceeded, waiting ...", output_format, ) + time.sleep(1) + except exceptions.MailmergeError as error: + if fail_on_error: + raise exceptions.MailmergeError(error) + else: + print_bright_white_on_cyan( + ">>> Error on message {message_num}\n" + "{error}" + .format( + message_num=message_num, + error=error, + ), + output_format, + ) + break else: break - time.sleep(1) print_bright_white_on_cyan( ">>> message {message_num}" .format(message_num=message_num), From e75ecdc17656cde80af64e989eda03e90c07d65a Mon Sep 17 00:00:00 2001 From: Frank Breedijk Date: Wed, 2 Jun 2021 15:33:16 +0200 Subject: [PATCH 2/2] Handle more errors and be clear about not sending --- mailmerge/__main__.py | 21 +++++++++++++++------ mailmerge/sendmail_client.py | 4 ++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/mailmerge/__main__.py b/mailmerge/__main__.py index 50fc9c2..77873dc 100644 --- a/mailmerge/__main__.py +++ b/mailmerge/__main__.py @@ -120,6 +120,7 @@ def main(sample, dry_run, limit, no_limit, fail_on_error, resume, for _, row in enumerate_range(csv_database, start, stop): sender, recipients, message = template_message.render(row) + sent=False while True: try: sendmail_client.sendmail(sender, recipients, message) @@ -144,18 +145,26 @@ def main(sample, dry_run, limit, no_limit, fail_on_error, resume, ) break else: + sent=True break print_bright_white_on_cyan( ">>> message {message_num}" .format(message_num=message_num), output_format, ) - print_message(message, output_format) - print_bright_white_on_cyan( - ">>> message {message_num} sent" - .format(message_num=message_num), - output_format, - ) + if sent: + print_message(message, output_format) + print_bright_white_on_cyan( + ">>> message {message_num} sent" + .format(message_num=message_num), + output_format, + ) + else: + print_bright_white_on_cyan( + "!!! message {message_num} NOT sent" + .format(message_num=message_num), + output_format, + ) message_num += 1 except exceptions.MailmergeError as error: diff --git a/mailmerge/sendmail_client.py b/mailmerge/sendmail_client.py index 129f394..604ea05 100644 --- a/mailmerge/sendmail_client.py +++ b/mailmerge/sendmail_client.py @@ -130,6 +130,10 @@ def sendmail(self, sender, recipients, message): "{}:{} failed to connect to server: {}" .format(host, port, err) ) + except Exception as e: + raise exceptions.MailmergeError( + e + ) # Update timestamp of last sent message self.lastsent = now