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.

164 lines
8.7 KiB

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