Skip to content

Commit

Permalink
fix: unknown charset windows-874 problem on incoming mail
Browse files Browse the repository at this point in the history
When the sender is using email with windows-874 charset (i.e., Outlook / Thai), the incoming email, i.e., to Issue will result in weird characters.

This is due to, python don't know about this charset. This fix by using alias charset for the problematic charset.

(cherry picked from commit 69f9db6)

# Conflicts:
#	frappe/email/receive.py
  • Loading branch information
kittiu authored and mergify[bot] committed Apr 22, 2024
1 parent ddae585 commit 86eff33
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion frappe/email/receive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import datetime
import email
import email.charset
import email.utils
import imaplib
import json
Expand Down Expand Up @@ -38,6 +39,13 @@
from frappe.utils.html_utils import clean_email_html
from frappe.utils.user import is_system_user

# use alias charset for python unknown charset
email.charset.ALIASES.update(
{
"windows-874": "cp874",
}
)

# fix due to a python bug in poplib that limits it to 2048
poplib._MAXLINE = 1_00_000

Expand Down Expand Up @@ -408,11 +416,20 @@ def set_subject(self):
"""Parse and decode `Subject` header."""
_subject = decode_header(self.mail.get("Subject", "No Subject"))
self.subject = _subject[0][0] or ""
<<<<<<< HEAD
if _subject[0][1]:
self.subject = safe_decode(self.subject, _subject[0][1])
else:
# assume that the encoding is utf-8
self.subject = safe_decode(self.subject)[:140]
=======
charset = _subject[0][1]

if charset:
# Encoding is known by decode_header (might also be unknown-8bit)
charset = email.charset.ALIASES.get(charset, charset)
self.subject = safe_decode(self.subject, charset)
>>>>>>> 69f9db6751 (fix: unknown charset windows-874 problem on incoming mail)

if not self.subject:
self.subject = "No Subject"
Expand Down Expand Up @@ -507,7 +524,7 @@ def get_charset(self, part):

def get_payload(self, part):
charset = self.get_charset(part)

charset = email.charset.ALIASES.get(charset, charset)
try:
return str(part.get_payload(decode=True), str(charset), "ignore")
except LookupError:
Expand Down

0 comments on commit 86eff33

Please sign in to comment.