You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo使用邮件模板生成邮件报错:'mail.template'无generate_email属性

问题解决:Odoo邮件模板生成邮件报错 AttributeError: 'mail.template' object has no attribute 'generate_email'

错误原因

在Odoo 13及以后的版本中,原有的generate_email方法已被重命名为_generate_email,且该方法返回的是包含邮件主题、HTML正文等内容的完整字典,并非单纯的邮件正文字符串。

修正后的代码

data = records
template_object = env['mail.template']
template = template_object.search([('name', '=', 'Custom_Email_Template')], limit=1)
if not template:
    raise ValueError("邮件模板 Custom_Email_Template 未找到")

for record in data:
    # 调用新版方法生成邮件内容字典
    email_data = template._generate_email([record.id])[record.id]
    email_values = {
        'subject': email_data.get('subject', ''),
        'body_html': email_data.get('body_html', ''),
        'email_to': 'some@example.com'
    }
    # 创建邮件并发送
    env['mail.mail'].create(email_values).send()

关键修正点

  • 将generate_email替换为_generate_email(注意方法名前的下划线)
  • _generate_email接收记录ID列表作为参数,返回以记录ID为键的字典,需通过[record.id]获取对应记录的邮件数据
  • 从返回的email_data字典中提取subject和body_html字段,无需手动空赋值
  • 增加模板存在性检查,避免因模板未找到导致后续报错

内容的提问来源于stack exchange,提问作者Muhammad Hazique Khatri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 20:39:58