Skip to content
Snippets Groups Projects
Commit 49fce323 authored by Charlotte Hausman's avatar Charlotte Hausman
Browse files

notification fixes for production

parent 8bbb9ce5
No related branches found
No related tags found
3 merge requests!1408catch up with 2.8.1.1, pin zope.sqlalchemy to 2.0,!1405merge 2.8.1.1 to main,!1397notification fixes for production
Pipeline #10191 passed
......@@ -30,7 +30,7 @@ with open("contacts_wrest/_version.py", "r") as vfile:
README = Path("README.md").read_text()
requires = ["dsnparse", "mysqlclient", "psycopg2", "pycapo", "pymysql"]
requires = ["dsnparse==0.1.15", "mysqlclient", "psycopg2", "pycapo", "pymysql"]
setup(
name="ssa-" + Path().absolute().name,
......
......@@ -35,6 +35,7 @@ requires = [
"requests",
"transaction",
"immutable_views",
"email-validator==2.0.0",
]
setup(
......
......@@ -26,6 +26,7 @@ import smtplib
from typing import Dict
import requests
from email_validator import validate_email, EmailNotValidError
from pycapo import CapoConfig
from workspaces.notification.services.interfaces import (
......@@ -76,32 +77,43 @@ class NotificationService(NotificationServiceIF):
if not receiver_emails:
return "No PI email found."
logger.info(f">> In send_email, sending email to: {receiver_emails}")
logger.info(f">> The parameters are: {parameters}")
template = self.info.lookup_template(template_name)
# verify email addresses are valid. Drop invalid addresses
for address in receiver_emails:
try:
validate_email(address)
except EmailNotValidError as e:
logger.info(f"Invalid email address {address} found, removing from receivers list." + e)
receiver_emails.remove(address)
if template is None:
logger.info("No template '" + template_name + "' found.")
if len(receiver_emails) > 0:
logger.info(f">> In send_email, sending email to: {receiver_emails}")
logger.info(f">> The parameters are: {parameters}")
template = self.info.lookup_template(template_name)
file = template.render(parameters)
if template is None:
logger.info("No template '" + template_name + "' found.")
# Load the headers and email content from the template
msg = email.message_from_bytes(file.content)
file = template.render(parameters)
# Attach the 'from', 'to', and 'cc' headers if they weren't set in the template
if msg["From"] is None:
msg["From"] = sender_email
# Load the headers and email content from the template
msg = email.message_from_bytes(file.content)
if msg["To"] is None:
msg["To"] = ', '.join(receiver_emails)
# Attach the 'from', 'to', and 'cc' headers if they weren't set in the template
if msg["From"] is None:
msg["From"] = sender_email
if msg["Cc"] is None and "cc_email" in parameters and parameters["cc_email"] is not None:
if isinstance(parameters["cc_email"], list):
msg["Cc"] = ', '.join(parameters["cc_email"])
else:
msg["Cc"] = parameters["cc_email"]
if msg["To"] is None:
msg["To"] = ", ".join(receiver_emails)
with smtplib.SMTP(smtp_server) as server:
r = server.send_message(msg)
if msg["Cc"] is None and "cc_email" in parameters and parameters["cc_email"] is not None:
if isinstance(parameters["cc_email"], list):
msg["Cc"] = ", ".join(parameters["cc_email"])
else:
msg["Cc"] = parameters["cc_email"]
return r
with smtplib.SMTP(smtp_server) as server:
r = server.send_message(msg)
return r
else:
logger.info("No valid email addresses provided. Skipping sending mail.")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment