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.

114 lines
5.6 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. if re.search(correct_email_from, email_from) is None:
  60. email_from = default_email_from
  61. msg = ir_mail_server.build_email(
  62. email_from=email_from,
  63. email_to=email.get('email_to'),
  64. subject=email.get('subject'),
  65. body=email.get('body'),
  66. body_alternative=email.get('body_alternative'),
  67. email_cc=tools.email_split(mail.email_cc),
  68. reply_to=mail.reply_to,
  69. attachments=attachments,
  70. message_id=mail.message_id,
  71. references=mail.references,
  72. object_id=mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
  73. subtype='html',
  74. subtype_alternative='plain',
  75. headers=headers)
  76. res = ir_mail_server.send_email(cr, uid, msg,
  77. mail_server_id=mail.mail_server_id.id,
  78. context=context)
  79. if res:
  80. mail.write({'state': 'sent', 'message_id': res})
  81. mail_sent = True
  82. else:
  83. mail.write({'state': 'exception'})
  84. mail_sent = False
  85. # /!\ can't use mail.state here, as mail.refresh() will cause an error
  86. # see revid:odo@openerp.com-20120622152536-42b2s28lvdv3odyr in 6.1
  87. if mail_sent:
  88. self._postprocess_sent_message(cr, uid, mail, context=context)
  89. except Exception as e:
  90. _logger.exception('failed sending mail.mail %s', mail.id)
  91. mail.write({'state': 'exception'})
  92. if raise_exception:
  93. if isinstance(e, AssertionError):
  94. # get the args of the original error, wrap into a value and throw a MailDeliveryException
  95. # that is an except_orm, with name and value as arguments
  96. value = '. '.join(e.args)
  97. raise MailDeliveryException(_("Mail Delivery Failed"), value)
  98. raise
  99. if auto_commit == True:
  100. cr.commit()
  101. return True