Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continue on error and log to a file #124

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions mailmerge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions mailmerge/sendmail_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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