From 14e2307d1c5335effbe679b2e3c7e7094bcbbbf8 Mon Sep 17 00:00:00 2001 From: Euan Date: Tue, 2 Apr 2024 12:01:13 +0200 Subject: [PATCH 1/2] allowing the email mutator to update recipients --- superset/utils/core.py | 2 ++ tests/integration_tests/email_tests.py | 30 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/superset/utils/core.py b/superset/utils/core.py index de1034ddb06b0..1b565c23f26d9 100644 --- a/superset/utils/core.py +++ b/superset/utils/core.py @@ -902,6 +902,8 @@ def send_email_smtp( # pylint: disable=invalid-name,too-many-arguments,too-many msg_mutator = config["EMAIL_HEADER_MUTATOR"] # the base notification returns the message without any editing. new_msg = msg_mutator(msg, **(header_data or {})) + if new_msg["To"].split(", ") != recipients: + recipients = new_msg["To"].split(", ") send_mime_email(smtp_mail_from, recipients, new_msg, config, dryrun=dryrun) diff --git a/tests/integration_tests/email_tests.py b/tests/integration_tests/email_tests.py index 7c7cc1683089f..83ff21925818b 100644 --- a/tests/integration_tests/email_tests.py +++ b/tests/integration_tests/email_tests.py @@ -89,6 +89,36 @@ def mutator(msg, **kwargs): assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload() app.config["EMAIL_HEADER_MUTATOR"] = base_email_mutator + @mock.patch("superset.utils.core.send_mime_email") + def test_send_smtp_with_email_mutator_changing_recipients(self, mock_send_mime): + attachment = tempfile.NamedTemporaryFile() + attachment.write(b"attachment") + attachment.seek(0) + + # putting this into a variable so that we can reset after the test + base_email_mutator = app.config["EMAIL_HEADER_MUTATOR"] + + def mutator(msg, **kwargs): + msg.replace_header("To", "mutated") + return msg + + app.config["EMAIL_HEADER_MUTATOR"] = mutator + utils.send_email_smtp( + "to", "subject", "content", app.config, files=[attachment.name] + ) + assert mock_send_mime.called + call_args = mock_send_mime.call_args[0] + logger.debug(call_args) + assert call_args[0] == app.config["SMTP_MAIL_FROM"] + assert call_args[1] == ["mutated"] + msg = call_args[2] + assert msg["Subject"] == "subject" + assert msg["From"] == app.config["SMTP_MAIL_FROM"] + assert len(msg.get_payload()) == 2 + mimeapp = MIMEApplication("attachment") + assert msg.get_payload()[-1].get_payload() == mimeapp.get_payload() + app.config["EMAIL_HEADER_MUTATOR"] = base_email_mutator + @mock.patch("superset.utils.core.send_mime_email") def test_send_smtp_data(self, mock_send_mime): utils.send_email_smtp( From 9fd99a881f32b8bfd39a5fec1a6ef22f9faf0abc Mon Sep 17 00:00:00 2001 From: Euan Date: Wed, 3 Apr 2024 10:37:22 +0200 Subject: [PATCH 2/2] updated recipients check --- superset/utils/core.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/superset/utils/core.py b/superset/utils/core.py index 1b565c23f26d9..5df896ef599a8 100644 --- a/superset/utils/core.py +++ b/superset/utils/core.py @@ -852,6 +852,7 @@ def send_email_smtp( # pylint: disable=invalid-name,too-many-arguments,too-many msg["CC"] = ", ".join(smtp_mail_cc) recipients = recipients + smtp_mail_cc + smtp_mail_bcc = [] if bcc: # don't add bcc in header smtp_mail_bcc = get_email_address_list(bcc) @@ -902,8 +903,11 @@ def send_email_smtp( # pylint: disable=invalid-name,too-many-arguments,too-many msg_mutator = config["EMAIL_HEADER_MUTATOR"] # the base notification returns the message without any editing. new_msg = msg_mutator(msg, **(header_data or {})) - if new_msg["To"].split(", ") != recipients: - recipients = new_msg["To"].split(", ") + new_to = new_msg["To"].split(", ") if "To" in new_msg else [] + new_cc = new_msg["Cc"].split(", ") if "Cc" in new_msg else [] + new_recipients = new_to + new_cc + smtp_mail_bcc + if set(new_recipients) != set(recipients): + recipients = new_recipients send_mime_email(smtp_mail_from, recipients, new_msg, config, dryrun=dryrun)