social/mail_tracking/models/ir_mail_server.py

124 lines
4.1 KiB
Python
Raw Normal View History

2018-03-19 16:41:26 +01:00
# Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
# 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
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 {}
2021-06-25 10:04:21 +02:00
headers["X-Odoo-Database"] = getattr(threading.currentThread(), "dbname", None)
2019-11-18 11:46:59 +01:00
headers["X-Odoo-Tracking-ID"] = tracking_email_id
return headers
def _tracking_email_id_body_get(self, body):
2019-11-18 11:46:59 +01:00
body = body or ""
# 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
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",
):
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,
body_alternative=body_alternative,
2019-11-18 11:46:59 +01:00
subtype_alternative=subtype_alternative,
)
return msg
def _tracking_email_get(self, message):
tracking_email_id = False
2019-11-18 11:46:59 +01:00
if message.get("X-Odoo-Tracking-ID", "").isdigit():
tracking_email_id = int(message["X-Odoo-Tracking-ID"])
return self.env["mail.tracking.email"].browse(tracking_email_id)
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)
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")
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,
):
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)
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,
)
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
)
if vals:
2019-11-18 11:46:59 +01:00
self.env["mail.tracking.event"].sudo().create(vals)
return message_id