如何在 Odoo v8 中通过代码发送带有模板的邮件?

How to send a mail with a template through code in Odoo v8?

我正在尝试通过 Python 发送邮件(已加载模板)。

我在版本 7 中使用 email.template 模型的函数 send_mail 完成了此操作。现在,我想要它用于版本 8,但我无法管理它。

这不是这些 SMTP 服务器或模板的问题,因为如果我手动发送邮件,它就会正常运行。

它似乎没有进入函数send_mail(我在函数的第一行上面写了一条记录器信息消息,但它从未出现过)。

这是我的代码:

return self.env['email.template'].send_mail(
   self.env.cr, self.env.uid, template.id, self.id, force_send=True,
   context=self.env.context)

我还检查了函数需要的 template.idself.id 参数是否正确。但是没有错误,没有消息,它忽略了这个功能。我也尝试过不使用 cruidcontext,但结果相同。

顺便说一句,send_mail函数有一个我以前从未见过的装饰器,@api.cr_uid_id_context,我不知道它的含义。

有人能帮帮我吗?

一旦我为类似目的创建了一个模板,请检查它并尝试解决您的问题。

   for rec in job_records:
         _logger.error('In project task listttttttttttttttttttttttttttttt  %s', rec.job_id.partner_id.email)
         attachment_ids = self.pool.get('ir.attachment').search(cr, uid, [('res_model','=', 'hr.applicant'),('res_id','=',rec.id),('blind_cv','=','True')])
         message = ("Hello %s \n Below Prurchase Order is  Delay Today: \n Supplier Name : %s \n Purchase Order Refereance : %s \n Order Date : %s \n Delivery Date : %s \n Product Names : %s \n\n")
        # context.update({'default_student_ids' : [(6, 0, [rec.id])]})
         vals = {'state': 'outgoing',
                         'subject': 'CVs',
                         'body_html': '<pre>%s</pre>' % message,
                         'email_to': rec.job_id.partner_id.email,
                         'email_from': 'rahuls.logicious@gmail.com',
                         'attachment_ids': [(6, 0, attachment_ids)],

                 }
         email_ids.append(self.pool.get('mail.mail').create(cr, uid, vals, context=context))
     if email_ids:
         self.pool.get('mail.mail').send(cr, uid, email_ids, context=context)

一段时间后我发现了问题。我将我的代码与调用相同函数的其他模块进行了比较,并尝试模拟它们。

最后我设法用 pool 而不是 env 调用 email.template 模型,这样:

return self.pool['email.template'].send_mail(
   self.env.cr, self.env.uid, template.id, self.id, force_send=True,
   context=self.env.context)

现在它进入该功能并且可以正常工作,使用所需模板发送邮件。

你可以这样做:

def send_email(self, cr, uid, ids, vals, context=None):
    # Set Context with ids
    compose_ctx = dict(context,active_ids=ids)
    # Set your template search domain
    search_domain = [('name', '=', 'Application accepted')]
    # Get template id
    template_id = self.pool['email.template'].search(cr, uid, search_domain, context=context)[0]
    # Compose email
    compose_id = self.pool['mail.compose.message'].create(cr, uid, {
        'model': self._name,
        'composition_mode': 'mass_mail',
        'template_id': template_id,
        'post': True,
        'notify': True,
        }, context=compose_ctx)
    self.pool['mail.compose.message'].write(cr, uid, [compose_id], self.pool['mail.compose.message'].onchange_template_id(cr, uid, [compose_id], template_id, 'mass_mail', self._name, False,context=compose_ctx)['value'],context=compose_ctx)
    # Send mail with composed id
    self.pool['mail.compose.message'].send_mail(cr, uid, [compose_id], context=compose_ctx)

当使用新的API(self.env而不是self.pool)时,您不仅不再需要通过cr/uid /context 参数,因为您通常也不传递 id(s);相反,您 browse() 获取一个对象,然后调用它的方法。

尝试:

return template.send_mail(self.id, force_send=True)