'Method to send email linked to notified messages. The recipients are
the recordset on which this method is called.
:param boolean force_send: send notification emails now instead of letting the scheduler handle the email queue
:param boolean send_after_commit: send notification emails after the transaction end instead of durign the
transaction; this option is used only if force_send is True
:param user_signature: add current user signature to notification emails'

@api.multi
def _notify_by_email(self, message, force_send=False, send_after_commit=True, user_signature=True):

    if (not self.ids):
        return True
    base_template = None
    if (message.model and self._context.get('custom_layout', False)):
        base_template = self.env.ref(self._context['custom_layout'], raise_if_not_found=False)
    if (not base_template):
        base_template = self.env.ref('mail.mail_template_data_notification_email_default')
    base_template_ctx = self._notify_prepare_template_context(message)
    if (not user_signature):
        base_template_ctx['signature'] = False
    base_mail_values = self._notify_prepare_email_values(message)
    if (message.model and message.res_id and hasattr(self.env[message.model], '_message_notification_recipients')):
        recipients = self.env[message.model].browse(message.res_id)._message_notification_recipients(message, self)
    else:
        recipients = self.env['mail.thread']._message_notification_recipients(message, self)
    emails = self.env['mail.mail']
    (recipients_nbr, recipients_max) = (0, 50)
    for (email_type, recipient_template_values) in recipients.iteritems():
        if recipient_template_values['followers']:
            template_fol_values = dict(base_template_ctx, **recipient_template_values)
            template_fol_values['has_button_follow'] = False
            template_fol = base_template.with_context(**template_fol_values)
            fol_values = template_fol.generate_email(message.id, fields=['body_html', 'subject'])
            (new_emails, new_recipients_nbr) = self._notify_send(fol_values['body'], fol_values['subject'], recipient_template_values['followers'], **base_mail_values)
            self._notify_udpate_notifications(new_emails)
            emails |= new_emails
            recipients_nbr += new_recipients_nbr
        if recipient_template_values['not_followers']:
            template_not_values = dict(base_template_ctx, **recipient_template_values)
            template_not_values['has_button_unfollow'] = False
            template_not = base_template.with_context(**template_not_values)
            not_values = template_not.generate_email(message.id, fields=['body_html', 'subject'])
            (new_emails, new_recipients_nbr) = self._notify_send(not_values['body'], not_values['subject'], recipient_template_values['not_followers'], **base_mail_values)
            self._notify_udpate_notifications(new_emails)
            emails |= new_emails
            recipients_nbr += new_recipients_nbr
    test_mode = getattr(threading.currentThread(), 'testing', False)
    if (force_send and (recipients_nbr < recipients_max) and ((not self.pool._init) or test_mode)):
        email_ids = emails.ids
        dbname = self.env.cr.dbname
        _context = self._context
        def send_notifications():
            db_registry = registry(dbname)
            with api.Environment.manage():
                with db_registry.cursor() as cr:
                    env = api.Environment(cr, SUPERUSER_ID, _context)
                    env['mail.mail'].browse(email_ids).send()
        if ((not test_mode) and send_after_commit):
            self._cr.after('commit', send_notifications)
        else:
            emails.send()
    return True
