odoo12发送邮件(使用mail.template)

编写邮件模板

<?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>

效果图

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值