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.

48 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016-2017 Compassion CH (http://www.compassion.ch)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import models, api, tools
  5. class TestMassMailing(models.TransientModel):
  6. _inherit = 'mail.mass_mailing.test'
  7. @api.multi
  8. def send_mail_test(self):
  9. """ Send with Sendgrid if needed.
  10. """
  11. self.ensure_one()
  12. mailing = self.mass_mailing_id
  13. template = mailing.email_template_id.with_context(
  14. lang=mailing.lang.code or self.env.context['lang'])
  15. if template:
  16. # Send with SendGrid (and use E-mail Template)
  17. sendgrid_template = template.sendgrid_localized_template
  18. res_id = self.env.user.partner_id.id
  19. body = template.render_template(
  20. mailing.body_html, template.model, [res_id],
  21. post_process=True)[res_id]
  22. test_emails = tools.email_split(self.email_to)
  23. emails = self.env['mail.mail']
  24. for test_mail in test_emails:
  25. email_vals = {
  26. 'email_from': mailing.email_from,
  27. 'reply_to': mailing.reply_to,
  28. 'email_to': test_mail,
  29. 'subject': mailing.name,
  30. 'body_html': body,
  31. 'sendgrid_template_id': sendgrid_template.id,
  32. 'substitution_ids': template.render_substitutions(
  33. res_id)[res_id],
  34. 'notification': True,
  35. 'mailing_id': mailing.id,
  36. 'attachment_ids': [(4, attachment.id) for attachment in
  37. mailing.attachment_ids],
  38. }
  39. emails += emails.create(email_vals)
  40. emails.send_sendgrid()
  41. else:
  42. super(TestMassMailing, self).send_mail_test()
  43. return True