[IMP] base: fallback on 'email_re' when getadresses fails

When 'getadresses' fails at parsing some input and give us a result like
'gmail.com' (see previous commit adding test cases) we fallback on using
'email_re' which is better at finding email addresses in a global string.
We use it only in this specific case as fallback mechanism to rely on
'getadresses' when possible.

Task-3572208

X-original-commit: odoo/odoo@8e61a3b690
Part-of: odoo/odoo#141856
This commit is contained in:
Thibault Delavallée
2023-11-17 16:11:51 +00:00
parent d5b39338ff
commit a03ae3765e
2 changed files with 60 additions and 20 deletions
+14 -2
View File
@@ -549,13 +549,25 @@ def email_split_tuples(text):
if not text:
return []
return list(map(_parse_based_on_spaces, [
# found valid pairs, filtering out failed parsing
valid_pairs = [
(addr[0], addr[1]) for addr in getaddresses([text])
# getaddresses() returns '' when email parsing fails, and
# sometimes returns emails without at least '@'. The '@'
# is strictly required in RFC2822's `addr-spec`.
if addr[1] and '@' in addr[1]
]))
]
# corner case: returning '@gmail.com'-like email (see test_email_split)
if any(pair[1].startswith('@') for pair in valid_pairs):
filtered = [
found_email for found_email in email_re.findall(text)
if found_email and not found_email.startswith('@')
]
if filtered:
valid_pairs = [('', found_email) for found_email in filtered]
return list(map(_parse_based_on_spaces, valid_pairs))
def email_split(text):
""" Return a list of the email addresses found in ``text`` """