test_mailing.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- coding: utf-8 -*-
  2. from openerp import tools
  3. from openerp.osv import osv, fields
  4. class TestMassMailing(osv.TransientModel):
  5. _name = 'mail.mass_mailing.test'
  6. _description = 'Sample Mail Wizard'
  7. _columns = {
  8. 'email_to': fields.char('Recipients', required=True,
  9. help='Comma-separated list of email addresses.'),
  10. 'mass_mailing_id': fields.many2one(
  11. 'mail.mass_mailing', 'Mailing', required=True, ondelete='cascade'),
  12. }
  13. _defaults = {
  14. 'email_to': lambda self, cr, uid, ctx=None: self.pool['mail.message']._get_default_from(cr, uid, context=ctx),
  15. }
  16. def send_mail_test(self, cr, uid, ids, context=None):
  17. Mail = self.pool['mail.mail']
  18. for wizard in self.browse(cr, uid, ids, context=context):
  19. mailing = wizard.mass_mailing_id
  20. test_emails = tools.email_split(wizard.email_to)
  21. mail_ids = []
  22. for test_mail in test_emails:
  23. mail_values = {
  24. 'email_from': mailing.email_from,
  25. 'reply_to': mailing.reply_to,
  26. 'email_to': test_mail,
  27. 'subject': mailing.name,
  28. 'body_html': '',
  29. 'notification': True,
  30. 'mailing_id': mailing.id,
  31. 'attachment_ids': [(4, attachment.id) for attachment in mailing.attachment_ids],
  32. }
  33. mail_mail_obj = Mail.browse(cr, uid, Mail.create(cr, uid, mail_values, context=context), context=context)
  34. unsubscribe_url = Mail._get_unsubscribe_url(cr, uid, mail_mail_obj, test_mail, context=context)
  35. body = tools.append_content_to_html(mailing.body_html, unsubscribe_url, plaintext=False, container_tag='p')
  36. Mail.write(cr, uid, mail_mail_obj.id, {'body_html': mailing.body_html}, context=context)
  37. mail_ids.append(mail_mail_obj.id)
  38. Mail.send(cr, uid, mail_ids, context=context)
  39. self.pool['mail.mass_mailing'].write(cr, uid, [mailing.id], {'state': 'test'}, context=context)
  40. return True