summaryrefslogtreecommitdiff
path: root/odoo/tools/mail.py
diff options
context:
space:
mode:
authorJulien Castiaux <juc@odoo.com>2019-10-29 14:06:18 +0000
committerJulien Castiaux <juc@odoo.com>2019-11-12 13:48:40 +0000
commit7b65a63c97d185d9b6e6f1b0bfbc7968d1e4de49 (patch)
treed4972ef2ee16ab5bcb5f1a6b1c9b725aad917a72 /odoo/tools/mail.py
parentf0ae04ab3ddeb05ad02c0358ae174993b19a4c15 (diff)
[IMP] tools: restore P2 formataddr default behavior
Python 2 `email.tools.formataddr` doesn't encode not RFC-2822 compliant realname to base64 or quoted-printable. This is the wanted behavior to output formatted email addresses on screen. Python 3 `email.tools.formataddr` does encode the realname to be RFC-2822 compliant. This is the wanted behavior when connecting to a smtp server to send emails. That method has been deprecated by pep-594 as the new python email API is capable of automatically formatting email address in a RFC compliant way. As the method was still in use in a lot of modules as the preferred way to format email addresses, a refined P3-like implementation as been included in the tool suite. This commit changes the default behavior so it mimics P2 implementation with an easy way to use the P3 behavior. Task: 2003936
Diffstat (limited to 'odoo/tools/mail.py')
-rw-r--r--odoo/tools/mail.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/odoo/tools/mail.py b/odoo/tools/mail.py
index 3e881d0d841..03ac8760ece 100644
--- a/odoo/tools/mail.py
+++ b/odoo/tools/mail.py
@@ -516,9 +516,10 @@ def email_escape_char(email_address):
def decode_message_header(message, header, separator=' '):
return separator.join(h for h in message.get_all(header, []) if h)
-def formataddr(pair):
- """Takes a 2-tuple of the form (realname, email_address) and returns
- the string value suitable for an RFC 2822 From, To or Cc header.
+def formataddr(pair, charset='utf-8'):
+ """Pretty format a 2-tuple of the form (realname, email_address).
+
+ Set the charset to ascii to get a RFC-2822 compliant email.
The email address is considered valid and is left unmodified.
@@ -535,9 +536,9 @@ def formataddr(pair):
address.encode('ascii')
if name:
try:
- name.encode('ascii')
+ name.encode(charset)
except UnicodeEncodeError:
- # non-ascii name, transcode the name in a safe format
+ # charset mismatch, encode as utf-8/base64
# rfc2047 - MIME Message Header Extensions for Non-ASCII Text
return "=?utf-8?b?{name}?= <{addr}>".format(
name=base64.b64encode(name.encode('utf-8')).decode('ascii'),