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.

219 lines
9.8 KiB

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