- def render_message(self, cr, uid, wizard, res_id, context=None):
- """ Generate an email from the template for given (wizard.model, res_id)
- pair. This method is meant to be inherited by email_template that
- will produce a more complete dictionary. """
- return {
- 'subject': self.render_template(cr, uid, wizard.subject, wizard.model, res_id, context),
- 'body': self.render_template(cr, uid, wizard.body, wizard.model, res_id, context),
- 'email_from': self.render_template(cr, uid, wizard.email_from, wizard.model, res_id, context),
- 'reply_to': self.render_template(cr, uid, wizard.reply_to, wizard.model, res_id, context),
- }
+ def get_mail_values(self, cr, uid, wizard, res_ids, context=None):
+ """Generate the values that will be used by send_mail to create mail_messages
+ or mail_mails. """
+ results = dict.fromkeys(res_ids, False)
+ rendered_values, default_recipients = {}, {}
+ mass_mail_mode = wizard.composition_mode == 'mass_mail'
+
+ # render all template-based value at once
+ if mass_mail_mode and wizard.model:
+ rendered_values = self.render_message_batch(cr, uid, wizard, res_ids, context=context)
+ # compute alias-based reply-to in batch
+ reply_to_value = dict.fromkeys(res_ids, None)
+ if mass_mail_mode and not wizard.no_auto_thread:
+ reply_to_value = self.pool['mail.thread'].message_get_reply_to(cr, uid, res_ids, default=wizard.email_from, context=dict(context, thread_model=wizard.model))
+
+ for res_id in res_ids:
+ # static wizard (mail.message) values
+ mail_values = {
+ 'subject': wizard.subject,
+ 'body': wizard.body,
+ 'parent_id': wizard.parent_id and wizard.parent_id.id,
+ 'partner_ids': [partner.id for partner in wizard.partner_ids],
+ 'attachment_ids': [attach.id for attach in wizard.attachment_ids],
+ 'author_id': wizard.author_id.id,
+ 'email_from': wizard.email_from,
+ 'record_name': wizard.record_name,
+ 'no_auto_thread': wizard.no_auto_thread,
+ }
+ # mass mailing: rendering override wizard static values
+ if mass_mail_mode and wizard.model:
+ # always keep a copy, reset record name (avoid browsing records)
+ mail_values.update(notification=True, model=wizard.model, res_id=res_id, record_name=False)
+ # auto deletion of mail_mail
+ if 'mail_auto_delete' in context:
+ mail_values['auto_delete'] = context.get('mail_auto_delete')
+ # rendered values using template
+ email_dict = rendered_values[res_id]
+ mail_values['partner_ids'] += email_dict.pop('partner_ids', [])
+ mail_values.update(email_dict)
+ if not wizard.no_auto_thread:
+ mail_values.pop('reply_to')
+ if reply_to_value.get(res_id):
+ mail_values['reply_to'] = reply_to_value[res_id]
+ if wizard.no_auto_thread and not mail_values.get('reply_to'):
+ mail_values['reply_to'] = mail_values['email_from']
+ # mail_mail values: body -> body_html, partner_ids -> recipient_ids
+ mail_values['body_html'] = mail_values.get('body', '')
+ mail_values['recipient_ids'] = [(4, id) for id in mail_values.pop('partner_ids', [])]
+
+ # process attachments: should not be encoded before being processed by message_post / mail_mail create
+ mail_values['attachments'] = [(name, base64.b64decode(enc_cont)) for name, enc_cont in email_dict.pop('attachments', list())]
+ attachment_ids = []
+ for attach_id in mail_values.pop('attachment_ids'):
+ new_attach_id = self.pool.get('ir.attachment').copy(cr, uid, attach_id, {'res_model': self._name, 'res_id': wizard.id}, context=context)
+ attachment_ids.append(new_attach_id)
+ mail_values['attachment_ids'] = self.pool['mail.thread']._message_preprocess_attachments(
+ cr, uid, mail_values.pop('attachments', []),
+ attachment_ids, 'mail.message', 0, context=context)
+
+ results[res_id] = mail_values
+ return results