use 7bit as default encoding
This commit is contained in:
parent
1241838b26
commit
d61f45ea86
|
@ -677,27 +677,30 @@ def get_encoding(msg: Message) -> str:
|
|||
"""
|
||||
Return the message encoding, possible values:
|
||||
- quoted-printable
|
||||
- base64: default if unknown
|
||||
- base64
|
||||
- 7bit: default if unknown or empty
|
||||
"""
|
||||
cte = str(msg.get("content-transfer-encoding", "")).lower()
|
||||
if cte == "":
|
||||
return "base64"
|
||||
if cte in ("", "7bit"):
|
||||
return "7bit"
|
||||
|
||||
if cte == "quoted-printable" or cte == "base64":
|
||||
if cte in ("quoted-printable", "base64"):
|
||||
return cte
|
||||
|
||||
LOG.exception("Unknown encoding %s", cte)
|
||||
|
||||
return "base64"
|
||||
return "7bit"
|
||||
|
||||
|
||||
def encode_text(text: str, encoding: str = "base64") -> str:
|
||||
def encode_text(text: str, encoding: str = "7bit") -> str:
|
||||
if encoding == "quoted-printable":
|
||||
encoded = quopri.encodestring(text.encode("utf-8"))
|
||||
return str(encoded, "utf-8")
|
||||
else: # use base64 by default
|
||||
elif encoding == "base64":
|
||||
encoded = base64.b64encode(text.encode("utf-8"))
|
||||
return str(encoded, "utf-8")
|
||||
else: # 7bit - no encoding
|
||||
return text
|
||||
|
||||
|
||||
def add_header(msg: Message, text_header, html_header) -> Message:
|
||||
|
|
|
@ -422,19 +422,24 @@ def test_normalize_reply_email(flask_client):
|
|||
|
||||
def test_get_encoding():
|
||||
msg = email.message_from_string("")
|
||||
assert get_encoding(msg) == "base64"
|
||||
assert get_encoding(msg) == "7bit"
|
||||
|
||||
msg = email.message_from_string("Content-TRANSFER-encoding: Invalid")
|
||||
assert get_encoding(msg) == "base64"
|
||||
assert get_encoding(msg) == "7bit"
|
||||
|
||||
msg = email.message_from_string("Content-TRANSFER-encoding: quoted-printable")
|
||||
assert get_encoding(msg) == "quoted-printable"
|
||||
|
||||
msg = email.message_from_string("Content-TRANSFER-encoding: base64")
|
||||
assert get_encoding(msg) == "base64"
|
||||
|
||||
|
||||
def test_encode_text():
|
||||
assert encode_text("") == ""
|
||||
assert encode_text("ascii") == "YXNjaWk="
|
||||
assert encode_text("ascii") == "ascii"
|
||||
assert encode_text("ascii", "base64") == "YXNjaWk="
|
||||
assert encode_text("ascii", "quoted-printable") == "ascii"
|
||||
|
||||
assert encode_text("mèo méo") == "bcOobyBtw6lv"
|
||||
assert encode_text("mèo méo") == "mèo méo"
|
||||
assert encode_text("mèo méo", "base64") == "bcOobyBtw6lv"
|
||||
assert encode_text("mèo méo", "quoted-printable") == "m=C3=A8o m=C3=A9o"
|
||||
|
|
Loading…
Reference in a new issue