summaryrefslogtreecommitdiff
path: root/odoo/tools
diff options
context:
space:
mode:
authorstd-odoo <std@odoo.com>2021-04-14 07:23:06 +0000
committerstd-odoo <std@odoo.com>2021-05-17 11:14:53 +0000
commit13ff118da832a339d9cb43d74bf0b5dd0cf3c036 (patch)
tree4221511c86ac6f84010f7dc921fa25a924f74331 /odoo/tools
parent44e185dbd6ddeda7cdcd76882fa0bd231e9d3f37 (diff)
[IMP] mail: allow to force the FROM headers of the outgoing mail server
Purpose ======= We want to be able to force the FROM headers when we sent email in SMTP. So we can avoid the emails to be considered as spam. Specifications ============== This is done with 2 system parameters. If the system parameter `mail.force.smtp.from` is set we encapsulate all outgoing email from with the given value. If the previous system parameter is not set and if both `mail.dynamic.smtp.from` and `mail.catchall.domain` are set, we encapsulate the FROM only if the domain of the email is not the same as the domain of the catchall parameter. Otherwise we do not encapsulate the email (same behavior as before this commit). Task 2367946 See odoo/odoo/pull/61853 closes odoo/odoo#70854 X-original-commit: 044532aed6f796cd93e37556b4a8ce47d2c23a7c Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com> Signed-off-by: Olivier Dony (odo) <odo@openerp.com>
Diffstat (limited to 'odoo/tools')
-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],
+ ))