Odoo12“通过邮件发送”按钮简单实现

Odoo12“通过邮件发送”按钮简单实现

前提是服务器已配置好邮件发件服务器,否则发也发不出去滴!

创建一个邮件模板(本文以开发的薪资核算表为例):

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">
        <record id="wage_payroll_accounting_email_template" model="mail.template">
            <field name="name">薪酬核算明细员工邮件模板</field>
            <field name="model_id" ref="odoo_wage_manage.model_wage_payroll_accounting"/>
            <field name="subject">${object.employee_id.name}在${object.date_code}的工资核算明细!</field>
            <field name="email_from">${object.sudo().create_uid.email_formatted | safe}</field>
            <field name="email_to">${object.employee_id.work_email}</field>
            <field name="body_html" type="html">
                <table border="0" cellpadding="0" cellspacing="0" style="padding-top: 16px; background-color: #F1F1F1; font-family:Verdana, Arial,sans-serif; color: #454748; width: 100%; border-collapse:separate;">
                    <tr>
                        <td align="center">
                            <table border="0" cellpadding="0" cellspacing="0" width="590" style="padding: 16px; background-color: white; color: #454748; border-collapse:separate;">
                                <tbody>
                                    <!-- HEADER -->
                                    <tr>
                                        <td align="center" style="min-width: 590px;">
                                            <table border="0" cellpadding="0" cellspacing="0" width="590" style="min-width: 590px; background-color: white; padding: 0px 8px 0px 8px; border-collapse:separate;">
                                                <tr>
                                                    <td valign="middle">
                                                        <span style="font-size: 20px; font-weight: bold;">
                                                            ${object.name}
                                                        </span>
                                                    </td>
                                                    <td valign="middle" align="right">
                                                        <img src="/logo.png?company=${object.company_id.id}" style="padding: 0px; margin: 0px; height: auto; width: 80px;" alt="${object.company_id.name}"/>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan="2" style="text-align:center;">
                                                        <hr width="100%" style="background-color:rgb(204,204,204);border:medium none;clear:both;display:block;font-size:0px;min-height:1px;line-height:0; margin: 16px 0px 16px 0px;"/>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <!-- CONTENT -->
                                    <tr>
                                        <td align="center" style="min-width: 590px;">
                                            <table border="0" cellpadding="0" cellspacing="0" width="590" style="min-width: 590px; background-color: white; padding: 0px 8px 0px 8px; border-collapse:separate;">
                                                <tr>
                                                    <td valign="top" style="font-size: 13px;">
                                                        <div>
                                                            ${object.employee_id.name},您<strong>${object.date_code}</strong>期间薪资已核算完成,请知悉!
                                                            <br/>
                                                            <br/>
                                                            基本工资:<strong>${object.base_wage}</strong>
                                                            <br/>
                                                            <br/>
                                                            薪资项目合计:<strong>${object.structure_sum}</strong>
                                                            <br/>
                                                            <br/>
                                                            应出勤天数:<strong>${object.attendance_days}</strong>
                                                            <br/>
                                                            <br/>
                                                            缺勤扣款合计:<strong>${object.absence_sum}</strong>
                                                            <br/>
                                                            <br/>
                                                            绩效合计:<strong>${object.performance_sum}</strong>
                                                            <br/>
                                                            <br/>
                                                            加班费合计:<strong>${object.overtime_sum}</strong>
                                                            <br/>
                                                            <br/>
                                                            打卡扣款合计:<strong>${object.attendance_sum}</strong>
                                                            <br/>
                                                            <br/>
                                                            社保个人合计:<strong>${object.statement_sum}</strong>
                                                            <br/>
                                                            <br/>
                                                            本月个税:<strong>${object.this_months_tax}</strong>
                                                            <br/>
                                                            <br/>
                                                            应发工资:<strong>${object.pay_wage}</strong>
                                                            <br/>
                                                            <br/>
                                                            实发工资:<strong>${object.real_wage}</strong>
                                                            <br/>
                                                            <br/>
                                                            备注:<strong>${object.notes}</strong>
                                                        </div>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="text-align:center;">
                                                        <hr width="100%"
                                                            style="background-color:rgb(204,204,204);border:medium none;clear:both;display:block;font-size:0px;min-height:1px;line-height:0; margin: 16px 0px 16px 0px;"/>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <!-- FOOTER -->
                                    <tr>
                                        <td align="center" style="min-width: 590px;">
                                            <table border="0" cellpadding="0" cellspacing="0" width="590"
                                                   style="min-width: 590px; background-color: white; font-size: 11px; padding: 0px 8px 0px 8px; border-collapse:separate;">
                                                <tr>
                                                    <td valign="middle" align="left">
                                                        ${object.company_id.name}
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td valign="middle" align="left" style="opacity: 0.7;">
                                                        ${object.company_id.phone}
                                                        % if object.company_id.email
                                                        |
                                                        <a href="'mailto:%s' % ${object.company_id.email}"
                                                           style="text-decoration:none; color: #454748;">
                                                            ${object.company_id.email}
                                                        </a>
                                                        % endif
                                                        % if object.company_id.website
                                                        |
                                                        <a href="'%s' % ${object.company_id.website}"
                                                           style="text-decoration:none; color: #454748;">
                                                            ${object.company_id.website}
                                                        </a>
                                                        % endif
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                </table>
            </field>
            <field name="report_name">RFQ_${(object.name or '').replace('/','_')}</field>
            <field name="user_signature" eval="False"/>
            <field name="auto_delete" eval="True"/>
        </record>
    </data>
</odoo>

在薪资核算表单中添加发送邮件按钮

.....此处省略前部分代码......
<header>
    <field name="email_state" invisible="1"/>
    <button name="action_send_employee_email" string="通过Email发送" type="object" class="oe_highlight"
            attrs="{'invisible': [('email_state','=', True)]}"/>
    <button name="action_send_employee_email" string="重新发送邮件" type="object"
            attrs="{'invisible': [('email_state','=', False)]}" confirm="确认重新发送吗?"/>
</header>
.....此处省略后部分代码......

模型发送邮件的函数

@api.multi
def action_send_employee_email(self):
    """
    发送邮件功能函数
    :return: 
    """
    self.ensure_one()
    if not self.employee_id.work_email:
        raise UserError('员工%s未设置工作邮箱,无法发送!' % self.employee_id.name)
    template_id = self.env.ref('odoo_wage_manage.wage_payroll_accounting_email_template', raise_if_not_found=False)
    if template_id:
        template_id.sudo().with_context(lang=self.env.context.get('lang')).send_mail(self.id, force_send=True)
        self.email_state = True

差不多了,就这几步。

需要用到mail模块中send_mail函数:

文件位置:source/addons/mail/models/mail_template.py

@api.multi
    def send_mail(self, res_id, force_send=False, raise_exception=False, email_values=None, notif_layout=False):
  生成一个新的mail.mail。 模板在记录中呈现
  res_id和模型来自模板。
  :param int res_id:呈现模板的记录的id
  :param bool force_send:立即发送电子邮件; 否则使用邮件队列(推荐);
  :param dict email_values:使用这些值更新生成的邮件 定制邮件;
  :param str notif_layout:封装的可选通知布局 生成电子邮件

效果图

email_odoo2.png

odoo_email1.png

email_odoo3.png

Last modification:September 5th, 2019 at 10:02 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment