How to send an email from a button located in the action dropdown of an Odoo 11 form?

别说谁变了你拦得住时间么 提交于 2019-11-28 14:33:39

I have understood that you want to add a button in the actions section of the form of the model hr.payslip, created by the module hr_payroll.

I see that you are creating a new model named email.payslip. That is not necessary to achieve your purpose, check the following steps:

Modify the __manifest__.py of your module to make it depend on hr_payroll and mail:

'depends': [
    'hr_payroll',
    'mail',
],

Modify your XML action this way:

<record id="action_email_payslip" model="ir.actions.server">
    <field name="name">Email Payslip</field>
    <field name="model_id" ref="hr_payroll.model_hr_payslip"/>
    <field name="binding_model_id" ref="hr_payroll.model_hr_payslip"/>
    <field name="state">code</field>
    <field name="code">
if records:
    action = records.action_email_payslip_send()
    </field>
</record>

This is to create a button in the actions section of the views of the model hr.payslip. The button will call a Python method of this model, which will be in charge of calling the pop-up to send an email.

Now let's define that method in Python:

class HrPayslip(models.Model):
    _inherit = 'hr.payslip'

    @api.multi
    def action_email_payslip_send(self):
        self.ensure_one()
        template = self.env.ref(
            'your_module.email_template_payslip',
            False,
        )
        compose_form = self.env.ref(
            'mail.email_compose_message_wizard_form',
            False,
        )
        ctx = dict(
            default_model='hr.payslip',
            default_res_id=self.id,
            default_use_template=bool(template),
            default_template_id=template and template.id or False,
            default_composition_mode='comment',
        )
        return {
            'name': _('Compose Email'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'mail.compose.message',
            'views': [(compose_form.id, 'form')],
            'view_id': compose_form.id,
            'target': 'new',
            'context': ctx,
        }

Just replace your_module by the technical name of your module. This method will open the form to send an email, and we are telling it to load by default our custom email template, whose XML ID is email_template_payslip.

Now, we have to define that email template in XML. Create a new folder named data in the root path of your module, put inside the XML file, for example, named email_template_data.xml. Do not forget to add in the data key of your __manifest__.py the line 'data/email_template_data.xml', to tell your module that it must load that XML file content:

<?xml version="1.0" encoding="UTF-8"?>
<odoo noupdate="0">

        <record id="email_template_payslip" model="mail.template">
            <field name="name">Payslip - Send by Email</field>
            <field name="email_from">${(user.email or '')|safe}</field>                
            <field name="subject">${object.company_id.name|safe} Payslip (Ref ${object.name or 'n/a' })</field>
            <field name="email_to">${(object.employee_id.work_email or '')|safe}</field>
            <field name="model_id" ref="hr_payroll.model_hr_payslip"/>
            <field name="auto_delete" eval="True"/>
            <field name="lang">${(object.employee_id.user_id.lang or user.lang)}</field>
            <field name="body_html"><![CDATA[
<div style="font-family: 'Lucida Grande', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF; ">

    <p>Hello ${object.employee_id.name},</p>

    <p>Here is your payslip from ${object.company_id.name}: </p>

    <p style="border-left: 1px solid #8e0000; margin-left: 30px;">
       &nbsp;&nbsp;Name: <strong>${object.name}</strong><br />
    </p>
    <p>If you have any question, do not hesitate to contact us.</p>
    <p>Thank you for choosing ${object.company_id.name or 'us'}!</p>
    <br/>
    <br/>
</div>
            ]]></field>
        </record>

</odoo>

In the ctx variable you have every data you added in the Python method. In the object variable, every field of the current hr.payslip record. You can use dot notation to reach any relational field. Check out other email templates to learn more about Mako language.

If you definitely want to use your model email.payslip, you should do almost the same process (depending on what you want exactly) and replace the hr.payslip references by email.payslip ones.

Once you are pretty sure that you are making any modification no more on your email template, you can turn the noupdate attribute to 1, to let Odoo users to customise the email template from the interface without losing their changes in case your module is updated:

<odoo noupdate="1">
    ...
</odoo>

Once you see the email pop-up and the template loaded OK by default, remember to check these three steps:

  1. The work email of the employee of the current payslip record must be filled in (since it is the destination of the email).
  2. You must have configured your outgoing mail server.
  3. Check the cron task Mail: Email Queue Manager. It must be active and running each minute (if you want to send the email in one minute at the most), or just click on Run Manually. Also the parameter force_send could be set to True in the email, in order to not depend on cron jobs.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!