diff --git a/app/delivery/send_to_providers.py b/app/delivery/send_to_providers.py index db0b92f01f..e39872bcb0 100644 --- a/app/delivery/send_to_providers.py +++ b/app/delivery/send_to_providers.py @@ -187,7 +187,7 @@ def check_service_over_bounce_rate(service_id: str): ) -def mime_encoded_word_syntax(charset="utf-8", encoding="B", encoded_text="") -> str: +def mime_encoded_word_syntax(encoded_text="", charset="utf-8", encoding="B") -> str: """MIME encoded-word syntax is a way to encode non-ASCII characters in email headers. It is described here: https://docs.aws.amazon.com/ses/latest/dg/send-email-raw.html#send-email-mime-encoding-headers @@ -205,7 +205,7 @@ def get_from_address(friendly_from: str, email_from: str, sending_domain: str) - name using MIME encoded-word syntax, as described in Sending raw email using the Amazon SES API." """ friendly_from_b64 = base64.b64encode(friendly_from.encode()).decode("utf-8") - friendly_from_mime = mime_encoded_word_syntax(charset="utf-8", encoding="B", encoded_text=friendly_from_b64) + friendly_from_mime = mime_encoded_word_syntax(encoded_text=friendly_from_b64, charset="utf-8", encoding="B") return f'"{friendly_from_mime}" <{unidecode(email_from)}@{unidecode(sending_domain)}>' diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index 7054f6ea5d..1483603838 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -1221,3 +1221,17 @@ def test_check_service_over_bounce_rate_normal(self, mocker: MockFixture, notify mock_logger = mocker.patch("app.notifications.validators.current_app.logger.warning") assert send_to_providers.check_service_over_bounce_rate(fake_uuid) is None mock_logger.assert_not_called() + + +@pytest.mark.parametrize("encoded_text, charset, encoding, expected", + [ + ("hello_world", "utf-8", "B", "=?utf-8?B?hello_world?="), + ("hello_world", "utf-8", "Q", "=?utf-8?Q?hello_world?="), + ("hello_world2", "utf-8", "B", "=?utf-8?B?hello_world2?="), + ], +) +def test_mime_encoded_word_syntax_encoding(encoded_text, charset, encoding, expected): + result = send_to_providers.mime_encoded_word_syntax( + encoded_text=encoded_text, charset=charset, encoding=encoding + ) + assert result == expected