MailTemplates: improve email header encoding
Use MIME encoded words also for names. https://en.wikipedia.org/wiki/Email#Message_header
... | @@ -15,8 +15,8 @@ try: | ... | @@ -15,8 +15,8 @@ try: |
except ImportError: | except ImportError: | ||
BaseMailTemplate = None | BaseMailTemplate = None | ||
from email.Header import make_header | from email.Header import Header | ||
from email.Utils import make_msgid | from email.Utils import make_msgid, formataddr, getaddresses | ||
if BaseMailTemplate is not None: | if BaseMailTemplate is not None: | ||
def _process_utf8(self,kw): | def _process_utf8(self,kw): | ||
... | @@ -60,14 +60,32 @@ if BaseMailTemplate is not None: | ... | @@ -60,14 +60,32 @@ if BaseMailTemplate is not None: |
headers.get(header)))) | headers.get(header)))) | ||
if value is not None: | if value is not None: | ||
values[key]=value | values[key]=value | ||
# turn some sequences in coma-seperated strings | # turn some sequences in coma-seperated strings | ||
if isinstance(value, (tuple, list)): | if isinstance(value, (tuple, list)): | ||
value = ', '.join(value) | value = ', '.join(value) | ||
# make sure we have no unicode headers | # make sure we have no unicode headers | ||
if isinstance(value,unicode): | if isinstance(value,unicode): | ||
value = value.encode(encoding) | value = value.encode(encoding) | ||
if key=='subject': | |||
value = make_header([(value, 'utf-8')]).encode() | if key == 'subject': | ||
try: | |||
# Try to keep header non encoded | |||
|
|||
value = Header(value.encode("ascii")) | |||
except UnicodeDecodeError: | |||
value = Header(value, "UTF-8") | |||
else: | |||
value_list = getaddresses([value]) | |||
dest_list = [] | |||
for name, email in value_list: | |||
try: | |||
name = Header(name.encode("ascii")) | |||
except UnicodeDecodeError: | |||
name = Header(name, "UTF-8") | |||
dest_list.append(formataddr((name.encode(), email))) | |||
value = ", ".join(dest_list) | |||
headers[header]=value | headers[header]=value | ||
# check required values have been supplied | # check required values have been supplied | ||
errors = [] | errors = [] | ||
... | ... |