1. 新建邮件模板 【 send_mail_template.xml 】参数说明:email_from:邮件发送邮箱subject:邮件标题model_id: 关联发送邮件的模型email_to:接收邮箱注意:model_id的格式为 [模块名.model_模型名],例如模块名为A,A里面定义了一个model为B,那么此处的model_id为A.model_B,B的名字如果包含".",则使用"_"代替"."

<?xml version="1.0" encoding="utf-8"?> <odoo> <data noupdate="0"> <record id="send_mail_template" model="mail.template"> <field name="name">审批任务</field> <field name="email_from">xxxxxxx@qq.com</field> <field name="subject">{{(object.title)}}</field> <field name="model_id" ref="user_manage.model_user_manage"/> <field name="email_to">xxxxxx@163.com</field> <field name="body_html" type="html"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"/> <title>审批任务</title> <style> span.oe_mail_footer_access { display:block; text-align:center; color:grey; } </style> </head> <body> <div style="border-radius: 2px; max-width: 1200px; height: auto;margin-left: auto;margin-right: auto;background-color:#f9f9f9;"> <div style="height:auto;text-align: center;font-size : 30px;color: #8A89BA;"> <strong>有新的审批任务,请查看 http://www.xxxxx.com</strong> </div> </div> </body> </html> </field> </record> </data> </odoo>

2. 在模块的__mainfest___.py中添加邮件模板视图,并依赖mail模块

# 在depends中添加"mail" 'depends': ['base', 'web', 'mail'], # 在data中加入邮件模板 'data': [ 'security/ir.model.access.csv', 'views/views.xml', 'views/send_mail_template.xml',

3. 在models.py中继承邮件模型

class user_manage(models.Model): _name = 'user_manage' _inherit = ['mail.thread', 'mail.activity.mixin'] _description = '人员管理主模型'

4. 在models中编写发送邮件的方法

# 发送邮件 def send_mail(self): self.ensure_one() # 查找邮件模板 template_id = self.env.ref('user_manage.send_mail_template', raise_if_not_found=False) if template_id: # 传递邮件参数,可以在这修改邮件接收人,发送人等参数 email_values = { 'email_to': 'xxxxx@qq.com,xxxx@163.com' } template_id.sudo().with_context(lang=self.env.context.get('lang')).send_mail(self.id, force_send=True, email_values=email_values)

5. 需要在odoo设置中勾选"外部服务器",设置"别域名"为外部服务器域名例如:qq.com

erpodoo使用教程(使用自定义模板发送邮件)(1)

6. 设置发件服务器示例如下:

erpodoo使用教程(使用自定义模板发送邮件)(2)

7. 如果上述操作都已完成,但是发送邮件报错的话,可以尝试修改如下:

,