Enable Dark Mode!
how-to-send-email-from-code-in-odoo-17.jpg
By: Busthana Shirin

How to Send Email From Code in Odoo 17

Technical Odoo 17

Email is a vital tool for corporate communication and is essential to many facets of contemporary business operations. When it comes to communication, email is the main channel used both inside and outside the company. It is the most straightforward and effective way to transmit information. We can communicate with coworkers, clients, and partners via email and send messages and documents. Email keeps track of conversations in writing. Email is a versatile and essential tool in both personal and professional contexts, and it has a wide range of applications beyond these examples. It provides a convenient and reliable means of written communication in the digital age.
We may talk about creating an email template in Odoo17 and sending emails with a button click in this blog.
For the purpose of communicating information to clients, suppliers, staff members, and other parties, Odoo offers an email option in every module. Through modification, we are able to set the Odoo email send option.
<odoo>
   <data>
       <record id="email_template_name" model="mail.template">
           <field name="name">EMAIL TEMPLATE NAME</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">Add Description</field>
           <field name="body_html" type="html">
               <div>
                   <p>
                       Hello,
                       <br/>
                       <br/>
                       This is our First Email Template!
                       <br/>
                       <br/>
                       <br/>Mitchell Admin
                       <br/>
                       <br/>
                   </p>
               </div>
           </field>
       </record>
   </data>
</odoo>
The email_from and partner_to fields, which are the email from and to fields, must be added to the email template. These fields are required.
<field name="email_from">{{ (object.user_id.email) }}</field>
<field name="partner_to">{{ object.partner_id.id }}</field>
We can add a subject in the subject field.
<field name="subject">{{ object.company_id.name }}</field>
The body can be added to the body_html field.
<field name="body_html" type="html">
<p> Email Body</p>
</field>
Sent Email by button click:
Email sent with a mouse click it is possible to talk about adding a button to the view and using it in a function here. Should we include the button in the view, it will be observable in the model's form view header.
<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>
For a button with the name "action_send_mail," we may define an action. The action function needs to be created 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 an email by using the action_send_email() method; in this case, we must replace "email_template_name" in the provided template with the email template name.
The simplest and most straightforward way to send emails is by using a template, and making a template is also quite simple.
Set up the SMTP settings for Odoo:
Verify that the SMTP settings on your Odoo instance are set up to send emails. In Odoo, you may adjust this by heading to "Settings" > "General Settings" > "Outgoing Mail Servers."
Plan the email's dispatch:
If you want to schedule the email to be sent at a specific time or based on certain conditions, you can use Odoo's scheduled actions or other triggers.
That's it! With these steps, you can send emails from your custom code in Odoo 17. Make sure to adapt the code to your specific needs and customize the email content as required.
To read more about sending emails from Code in Odoo 16, refer to our blog How to Send Email From Code in Odoo 16.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message