mail_thread.py 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import datetime
  4. from odoo import api, models, fields, tools
  5. BLACKLIST_MAX_BOUNCED_LIMIT = 5
  6. class MailThread(models.AbstractModel):
  7. """ Update MailThread to add the support of bounce management in mass mailing traces. """
  8. _inherit = 'mail.thread'
  9. @api.model
  10. def _message_route_process(self, message, message_dict, routes):
  11. """ Override to update the parent mailing traces. The parent is found
  12. by using the References header of the incoming message and looking for
  13. matching message_id in mailing.trace. """
  14. if routes:
  15. # even if 'reply_to' in ref (cfr mail/mail_thread) that indicates a new thread redirection
  16. # (aka bypass alias configuration in gateway) consider it as a reply for statistics purpose
  17. thread_references = message_dict['references'] or message_dict['in_reply_to']
  18. msg_references = tools.mail_header_msgid_re.findall(thread_references)
  19. if msg_references:
  20. self.env['mailing.trace'].set_opened(domain=[('message_id', 'in', msg_references)])
  21. self.env['mailing.trace'].set_replied(domain=[('message_id', 'in', msg_references)])
  22. return super(MailThread, self)._message_route_process(message, message_dict, routes)
  23. def message_post_with_template(self, template_id, **kwargs):
  24. # avoid having message send through `message_post*` methods being implicitly considered as
  25. # mass-mailing
  26. no_massmail = self.with_context(
  27. default_mass_mailing_name=False,
  28. default_mass_mailing_id=False,
  29. )
  30. return super(MailThread, no_massmail).message_post_with_template(template_id, **kwargs)
  31. @api.model
  32. def _routing_handle_bounce(self, email_message, message_dict):
  33. """ In addition, an auto blacklist rule check if the email can be blacklisted
  34. to avoid sending mails indefinitely to this email address.
  35. This rule checks if the email bounced too much. If this is the case,
  36. the email address is added to the blacklist in order to avoid continuing
  37. to send mass_mail to that email address. If it bounced too much times
  38. in the last month and the bounced are at least separated by one week,
  39. to avoid blacklist someone because of a temporary mail server error,
  40. then the email is considered as invalid and is blacklisted."""
  41. super(MailThread, self)._routing_handle_bounce(email_message, message_dict)
  42. bounced_email = message_dict['bounced_email']
  43. bounced_msg_id = message_dict['bounced_msg_id']
  44. bounced_partner = message_dict['bounced_partner']
  45. if bounced_msg_id:
  46. self.env['mailing.trace'].set_bounced(domain=[('message_id', 'in', bounced_msg_id)])
  47. if bounced_email:
  48. three_months_ago = fields.Datetime.to_string(datetime.datetime.now() - datetime.timedelta(weeks=13))
  49. stats = self.env['mailing.trace'].search(['&', '&', ('trace_status', '=', 'bounce'), ('write_date', '>', three_months_ago), ('email', '=ilike', bounced_email)]).mapped('write_date')
  50. if len(stats) >= BLACKLIST_MAX_BOUNCED_LIMIT and (not bounced_partner or any(p.message_bounce >= BLACKLIST_MAX_BOUNCED_LIMIT for p in bounced_partner)):
  51. if max(stats) > min(stats) + datetime.timedelta(weeks=1):
  52. blacklist_rec = self.env['mail.blacklist'].sudo()._add(bounced_email)
  53. blacklist_rec._message_log(
  54. body='This email has been automatically blacklisted because of too much bounced.')
  55. @api.model
  56. def message_new(self, msg_dict, custom_values=None):
  57. """ Overrides mail_thread message_new that is called by the mailgateway
  58. through message_process.
  59. This override updates the document according to the email.
  60. """
  61. defaults = {}
  62. if issubclass(type(self), self.pool['utm.mixin']):
  63. thread_references = msg_dict.get('references', '') or msg_dict.get('in_reply_to', '')
  64. msg_references = tools.mail_header_msgid_re.findall(thread_references)
  65. if msg_references:
  66. traces = self.env['mailing.trace'].search([('message_id', 'in', msg_references)], limit=1)
  67. if traces:
  68. defaults['campaign_id'] = traces.campaign_id.id
  69. defaults['source_id'] = traces.mass_mailing_id.source_id.id
  70. defaults['medium_id'] = traces.mass_mailing_id.medium_id.id
  71. if custom_values:
  72. defaults.update(custom_values)
  73. return super(MailThread, self).message_new(msg_dict, custom_values=defaults)