From 9694dfd119eda53eb40b5c22f7a7699376b82708 Mon Sep 17 00:00:00 2001 From: Norman Hooper Date: Fri, 7 Jun 2024 10:28:43 +0100 Subject: [PATCH 1/4] Ensure that SMTP message body is not empty Email notifications of both the start of a deploy, and of a failed deploy, have empty message bodies. Azure Communication Services rejects emails with empty message bodies with: > smtplib.SMTPDataError: (501, b'5.6.0 Request body validation error. Need either non-empty Html or PlainText body to be present.') --- src/commcare_cloud/commands/deploy/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/commcare_cloud/commands/deploy/utils.py b/src/commcare_cloud/commands/deploy/utils.py index da7e202563..a11b3911e4 100644 --- a/src/commcare_cloud/commands/deploy/utils.py +++ b/src/commcare_cloud/commands/deploy/utils.py @@ -100,7 +100,7 @@ def send_email(environment, subject, message='', to_admins=True, recipients=None """ Call a Django management command to send an email. - :param environment: The Environement object + :param environment: The Environment object :param subject: Email subject :param message: Email message :param to_admins: True if mail should be sent to Django admins @@ -108,6 +108,11 @@ def send_email(environment, subject, message='', to_admins=True, recipients=None """ if environment.fab_settings_config.email_enabled: print(color_summary(f">> Sending email: {subject}")) + + if not message: + # Azure Communication Services require a message body + message = subject + args = [ message, '--subject', subject, From d46308f0f72dfceabebc6e2ef883f4946f2929a1 Mon Sep 17 00:00:00 2001 From: Norman Hooper Date: Mon, 10 Jun 2024 11:15:39 +0100 Subject: [PATCH 2/4] Revert Ensure that SMTP message body is not empty This reverts commit 9694dfd119eda53eb40b5c22f7a7699376b82708. --- src/commcare_cloud/commands/deploy/utils.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/commcare_cloud/commands/deploy/utils.py b/src/commcare_cloud/commands/deploy/utils.py index a11b3911e4..da7e202563 100644 --- a/src/commcare_cloud/commands/deploy/utils.py +++ b/src/commcare_cloud/commands/deploy/utils.py @@ -100,7 +100,7 @@ def send_email(environment, subject, message='', to_admins=True, recipients=None """ Call a Django management command to send an email. - :param environment: The Environment object + :param environment: The Environement object :param subject: Email subject :param message: Email message :param to_admins: True if mail should be sent to Django admins @@ -108,11 +108,6 @@ def send_email(environment, subject, message='', to_admins=True, recipients=None """ if environment.fab_settings_config.email_enabled: print(color_summary(f">> Sending email: {subject}")) - - if not message: - # Azure Communication Services require a message body - message = subject - args = [ message, '--subject', subject, From 358dcce7f83f2328c2c9596e07b21f23402c5ee5 Mon Sep 17 00:00:00 2001 From: Norman Hooper Date: Mon, 10 Jun 2024 13:45:23 +0100 Subject: [PATCH 3/4] Set the subject to the message Set the subject to the message because the message is short. --- src/commcare_cloud/commands/deploy/utils.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/commcare_cloud/commands/deploy/utils.py b/src/commcare_cloud/commands/deploy/utils.py index da7e202563..d3ea46502c 100644 --- a/src/commcare_cloud/commands/deploy/utils.py +++ b/src/commcare_cloud/commands/deploy/utils.py @@ -60,27 +60,30 @@ def send_deploy_start_email(environment, context): and context.revision is not None ) env_name = environment.meta_config.deploy_env - subject = f"{context.user} has initiated a {context.service_name} deploy to {env_name}" + message = f"{context.user} has initiated a {context.service_name} deploy to {env_name}" prefix = "" if is_nonstandard_deploy_time: - subject += " outside maintenance window" + message += " outside maintenance window" prefix = "ATTENTION: " if is_non_default_branch: - subject += f" with non-default branch '{context.revision}'" + message += f" with non-default branch '{context.revision}'" prefix = "ATTENTION: " - subject = f"{prefix}{subject}" + message = f"{prefix}{message}" send_email( environment, - subject=subject, + subject=message, + message=message, ) def record_deploy_failed(environment, context): notify_slack_deploy_end(environment, context, is_success=False) + message = f"{context.service_name} deploy to {environment.name} failed" send_email( environment, - subject=f"{context.service_name} deploy to {environment.name} failed", + subject=message, + message=message, ) From 19547c9701d4d680ca6c55bbf22abe70ce12bd34 Mon Sep 17 00:00:00 2001 From: Norman Hooper Date: Wed, 19 Jun 2024 11:22:25 +0100 Subject: [PATCH 4/4] Alert the dev to a potential problem --- src/commcare_cloud/commands/deploy/utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/commcare_cloud/commands/deploy/utils.py b/src/commcare_cloud/commands/deploy/utils.py index d3ea46502c..4ad00ce800 100644 --- a/src/commcare_cloud/commands/deploy/utils.py +++ b/src/commcare_cloud/commands/deploy/utils.py @@ -99,16 +99,19 @@ def announce_deploy_success(environment, context): ) -def send_email(environment, subject, message='', to_admins=True, recipients=None): +def send_email(environment, subject, message, to_admins=True, recipients=None): """ Call a Django management command to send an email. - :param environment: The Environement object + :param environment: The Environment object :param subject: Email subject - :param message: Email message + :param message: Email message body :param to_admins: True if mail should be sent to Django admins :param recipients: List of additional addresses to send mail to """ + if not message: + raise ValueError('Some cloud hosting providers require a message body') + if environment.fab_settings_config.email_enabled: print(color_summary(f">> Sending email: {subject}")) args = [