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.

91 lines
3.8 KiB

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