Send Email From Code in Odoo 17

TEAM-BASSAM
December 8, 2023
Send-Email-From-Code-in-Odoo-17

Email is an essential corporate communication medium that is required for many facets of managing a modern organization. The main form of communication for both internal and external stakeholders in the company is email. This is the easiest and most efficient way to convey information. Email enables us to communicate with partners, clients, and colleagues as well as send and receive messages and documents. Email captures verbal conversations in writing. Beyond these applications, email is an extremely practical tool in both personal and professional contexts. It provides a stable and useful means of written communication in the age of digitization.

We may go over how to make an email template in Odoo17 and send emails using it in this tutorial.

<odoo>

            <data>

            <record id="email_template_name" model="mail.template">

            <field name="name">EMAIL TEMPLATE </field>

            <field name="model_id" ref="module_name.model_sample_name"/>

            <field name="subject">{{ object.company_id.name }}</field>

            <field name="email_from">{{ (object.user_id.email) }}</field>

            <field name="partner_to">{{ object.partner_id.id }}</field>

            <field name="description">Description</field>

            <field name="body_html" type="html">

                        <div>

                                    <p>Hello,<br/><br/>First Email Template!<br/><br/>

                                    <br/>Mitchell Admin<br/><br/></p>

                        </div>

            </field>

            </record>

            </data>

</odoo>
The email template must include the email from and to fields, sometimes referred to as email_from and partner_to. These fields must be filled out.
<field name="email_from">{{ (object.user_id.email) }}</field>

<field name="partner_to">{{ object.partner_id.id }}</field>

We can add a subject by filling in the subject field.

<field name="subject">{{ object.company_id.name }}</field>

The body_html field is where the body goes.

<field name="body_html" type="html">

        <p> Email Body</p></field>

Email sent by clicking a button: Here, we can talk about including a button in the view and utilizing it for a function. Email sent by clicking on a link. The button will be visible in the model’s form view header if it is added to the view.

<record id="view_patient_form" model="ir.ui.view">

            <field name="name">hospital.patient.form</field>

            <field name="model">hospital.patient</field>

            <field name="arch" type="xml">

            <form string="Patient">

                        <header>

                                    <button name="action_send_mail" type="object" string="Send Email"                                class="oe_highlight"/>

                        </header>

                        <sheet>

                                    <group>

                                                <group>

                                                <field name="name"/>

                                                <field name="responsible_id"/>

                                                <field name="age"/>

                                                </group>

                                    </group>

                        </sheet>

            </form>

            </field>

</record>

In order to establish an action for a button with the name "action_send_mail," we must first build the action function in a Python file.

def action_send_mail(self):

            template = self.env.ref('hopital.email_template_name)

            template.send_mail(self.id, force_send=True)

We can send emails using the action_send_email() method; in this case, “email_template_name” in the provided template has to be replaced with the email template name.

The simplest and most direct approach to sending emails is to use a template, and making a template is quite simple.

Set up the SMTP settings in Odoo:

Make sure your Odoo instance’s SMTP settings are set to send to email. Odoo users can adjust this by visiting

Settings” > “General Settings” > “Outgoing Mail Servers.”

Plan the email’s delivery time:

You can plan an email to be sent at a given time or in response to a specific condition by using Odoo’s scheduled actions or other triggers.

That’s it! You can send emails from your custom code in Odoo 17 by following these steps.

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *