2018-03-19 16:41:26 +01:00
|
|
|
# Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
|
2016-06-14 17:22:17 +02:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
|
|
|
import re
|
|
|
|
import threading
|
2019-11-18 11:46:59 +01:00
|
|
|
|
|
|
|
from odoo import api, models, tools
|
2016-06-14 17:22:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class IrMailServer(models.Model):
|
|
|
|
_inherit = "ir.mail_server"
|
|
|
|
|
|
|
|
def _tracking_headers_add(self, tracking_email_id, headers):
|
|
|
|
"""Allow other addons to add its own tracking SMTP headers"""
|
|
|
|
headers = headers or {}
|
2022-08-23 18:05:03 +02:00
|
|
|
headers["X-Odoo-Database"] = getattr(threading.current_thread(), "dbname", None)
|
2021-07-09 11:41:39 +02:00
|
|
|
headers["X-Odoo-MailTracking-ID"] = tracking_email_id
|
2016-06-14 17:22:17 +02:00
|
|
|
return headers
|
|
|
|
|
|
|
|
def _tracking_email_id_body_get(self, body):
|
2019-11-18 11:46:59 +01:00
|
|
|
body = body or ""
|
2016-06-14 17:22:17 +02:00
|
|
|
# https://regex101.com/r/lW4cB1/2
|
2019-11-18 11:46:59 +01:00
|
|
|
match = re.search(r'<img[^>]*data-odoo-tracking-email=["\']([0-9]*)["\']', body)
|
2021-06-25 10:04:21 +02:00
|
|
|
return str(match.group(1)) if match and match.group(1) else False
|
2016-06-14 17:22:17 +02:00
|
|
|
|
2019-11-18 11:46:59 +01:00
|
|
|
def build_email(
|
|
|
|
self,
|
|
|
|
email_from,
|
|
|
|
email_to,
|
|
|
|
subject,
|
|
|
|
body,
|
|
|
|
email_cc=None,
|
|
|
|
email_bcc=None,
|
|
|
|
reply_to=False,
|
|
|
|
attachments=None,
|
|
|
|
message_id=None,
|
|
|
|
references=None,
|
|
|
|
object_id=False,
|
|
|
|
subtype="plain",
|
|
|
|
headers=None,
|
|
|
|
body_alternative=None,
|
|
|
|
subtype_alternative="plain",
|
|
|
|
):
|
2016-06-14 17:22:17 +02:00
|
|
|
tracking_email_id = self._tracking_email_id_body_get(body)
|
|
|
|
if tracking_email_id:
|
|
|
|
headers = self._tracking_headers_add(tracking_email_id, headers)
|
|
|
|
msg = super(IrMailServer, self).build_email(
|
2019-11-18 11:46:59 +01:00
|
|
|
email_from,
|
|
|
|
email_to,
|
|
|
|
subject,
|
|
|
|
body,
|
|
|
|
email_cc=email_cc,
|
|
|
|
email_bcc=email_bcc,
|
|
|
|
reply_to=reply_to,
|
|
|
|
attachments=attachments,
|
|
|
|
message_id=message_id,
|
|
|
|
references=references,
|
|
|
|
object_id=object_id,
|
|
|
|
subtype=subtype,
|
|
|
|
headers=headers,
|
2016-06-14 17:22:17 +02:00
|
|
|
body_alternative=body_alternative,
|
2019-11-18 11:46:59 +01:00
|
|
|
subtype_alternative=subtype_alternative,
|
|
|
|
)
|
2016-06-14 17:22:17 +02:00
|
|
|
return msg
|
|
|
|
|
|
|
|
def _tracking_email_get(self, message):
|
2021-07-09 11:41:39 +02:00
|
|
|
try:
|
|
|
|
tracking_email_id = int(
|
|
|
|
message.get(
|
|
|
|
"X-Odoo-MailTracking-ID",
|
|
|
|
# Deprecated tracking header, kept as fallback
|
|
|
|
message["X-Odoo-Tracking-ID"],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except (TypeError, ValueError, KeyError):
|
|
|
|
tracking_email_id = False
|
2019-11-18 11:46:59 +01:00
|
|
|
return self.env["mail.tracking.email"].browse(tracking_email_id)
|
2016-06-14 17:22:17 +02:00
|
|
|
|
|
|
|
def _smtp_server_get(self, mail_server_id, smtp_server):
|
|
|
|
smtp_server_used = False
|
|
|
|
mail_server = None
|
|
|
|
if mail_server_id:
|
|
|
|
mail_server = self.browse(mail_server_id)
|
|
|
|
elif not smtp_server:
|
2019-11-18 11:46:59 +01:00
|
|
|
mail_server_ids = self.search([], order="sequence", limit=1)
|
2016-06-14 17:22:17 +02:00
|
|
|
mail_server = mail_server_ids[0] if mail_server_ids else None
|
|
|
|
if mail_server:
|
|
|
|
smtp_server_used = mail_server.smtp_host
|
2018-03-19 16:41:26 +01:00
|
|
|
else:
|
2019-11-18 11:46:59 +01:00
|
|
|
smtp_server_used = smtp_server or tools.config.get("smtp_server")
|
2016-06-14 17:22:17 +02:00
|
|
|
return smtp_server_used
|
|
|
|
|
|
|
|
@api.model
|
2019-11-18 11:46:59 +01:00
|
|
|
def send_email(
|
|
|
|
self,
|
|
|
|
message,
|
|
|
|
mail_server_id=None,
|
|
|
|
smtp_server=None,
|
|
|
|
smtp_port=None,
|
|
|
|
smtp_user=None,
|
|
|
|
smtp_password=None,
|
|
|
|
smtp_encryption=None,
|
|
|
|
smtp_debug=False,
|
|
|
|
smtp_session=None,
|
|
|
|
):
|
2016-06-14 17:22:17 +02:00
|
|
|
message_id = False
|
|
|
|
tracking_email = self._tracking_email_get(message)
|
2019-11-18 11:46:59 +01:00
|
|
|
smtp_server_used = self.sudo()._smtp_server_get(mail_server_id, smtp_server)
|
2016-06-14 17:22:17 +02:00
|
|
|
try:
|
|
|
|
message_id = super(IrMailServer, self).send_email(
|
2019-11-18 11:46:59 +01:00
|
|
|
message,
|
|
|
|
mail_server_id=mail_server_id,
|
|
|
|
smtp_server=smtp_server,
|
|
|
|
smtp_port=smtp_port,
|
|
|
|
smtp_user=smtp_user,
|
|
|
|
smtp_password=smtp_password,
|
|
|
|
smtp_encryption=smtp_encryption,
|
|
|
|
smtp_debug=smtp_debug,
|
|
|
|
smtp_session=smtp_session,
|
|
|
|
)
|
2016-06-14 17:22:17 +02:00
|
|
|
except Exception as e:
|
|
|
|
if tracking_email:
|
|
|
|
tracking_email.smtp_error(self, smtp_server_used, e)
|
|
|
|
if message_id and tracking_email:
|
|
|
|
vals = tracking_email._tracking_sent_prepare(
|
2019-11-18 11:46:59 +01:00
|
|
|
self, smtp_server_used, message, message_id
|
|
|
|
)
|
2016-06-14 17:22:17 +02:00
|
|
|
if vals:
|
2019-11-18 11:46:59 +01:00
|
|
|
self.env["mail.tracking.event"].sudo().create(vals)
|
2016-06-14 17:22:17 +02:00
|
|
|
return message_id
|