diff --git a/mailmerge/__main__.py b/mailmerge/__main__.py index d9648db..77873dc 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): """ @@ -116,6 +120,7 @@ def main(sample, dry_run, limit, no_limit, 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) @@ -124,20 +129,42 @@ 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: + sent=True break - time.sleep(1) 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