fix: decode non-ascii characters for formataddr

formataddr[1] by default encodes non-ascii characters to `=?utf-8?q`, which needs to be decoded to a human-readable string format. using decode_header and make_header we can decode the string back to human-readable format.

before:

In [1]: formataddr(("Tèst", "notifications@erpnext.com"))
Out[1]: '=?utf-8?b?VMOoc3Q=?= <notifications@erpnext.com>'

after:

In [2]: make_header(decode_header(formataddr(("Tèst", "notifications@erpnext.com"))))
Out[2]: 'Tèst <notifications@erpnext.com>'

references:
[1]: https://docs.python.org/3/library/email.utils.html#email.utils.formataddr

Signed-off-by: Chinmay D. Pai <chinmaydpai@gmail.com>
This commit is contained in:
Chinmay D. Pai 2019-10-29 16:13:48 +05:30
parent 51e74d037c
commit 2bdcdf5dcf
No known key found for this signature in database
GPG key ID: 75507BE256F40CED

View file

@ -10,6 +10,7 @@ from .html_utils import sanitize_html
import frappe
from frappe.utils.identicon import Identicon
from email.utils import parseaddr, formataddr
from email.header import decode_header, make_header
# utility functions like cint, int, flt, etc.
from frappe.utils.data import *
from six.moves.urllib.parse import quote
@ -65,7 +66,7 @@ def get_formatted_email(user):
"""get Email Address of user formatted as: `John Doe <johndoe@example.com>`"""
fullname = get_fullname(user)
mail = get_email_address(user)
return formataddr((fullname, mail))
return cstr(make_header(decode_header(formataddr((fullname, mail)))))
def extract_email_id(email):
"""fetch only the email part of the Email Address"""