summaryrefslogtreecommitdiff
path: root/odoo/tools/mail.py
diff options
context:
space:
mode:
Diffstat (limited to 'odoo/tools/mail.py')
-rw-r--r--odoo/tools/mail.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/odoo/tools/mail.py b/odoo/tools/mail.py
index 0bfc51cc2ad..a52e555c52a 100644
--- a/odoo/tools/mail.py
+++ b/odoo/tools/mail.py
@@ -554,6 +554,19 @@ def email_escape_char(email_address):
""" Escape problematic characters in the given email address string"""
return email_address.replace('\\', '\\\\').replace('%', '\\%').replace('_', '\\_')
+
+def email_domain_extract(email):
+ """Return the domain of the given email."""
+ if not email:
+ return
+
+ email_split = getaddresses([email])
+ if not email_split or not email_split[0]:
+ return
+
+ _, _, domain = email_split[0][1].rpartition('@')
+ return domain
+
# was mail_thread.decode_header()
def decode_message_header(message, header, separator=' '):
return separator.join(h for h in message.get_all(header, []) if h)
@@ -600,3 +613,30 @@ def formataddr(pair, charset='utf-8'):
name = email_addr_escapes_re.sub(r'\\\g<0>', name)
return f'"{name}" <{local}@{domain}>'
return f"{local}@{domain}"
+
+
+def encapsulate_email(old_email, new_email):
+ """Change the FROM of the message and use the old one as name.
+
+ e.g.
+ * Old From: "Admin" <admin@gmail.com>
+ * New From: notifications@odoo.com
+ * Output: "Admin (admin@gmail.com)" <notifications@odoo.com>
+ """
+ old_email_split = getaddresses([old_email])
+ if not old_email_split or not old_email_split[0]:
+ return old_email
+
+ new_email_split = getaddresses([new_email])
+ if not new_email_split or not new_email_split[0]:
+ return
+
+ if old_email_split[0][0]:
+ name_part = '%s (%s)' % old_email_split[0]
+ else:
+ name_part = old_email_split[0][1]
+
+ return formataddr((
+ name_part,
+ new_email_split[0][1],
+ ))