Monday, August 21, 2023

how to send email programmatically in odoo

In this blog we will explain how to send email programmatically in odoo

First : we have to create our email template just as bellow  

<?xml version="1.0" ?>
<odoo>
    <data noupdate="1">
        <record id="email_template_id" model="mail.template">
            <field name="name">Custom_Email_Template</field>
            <field name="model_id" ref="module_name.model_your_model_name"/>
            <field name="email_from">iap@odoo.com</field>
            <field name="partner_to">${object.partner_id.id}</field>
            <field name="subject">${object.company_id.name}</field>
            <field name="body_html" type="html">
                <div style="margin: 0px; padding: 0px;">
                    Email body
                </div>
            </field>
        </record>
    </data>
</odoo>

Second : create the python function that will send the email

def action_mail_send(self):
        template_id = self.env['mail.template'].search([('name', '=', 'Custom_Email_Template')])

        ctx = {
            'default_model': 'model_name',
            'default_res_id': record_id,
            'default_use_template': bool(template_id.id),
            'default_template_id': template_id.id,
            'default_composition_mode': 'comment',
            'mark_so_as_sent': True,
            'custom_layout': "mail.mail_notification_paynow",
            'proforma': self.env.context.get('proforma', False),
            'force_email': True,
            'default_check_value': True,
            'default_is_log': True,
        }
        self.env['mail.compose.message'].with_context()._action_send_mail()

 

The above code works for odoo 16 & 15 but in case we are work on odoo 14 we can use the code bellow

template_id = self.env.ref('hr_recruitment.email_template_data_applicant_congratulations')
        template_id.send_mail(self.id, force_send=True)

No comments:

Post a Comment

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...