From 81671a86a8cf8979ed6c9a2c5e280622361bb27c Mon Sep 17 00:00:00 2001 From: Giuseppe Steduto Date: Thu, 24 Aug 2023 10:15:21 +0200 Subject: [PATCH 1/2] email: don't send emails when notifications are disabled Closes reanahub/reana#736. --- reana_commons/email.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/reana_commons/email.py b/reana_commons/email.py index aa9c6430..dbf1d777 100644 --- a/reana_commons/email.py +++ b/reana_commons/email.py @@ -17,10 +17,14 @@ 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") REANA_EMAIL_SENDER = os.getenv("REANA_EMAIL_SENDER") +REANA_EMAIL_RECEIVER = os.getenv("REANA_EMAIL_RECEIVER") REANA_EMAIL_PASSWORD = os.getenv("REANA_EMAIL_PASSWORD") REANA_EMAIL_SMTP_SSL = bool(strtobool(os.getenv("REANA_EMAIL_SMTP_SSL", "False"))) REANA_EMAIL_SMTP_STARTTLS = bool( @@ -35,13 +39,28 @@ 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: + raise REANAEmailNotificationError( + "An email was about to be sent, but REANA notifications are disabled, " + "therefore it won't be dispatched." + ) + if not (REANA_EMAIL_SMTP_SERVER and REANA_EMAIL_SMTP_PORT): raise REANAEmailNotificationError( "Cannot send email, missing server and port configuration. " From 0fa088a4e912742cdc80eac758762b6d01650cef Mon Sep 17 00:00:00 2001 From: Giuseppe Steduto Date: Mon, 4 Sep 2023 16:09:15 +0200 Subject: [PATCH 2/2] release: 0.9.3a6 --- reana_commons/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reana_commons/version.py b/reana_commons/version.py index a418f390..5cc9689f 100755 --- a/reana_commons/version.py +++ b/reana_commons/version.py @@ -14,4 +14,4 @@ from __future__ import absolute_import, print_function -__version__ = "0.9.3a5" +__version__ = "0.9.3a6"