You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.8 KiB

  1. # Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. import re
  4. import threading
  5. from odoo import models, api, tools
  6. class IrMailServer(models.Model):
  7. _inherit = "ir.mail_server"
  8. def _tracking_headers_add(self, tracking_email_id, headers):
  9. """Allow other addons to add its own tracking SMTP headers"""
  10. headers = headers or {}
  11. headers['X-Odoo-Database'] = getattr(
  12. threading.currentThread(), 'dbname', None),
  13. headers['X-Odoo-Tracking-ID'] = tracking_email_id
  14. return headers
  15. def _tracking_email_id_body_get(self, body):
  16. body = body or ''
  17. # https://regex101.com/r/lW4cB1/2
  18. match = re.search(
  19. r'<img[^>]*data-odoo-tracking-email=["\']([0-9]*)["\']', body)
  20. return int(match.group(1)) if match.group(1) else False
  21. def build_email(self, email_from, email_to, subject, body, email_cc=None,
  22. email_bcc=None, reply_to=False, attachments=None,
  23. message_id=None, references=None, object_id=False,
  24. subtype='plain', headers=None, body_alternative=None,
  25. subtype_alternative='plain'):
  26. tracking_email_id = self._tracking_email_id_body_get(body)
  27. if tracking_email_id:
  28. headers = self._tracking_headers_add(tracking_email_id, headers)
  29. msg = super(IrMailServer, self).build_email(
  30. email_from, email_to, subject, body, email_cc=email_cc,
  31. email_bcc=email_bcc, reply_to=reply_to, attachments=attachments,
  32. message_id=message_id, references=references, object_id=object_id,
  33. subtype=subtype, headers=headers,
  34. body_alternative=body_alternative,
  35. subtype_alternative=subtype_alternative)
  36. return msg
  37. def _tracking_email_get(self, message):
  38. tracking_email_id = False
  39. if message.get('X-Odoo-Tracking-ID', '').isdigit():
  40. tracking_email_id = int(message['X-Odoo-Tracking-ID'])
  41. return self.env['mail.tracking.email'].browse(tracking_email_id)
  42. def _smtp_server_get(self, mail_server_id, smtp_server):
  43. smtp_server_used = False
  44. mail_server = None
  45. if mail_server_id:
  46. mail_server = self.browse(mail_server_id)
  47. elif not smtp_server:
  48. mail_server_ids = self.search([], order='sequence', limit=1)
  49. mail_server = mail_server_ids[0] if mail_server_ids else None
  50. if mail_server:
  51. smtp_server_used = mail_server.smtp_host
  52. else:
  53. smtp_server_used = smtp_server or tools.config.get('smtp_server')
  54. return smtp_server_used
  55. @api.model
  56. def send_email(self, message, mail_server_id=None, smtp_server=None,
  57. smtp_port=None, smtp_user=None, smtp_password=None,
  58. smtp_encryption=None, smtp_debug=False, smtp_session=None):
  59. message_id = False
  60. tracking_email = self._tracking_email_get(message)
  61. smtp_server_used = self.sudo()._smtp_server_get(
  62. mail_server_id, smtp_server,
  63. )
  64. try:
  65. message_id = super(IrMailServer, self).send_email(
  66. message, mail_server_id=mail_server_id,
  67. smtp_server=smtp_server, smtp_port=smtp_port,
  68. smtp_user=smtp_user, smtp_password=smtp_password,
  69. smtp_encryption=smtp_encryption, smtp_debug=smtp_debug,
  70. smtp_session=smtp_session)
  71. except Exception as e:
  72. if tracking_email:
  73. tracking_email.smtp_error(self, smtp_server_used, e)
  74. if message_id and tracking_email:
  75. vals = tracking_email._tracking_sent_prepare(
  76. self, smtp_server_used, message, message_id)
  77. if vals:
  78. self.env['mail.tracking.event'].sudo().create(vals)
  79. return message_id