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. " 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"