Skip to content

Commit

Permalink
Fixed #162: Email sending has been broken
Browse files Browse the repository at this point in the history
  • Loading branch information
m-i-l committed Nov 2, 2024
1 parent 57ae81c commit ff0579a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/indexing/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
8 changes: 3 additions & 5 deletions src/web/content/dynamic/searchmysite/adminutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit ff0579a

Please sign in to comment.