Skip to content

Commit

Permalink
email: don't send emails when notifications are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppe-steduto committed Aug 24, 2023
1 parent dd3d0ba commit 3343ce3
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion reana_commons/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from reana_commons.errors import REANAEmailNotificationError

# Email configuration
REANA_NOTIFICATIONS_ENABLED = bool(
strtobool(os.getenv("REANA_NOTIFICATIONS_ENABLED", "True"))
)
REANA_EMAIL_SMTP_SERVER = os.getenv("REANA_EMAIL_SMTP_SERVER")
REANA_EMAIL_SMTP_PORT = os.getenv("REANA_EMAIL_SMTP_PORT")
REANA_EMAIL_LOGIN = os.getenv("REANA_EMAIL_LOGIN")
Expand All @@ -35,13 +38,32 @@ def send_email(
login_email=REANA_EMAIL_LOGIN,
sender_email=REANA_EMAIL_SENDER,
):
"""Send emails from REANA platform."""
"""Send emails from REANA platform.
:param receiver_email: Email address of the receiver.
:param subject: Subject of the email.
:param body: Body of the email.
:param login_email: Email address of the logged user.
:param sender_email: Email address of the sender.
:raises REANAEmailNotificationError: If email cannot be sent, e.g. due to
missing configuration.
"""
message = EmailMessage()
message["From"] = f"REANA platform <{sender_email}>"
message["To"] = receiver_email
message["Subject"] = subject
message.set_content(body)

if not REANA_NOTIFICATIONS_ENABLED:
logging.info(
"An email was about to be sent, but REANA notifications are disabled, "
"therefore it won't be dispatched. \n\nMAIL INFO\n"
f"Login: {login_email}, "
f"sender: {sender_email}, receiver: {receiver_email}\n"
f"Body:\n{message}"
)
return

if not (REANA_EMAIL_SMTP_SERVER and REANA_EMAIL_SMTP_PORT):
raise REANAEmailNotificationError(
"Cannot send email, missing server and port configuration. "
Expand Down

0 comments on commit 3343ce3

Please sign in to comment.