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.

159 lines
8.1 KiB

  1. # -*- coding: utf-8 -*-
  2. ################################################################
  3. # License, author and contributors information in: #
  4. # __openerp__.py file at the root folder of this module. #
  5. ################################################################
  6. import base64
  7. import logging
  8. import psycopg2
  9. from openerp import _, api, fields, models
  10. from openerp import tools
  11. from openerp.addons.base.ir.ir_mail_server import MailDeliveryException
  12. from openerp.tools.safe_eval import safe_eval as eval
  13. _logger = logging.getLogger(__name__)
  14. class MailMail(models.Model):
  15. _inherit = 'mail.mail'
  16. email_bcc = fields.Char(string='BCC',
  17. help='Blind carbon copy message recipients')
  18. @api.multi
  19. def send(self, auto_commit=False, raise_exception=False):
  20. """ Sends the selected emails immediately, ignoring their current
  21. state (mails that have already been sent should not be passed
  22. unless they should actually be re-sent).
  23. Emails successfully delivered are marked as 'sent', and those
  24. that fail to be deliver are marked as 'exception', and the
  25. corresponding error mail is output in the server logs.
  26. :param bool auto_commit: whether to force a commit of the mail status
  27. after sending each mail (meant only for scheduler processing);
  28. should never be True during normal transactions (default: False)
  29. :param bool raise_exception: whether to raise an exception if the
  30. email sending process has failed
  31. :return: True
  32. """
  33. IrMailServer = self.env['ir.mail_server']
  34. for mail in self:
  35. try:
  36. # TDE note: remove me when model_id field is present on mail.message - done here to avoid doing it multiple times in the sub method
  37. if mail.model:
  38. model = self.env['ir.model'].sudo().search([('model', '=', mail.model)])[0]
  39. else:
  40. model = None
  41. if model:
  42. mail = mail.with_context(model_name=model.name)
  43. # load attachment binary data with a separate read(), as prefetching all
  44. # `datas` (binary field) could bloat the browse cache, triggerring
  45. # soft/hard mem limits with temporary data.
  46. attachments = [(a['datas_fname'], base64.b64decode(a['datas']))
  47. for a in mail.attachment_ids.sudo().read(['datas_fname', 'datas'])]
  48. # specific behavior to customize the send email for notified partners
  49. email_list = []
  50. if mail.email_to:
  51. email_list.append(mail.send_get_email_dict())
  52. for partner in mail.recipient_ids:
  53. email_list.append(mail.send_get_email_dict(partner=partner))
  54. # headers
  55. headers = {}
  56. bounce_alias = self.env['ir.config_parameter'].get_param("mail.bounce.alias")
  57. catchall_domain = self.env['ir.config_parameter'].get_param("mail.catchall.domain")
  58. if bounce_alias and catchall_domain:
  59. if mail.model and mail.res_id:
  60. headers['Return-Path'] = '%s-%d-%s-%d@%s' % (bounce_alias, mail.id, mail.model, mail.res_id, catchall_domain)
  61. else:
  62. headers['Return-Path'] = '%s-%d@%s' % (bounce_alias, mail.id, catchall_domain)
  63. if mail.headers:
  64. try:
  65. headers.update(eval(mail.headers))
  66. except Exception:
  67. pass
  68. # Writing on the mail object may fail (e.g. lock on user) which
  69. # would trigger a rollback *after* actually sending the email.
  70. # To avoid sending twice the same email, provoke the failure earlier
  71. mail.write({
  72. 'state': 'exception',
  73. 'failure_reason': _('Error without exception. Probably due do sending an email without computed recipients.'),
  74. })
  75. mail_sent = False
  76. # build an RFC2822 email.message.Message object and send it without queuing
  77. res = None
  78. for email in email_list:
  79. msg = IrMailServer.build_email(
  80. email_from=mail.email_from,
  81. email_to=email.get('email_to'),
  82. subject=mail.subject,
  83. body=email.get('body'),
  84. body_alternative=email.get('body_alternative'),
  85. email_cc=tools.email_split(mail.email_cc),
  86. email_bcc=tools.email_split(mail.email_bcc),
  87. reply_to=mail.reply_to,
  88. attachments=attachments,
  89. message_id=mail.message_id,
  90. references=mail.references,
  91. object_id=mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
  92. subtype='html',
  93. subtype_alternative='plain',
  94. headers=headers)
  95. try:
  96. res = IrMailServer.send_email(msg, mail_server_id=mail.mail_server_id.id)
  97. except AssertionError as error:
  98. if error.message == IrMailServer.NO_VALID_RECIPIENT:
  99. # No valid recipient found for this particular
  100. # mail item -> ignore error to avoid blocking
  101. # delivery to next recipients, if any. If this is
  102. # the only recipient, the mail will show as failed.
  103. _logger.info("Ignoring invalid recipients for mail.mail %s: %s",
  104. mail.message_id, email.get('email_to'))
  105. else:
  106. raise
  107. if res:
  108. mail.write({'state': 'sent', 'message_id': res, 'failure_reason': False})
  109. mail_sent = True
  110. # /!\ can't use mail.state here, as mail.refresh() will cause an error
  111. # see revid:odo@openerp.com-20120622152536-42b2s28lvdv3odyr in 6.1
  112. if mail_sent:
  113. _logger.info('Mail with ID %r and Message-Id %r successfully sent', mail.id, mail.message_id)
  114. mail._postprocess_sent_message_v9(mail_sent=mail_sent)
  115. except MemoryError:
  116. # prevent catching transient MemoryErrors, bubble up to notify user or abort cron job
  117. # instead of marking the mail as failed
  118. _logger.exception(
  119. 'MemoryError while processing mail with ID %r and Msg-Id %r. Consider raising the --limit-memory-hard startup option',
  120. mail.id, mail.message_id)
  121. raise
  122. except psycopg2.Error:
  123. # If an error with the database occurs, chances are that the cursor is unusable.
  124. # This will lead to an `psycopg2.InternalError` being raised when trying to write
  125. # `state`, shadowing the original exception and forbid a retry on concurrent
  126. # update. Let's bubble it.
  127. raise
  128. except Exception as e:
  129. failure_reason = tools.ustr(e)
  130. _logger.exception('failed sending mail (id: %s) due to %s', mail.id, failure_reason)
  131. mail.write({'state': 'exception', 'failure_reason': failure_reason})
  132. mail._postprocess_sent_message_v9(mail_sent=False)
  133. if raise_exception:
  134. if isinstance(e, AssertionError):
  135. # get the args of the original error, wrap into a value and throw a MailDeliveryException
  136. # that is an except_orm, with name and value as arguments
  137. value = '. '.join(e.args)
  138. raise MailDeliveryException(_("Mail Delivery Failed"), value)
  139. raise
  140. if auto_commit is True:
  141. self._cr.commit()
  142. return True