【问题标题】:How to send a mail with a template through code in Odoo v8?如何通过 Odoo v8 中的代码发送带有模板的邮件?
【发布时间】:2015-09-01 12:16:24
【问题描述】:

我正在尝试通过 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,我不知道它的含义。

有人可以帮帮我吗?

【问题讨论】:

    标签: python python-2.7 odoo-8 odoo


    【解决方案1】:

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

    试试:

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

    【讨论】:

    • 我发现这很有帮助。谢谢
    【解决方案2】:

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

    最后我用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)
    

    现在它进入了函数并且它工作了,邮件使用所需的模板发送。

    【讨论】:

      【解决方案3】:

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

         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)
      

      【讨论】:

      • 谢谢你,我赞成你,但该代码或多或少是函数send_mail 的内容,我刚刚发现了问题。我要发了。
      【解决方案4】:

      你可以这样做:

      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)
      

      【讨论】:

        猜你喜欢
        • 2019-10-15
        • 1970-01-01
        • 1970-01-01
        • 2017-01-28
        • 2015-10-21
        • 2020-03-23
        • 1970-01-01
        • 2020-11-14
        • 2015-11-11
        相关资源
        最近更新 更多