From 2f604a23d3933d0a21bf3327dc0f5f0a0cc38e84 Mon Sep 17 00:00:00 2001 From: skandrigi Date: Wed, 6 Nov 2024 14:55:24 -0600 Subject: [PATCH] emails were double sending previously, make small change to resolve this --- hiss/shared/admin_functions.py | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/hiss/shared/admin_functions.py b/hiss/shared/admin_functions.py index fc0aaac9..85f1de6e 100644 --- a/hiss/shared/admin_functions.py +++ b/hiss/shared/admin_functions.py @@ -11,39 +11,28 @@ def __init__(self, subject, text_content, html_content, from_email, recipient_li self.from_email = from_email self.recipient_list = recipient_list self.connection = connection + self.result = 0 def run(self): email = EmailMultiAlternatives(self.subject, self.text_content, self.from_email, self.recipient_list) email.attach_alternative(self.html_content, "text/html") - email.send(fail_silently=False, connection=self.connection) - #store results of threading for testing mass emails try: self.result = email.send(fail_silently=False, connection=self.connection) except Exception as e: - print("Error: ", e) + print("Error sending email: ", e) self.result = 0 -def send_mass_html_mail( - datatuple, fail_silently=False, user=None, password=None, connection=None -): +def send_mass_html_mail(datatuple, fail_silently=False, user=None, password=None, connection=None): """ - Given a datatuple of (subject, text_content, html_content, from_email, - recipient_list), sends each message to each recipient list. Returns the - number of emails sent. - - If from_email is None, the DEFAULT_FROM_EMAIL setting is used. - If auth_user and auth_password are set, they're used to log in. - If auth_user is None, the EMAIL_HOST_USER setting is used. - If auth_password is None, the EMAIL_HOST_PASSWORD setting is used. - - (from this StackOverflow answer + Sends each message in datatuple (subject, text_content, html_content, from_email, recipient_list). + Returns the number of emails sent. """ connection = connection or get_connection( username=user, password=password, fail_silently=fail_silently ) threads = [] - + for subject, text, html, from_email, recipient in datatuple: email_thread = MassEmailThread(subject, text, html, from_email, recipient, connection) email_thread.start() @@ -51,8 +40,7 @@ def send_mass_html_mail( for thread in threads: thread.join() - - total = sum(thread.result for thread in threads) - - # see how many emails sent successfully - return total \ No newline at end of file + + total_sent = sum(thread.result for thread in threads if thread.result) + + return total_sent \ No newline at end of file