Skip to content

Commit

Permalink
make sure sl_formataddr always return str (#1269)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenkims authored Sep 5, 2022
1 parent 313a928 commit 192d03f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/email_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,7 @@ def get_verp_info_from_email(email: str) -> Optional[Tuple[VerpType, int]]:


def sl_formataddr(name_address_tuple: Tuple[str, str]):
"""Same as formataddr but use utf-8 encoding by default"""
"""Same as formataddr but use utf-8 encoding by default and always return str (and never Header)"""
name, addr = name_address_tuple
return formataddr((name, Header(addr, "utf-8")))
# formataddr can return Header, make sure to convert to str
return str(formataddr((name, Header(addr, "utf-8"))))
4 changes: 4 additions & 0 deletions tests/test_email_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,10 @@ def test_add_header_multipart_with_invalid_part():


def test_sl_formataddr():
# when the name part (first element in the tuple) is empty, formataddr() returns a Header
# this makes sure sl_formataddr always returns str
assert sl_formataddr(("", "[email protected]")) == "[email protected]"

assert sl_formataddr(("é", "è@ç.à")) == "=?utf-8?b?w6k=?= <è@ç.à>"
# test that the same name-address can't be handled by the built-in formataddr
with pytest.raises(UnicodeEncodeError):
Expand Down

0 comments on commit 192d03f

Please sign in to comment.