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

Fix error message on email notification #2391

Merged
merged 2 commits into from
Jan 21, 2025
Merged
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
21 changes: 12 additions & 9 deletions notifications/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def run(self):
server.quit()
logger.info("E-mails were sent successfully.")
except Exception as exc:
logger.error("Could not send emails with Python smtlib, exception: {} -- {}".format(type(exc).__name__, exc.args))
logger.error("Could not send emails with Python smtlib", exc_info=True)
ex = ""
try:
ex = str(exc.args)
except Exception as exctwo:
logger.error(exctwo.args)
logger.warning(exctwo.args)
cron_rec = {
"name": "notification",
"message": "Error sending out email with Python smtplib: {}".format(ex),
Expand Down Expand Up @@ -92,6 +92,9 @@ def send_notification(subject, recipients, html, mailtype="", files=None):
SendMail(recipients, msg).start()
return

if "?" not in settings.EMAIL_API_ENDPOINT: # a.k.a dirty disabling email sending
return

to_addresses = recipients if isinstance(recipients, list) else [recipients]

# if not IS_PROD:
Expand Down Expand Up @@ -152,15 +155,15 @@ def send_notification(subject, recipients, html, mailtype="", files=None):
)

logger.info("E-mails were sent successfully.")
elif res.status_code == 401 or res.status_code == 403:
# Try sending with Python smtplib, if reaching the API fails
logger.error(f"Authorization/authentication failed ({res.status_code}) to the e-mail sender API.")
msg = construct_msg(subject, html)
SendMail(to_addresses, msg).start()
else:
logger.error(
f"Email send failed using API, status code: ({res.status_code})",
extra={
"content": res.content,
},
)
# Try sending with Python smtplib, if reaching the API fails
logger.error("Could not reach the e-mail sender API. Trying with Python smtplib...")
logger.warning(f"Authorization/authentication failed ({res.status_code}) to the e-mail sender API.")
msg = construct_msg(subject, html)
SendMail(to_addresses, msg).start()

return res.text
Loading