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.

117 lines
5.8 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. # -*- coding: utf-8 -*-
  2. import base64
  3. import logging
  4. import re
  5. from urllib import urlencode
  6. from urlparse import urljoin
  7. from openerp import tools
  8. from openerp import SUPERUSER_ID
  9. from openerp.addons.base.ir.ir_mail_server import MailDeliveryException
  10. from openerp.osv import fields, osv
  11. from openerp.tools.translate import _
  12. _logger = logging.getLogger(__name__)
  13. class mail_mail(osv.Model):
  14. _inherit = "mail.mail"
  15. def send(self, cr, uid, ids, auto_commit=False, raise_exception=False, context=None):
  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. catchall_alias = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.catchall.alias", context=context)
  30. catchall_domain = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.catchall.domain", context=context)
  31. correct_email_from = '@%s>?\s*$'%catchall_domain
  32. default_email_from = '%s@%s' % (catchall_alias, catchall_domain)
  33. ir_mail_server = self.pool.get('ir.mail_server')
  34. for mail in self.browse(cr, SUPERUSER_ID, ids, context=context):
  35. try:
  36. # handle attachments
  37. attachments = []
  38. for attach in mail.attachment_ids:
  39. attachments.append((attach.datas_fname, base64.b64decode(attach.datas)))
  40. # specific behavior to customize the send email for notified partners
  41. email_list = []
  42. if mail.email_to:
  43. email_list.append(self.send_get_email_dict(cr, uid, mail, context=context))
  44. for partner in mail.recipient_ids:
  45. email_list.append(self.send_get_email_dict(cr, uid, mail, partner=partner, context=context))
  46. # headers
  47. headers = {}
  48. bounce_alias = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.bounce.alias", context=context)
  49. catchall_domain = self.pool['ir.config_parameter'].get_param(cr, uid, "mail.catchall.domain", context=context)
  50. if bounce_alias and catchall_domain:
  51. if mail.model and mail.res_id:
  52. headers['Return-Path'] = '%s-%d-%s-%d@%s' % (bounce_alias, mail.id, mail.model, mail.res_id, catchall_domain)
  53. else:
  54. headers['Return-Path'] = '%s-%d@%s' % (bounce_alias, mail.id, catchall_domain)
  55. # build an RFC2822 email.message.Message object and send it without queuing
  56. res = None
  57. for email in email_list:
  58. email_from = mail.email_from
  59. reply_to = mail.reply_to
  60. if re.search(correct_email_from, email_from) is None:
  61. email_from = default_email_from
  62. else:
  63. reply_to = email_from
  64. msg = ir_mail_server.build_email(
  65. email_from=email_from,
  66. email_to=email.get('email_to'),
  67. subject=email.get('subject'),
  68. body=email.get('body'),
  69. body_alternative=email.get('body_alternative'),
  70. email_cc=tools.email_split(mail.email_cc),
  71. reply_to=reply_to,
  72. attachments=attachments,
  73. message_id=mail.message_id,
  74. references=mail.references,
  75. object_id=mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
  76. subtype='html',
  77. subtype_alternative='plain',
  78. headers=headers)
  79. res = ir_mail_server.send_email(cr, uid, msg,
  80. mail_server_id=mail.mail_server_id.id,
  81. context=context)
  82. if res:
  83. mail.write({'state': 'sent', 'message_id': res})
  84. mail_sent = True
  85. else:
  86. mail.write({'state': 'exception'})
  87. mail_sent = False
  88. # /!\ can't use mail.state here, as mail.refresh() will cause an error
  89. # see revid:odo@openerp.com-20120622152536-42b2s28lvdv3odyr in 6.1
  90. if mail_sent:
  91. self._postprocess_sent_message(cr, uid, mail, context=context)
  92. except Exception as e:
  93. _logger.exception('failed sending mail.mail %s', mail.id)
  94. mail.write({'state': 'exception'})
  95. if raise_exception:
  96. if isinstance(e, AssertionError):
  97. # get the args of the original error, wrap into a value and throw a MailDeliveryException
  98. # that is an except_orm, with name and value as arguments
  99. value = '. '.join(e.args)
  100. raise MailDeliveryException(_("Mail Delivery Failed"), value)
  101. raise
  102. if auto_commit == True:
  103. cr.commit()
  104. return True