编写邮件模板
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="0">
<record id="send_msg_template" model="mail.template">
<field name="name">邮件提醒</field>
<field name="email_from">${object.sudo().create_uid.email_formatted | safe}</field>
<field name="subject">${object.order_num}</field>
<field name="model_id" ref="pdc_laboratory.model_laboratory_approval"/>
<field name="email_to">
${(object.head_user.email)}
</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>你借用的设备逾期了,请及时归还!</strong>
</div>
</div>
</body>
</html>
</field>
</record>
</data>
</odoo>
把写好的模板添加到__manifast__.py中,并且继承mail模块(‘depends’: [‘base’, ‘mail’])
模型中继承 mail.thread,编写发送方法
@api.multi
def action_send_email(self):
self.ensure_one()
if not self.head_user.email:
raise UserError('用户%s未设置邮箱,无法发送!' % self.head_user.name)
template_id = self.env.ref('pdc_laboratory.send_msg_template', raise_if_not_found=False)
if template_id:
# 调用了/addons/mail/models/mail_template.py的send_mail(),参数1(res_id)是呈现模板的记录的id,force_send:是否立即发送(否则使用邮件队列)
template_id.sudo().with_context(lang=self.env.context.get('lang')).send_mail(self.id, force_send=True)
在模型的form视图上创建按钮并绑定
<header>
<button string="发送邮件" type="object" name="action_send_email" class="btn btn-success" confirm="确定发送?"/>
</header>
效果图