From ff0579a23781b539eb40ec7d3c8796e47e53e5c2 Mon Sep 17 00:00:00 2001 From: Michael Lewis Date: Sat, 2 Nov 2024 17:14:54 +0000 Subject: [PATCH] Fixed #162: Email sending has been broken --- src/indexing/common/utils.py | 12 ++++++------ src/web/content/dynamic/searchmysite/adminutils.py | 8 +++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/indexing/common/utils.py b/src/indexing/common/utils.py index 182aa87..afd3eb3 100644 --- a/src/indexing/common/utils.py +++ b/src/indexing/common/utils.py @@ -577,10 +577,12 @@ def get_latest_available_wikipedia_export(dump_location): # reply_to_email and to_email optional. # If reply_to_email None no Reply-To header set, and if to_email None then smtp_to_email env variable is used -# IMPORTANT: This function is in both indexing/common/utils.py and web/content/dynamic/searchmysite/util.py +# IMPORTANT: This function is in both indexing/common/utils.py and web/content/dynamic/searchmysite/adminutils.py # so if it is updated in one it should be updated in the other def send_email(reply_to_email, to_email, subject, text): + logger = logging.getLogger() success = True + server = None if not to_email: recipients = [smtp_to_email] else: @@ -595,16 +597,14 @@ def send_email(reply_to_email, to_email, subject, text): message["CC"] = smtp_to_email # Always cc the smtp_to_email env variable message["Subject"] = subject message.attach(MIMEText(text, "plain")) - server = smtplib.SMTP(smtp_server, smtp_port) + server = smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) #server.set_debuglevel(1) - server.starttls(context=context) # Secure the connection server.login(smtp_from_email, smtp_from_password) server.sendmail(smtp_from_email, recipients, message.as_string()) except Exception as e: success = False - #current_app.logger.error('Error sending email: {}'.format(e)) - logger = logging.getLogger() logger.error('Error sending email: {}'.format(e)) finally: - server.quit() + if server is not None: + server.quit() return success diff --git a/src/web/content/dynamic/searchmysite/adminutils.py b/src/web/content/dynamic/searchmysite/adminutils.py index 61ba4d5..da69f51 100644 --- a/src/web/content/dynamic/searchmysite/adminutils.py +++ b/src/web/content/dynamic/searchmysite/adminutils.py @@ -122,9 +122,10 @@ def delete_domain_from_database(domain): # reply_to_email and to_email optional. # If reply_to_email None no Reply-To header set, and if to_email None then smtp_to_email env variable is used -# IMPORTANT: This function is in both indexing/common/utils.py and web/content/dynamic/searchmysite/util.py +# IMPORTANT: This function is in both indexing/common/utils.py and web/content/dynamic/searchmysite/adminutils.py # so if it is updated in one it should be updated in the other def send_email(reply_to_email, to_email, subject, text): + logger = logging.getLogger() success = True server = None if not to_email: @@ -141,15 +142,12 @@ def send_email(reply_to_email, to_email, subject, text): message["CC"] = smtp_to_email # Always cc the smtp_to_email env variable message["Subject"] = subject message.attach(MIMEText(text, "plain")) - server = smtplib.SMTP(smtp_server, smtp_port) + server = smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) #server.set_debuglevel(1) - server.starttls(context=context) # Secure the connection server.login(smtp_from_email, smtp_from_password) server.sendmail(smtp_from_email, recipients, message.as_string()) except Exception as e: success = False - #current_app.logger.error('Error sending email: {}'.format(e)) - logger = logging.getLogger() logger.error('Error sending email: {}'.format(e)) finally: if server is not None: