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.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Lorenzo Battistini - Agile Business Group
  3. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
  4. import logging
  5. from odoo import models, api, tools, _
  6. from odoo.tools import ustr
  7. _logger = logging.getLogger(__name__)
  8. class MailThread(models.AbstractModel):
  9. _inherit = 'mail.thread'
  10. @api.model
  11. def notify_bounce_partners(self, message, fetchmail, message_dict):
  12. message_id = message.get('Message-Id')
  13. email_from = tools.decode_message_header(message, 'From')
  14. email_from_localpart = (
  15. (
  16. tools.email_split(email_from) or ['']
  17. )[0].split('@', 1)[0].lower()
  18. )
  19. # same criteria used by odoo
  20. # see addons/mail/models/mail_thread.py
  21. if (
  22. message.get_content_type() == 'multipart/report' or
  23. email_from_localpart == 'mailer-daemon'
  24. ):
  25. references = tools.decode_message_header(message, 'References')
  26. in_reply_to = tools.decode_message_header(
  27. message, 'In-Reply-To').strip()
  28. thread_references = references or in_reply_to
  29. msg_references = [
  30. ref for ref in
  31. tools.mail_header_msgid_re.findall(thread_references) if
  32. 'reply_to' not in ref
  33. ]
  34. MailMessage = self.env['mail.message']
  35. mail_messages = MailMessage.sudo().search([
  36. ('message_id', 'in', msg_references)], limit=1)
  37. recipients = mail_messages.mapped('author_id')
  38. recipients |= fetchmail.bounce_notify_partner_ids
  39. if not recipients:
  40. _logger.info(
  41. 'Not notifying bounce email from %s with Message-Id %s: '
  42. 'no recipients found',
  43. email_from, message_id
  44. )
  45. return
  46. _logger.info(
  47. 'Notifying bounce email from %s with Message-Id %s',
  48. email_from, message_id
  49. )
  50. email = self.env['mail.mail'].create({
  51. 'body_html': (
  52. u"%s<br/><br/><br/>%s<br/><br/>%s"
  53. % (
  54. ustr(message_dict['body']),
  55. _("Raw message:"),
  56. ustr(message.__str__()).replace(
  57. "\n", "<br/>")
  58. )),
  59. 'subject': message_dict['subject'],
  60. 'recipient_ids': [
  61. (6, 0, [p.id for p in recipients])
  62. ]
  63. })
  64. email.send()
  65. @api.model
  66. def message_route(
  67. self, message, message_dict, model=None, thread_id=None,
  68. custom_values=None
  69. ):
  70. if self.env.context.get('fetchmail_server_id'):
  71. fetchmail = self.env['fetchmail.server'].browse(
  72. self.env.context['fetchmail_server_id'])
  73. self.notify_bounce_partners(message, fetchmail, message_dict)
  74. return super(MailThread, self).message_route(
  75. message=message, message_dict=message_dict, model=model,
  76. thread_id=thread_id, custom_values=custom_values
  77. )