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.

49 lines
2.2 KiB

  1. class MailTemplate(models.Model):
  2. "Templates for sending email"
  3. _name = "mail.template"
  4. @api.multi
  5. def send_mail_with_multiple_attachments(self, res_id, additional_attachments, force_send=False, raise_exception=False):
  6. """Generates a new mail message for the given template and record,
  7. and schedules it for delivery through the ``mail`` module's scheduler.
  8. :param int res_id: id of the record to render the template with
  9. (model is taken from the template)
  10. :param bool force_send: if True, the generated mail.message is
  11. immediately sent after being created, as if the scheduler
  12. was executed for this message only.
  13. :returns: id of the mail.message that was created
  14. """
  15. self.ensure_one()
  16. Mail = self.env['mail.mail']
  17. Attachment = self.env['ir.attachment'] # TDE FIXME: should remove dfeault_type from context
  18. # create a mail_mail based on values, without attachments
  19. values = self.generate_email(res_id)
  20. values['recipient_ids'] = [(4, pid) for pid in values.get('partner_ids', list())]
  21. attachment_ids = values.pop('attachment_ids', [])
  22. attachments = values.pop('attachments', [])
  23. # add a protection against void email_from
  24. if 'email_from' in values and not values.get('email_from'):
  25. values.pop('email_from')
  26. mail = Mail.create(values)
  27. # manage attachments
  28. attachments.extend(additional_attachments)
  29. for attachment in attachments:
  30. attachment_data = {
  31. 'name': attachment[0],
  32. 'datas_fname': attachment[0],
  33. 'datas': attachment[1],
  34. 'res_model': 'mail.message',
  35. 'res_id': mail.mail_message_id.id,
  36. }
  37. attachment_ids.append(Attachment.create(attachment_data).id)
  38. if attachment_ids:
  39. values['attachment_ids'] = [(6, 0, attachment_ids)]
  40. mail.write({'attachment_ids': [(6, 0, attachment_ids)]})
  41. if force_send:
  42. mail.send(raise_exception=raise_exception)
  43. return mail.id # TDE CLEANME: return mail + api.returns ?