'Generates the recipients of the template. Default values can ben generated
instead of the template values if requested by template or context.
Emails (email_to, email_cc) can be transformed into partners if requested
in the context.'

@api.multi
def generate_recipients(self, results, res_ids):

    self.ensure_one()
    if (self.use_default_to or self._context.get('tpl_force_default_to')):
        default_recipients = self.env['mail.thread'].message_get_default_recipients(res_model=self.model, res_ids=res_ids)
        for (res_id, recipients) in default_recipients.iteritems():
            results[res_id].pop('partner_to', None)
            results[res_id].update(recipients)
    for (res_id, values) in results.iteritems():
        partner_ids = values.get('partner_ids', list())
        if self._context.get('tpl_partners_only'):
            mails = (tools.email_split(values.pop('email_to', '')) + tools.email_split(values.pop('email_cc', '')))
            for mail in mails:
                partner_id = self.env['res.partner'].find_or_create(mail)
                partner_ids.append(partner_id)
        partner_to = values.pop('partner_to', '')
        if partner_to:
            tpl_partner_ids = [int(pid) for pid in partner_to.split(',') if pid]
            partner_ids += self.env['res.partner'].sudo().browse(tpl_partner_ids).exists().ids
        results[res_id]['partner_ids'] = partner_ids
    return results
