Enable Dark Mode!
how-to-send-email-from-code-in-odoo-15.jpg
By: Aswathi PN

How to Send Email from Code in Odoo 15?

Technical Odoo 15

In every business, communication has significant importance. Communication via email is one of the best and simple methods provided by Odoo. Email communication is one of the fastest methods for exchanging information between people.

There are many more advantages of using email communication that is

a) There is no cost for using email so it is a very cheap method.

b) Sending Information via email is a very fast method.

c) It is very easy to use for business communications.

d) Most convenient 

e) Easy to copy

In this blog, we can discuss briefly how to create an Odoo email template, and how to send an email through a button click in Odoo 15.

In every module, Odoo provides an option to send emails, which will be helpful for sending out information to customers, vendors,m employees, or other concerned people. If the capability to create and send an email is not available in a menu or a module it can be brought in by customization of the Odoo platform.

How to create an Odoo email template?

We can discuss how to create an email template using an XML code, the Odoo email template will be mainly created using XML codes.

Example code for Odoo email template;

<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="auto_delete" eval="True"/>
           <field name="email_from">{{object.res_user_id.email}}</field>
           <field name="email_to">{{object.client_name.email}}</field>
           <field name="report_template" ref="action_example_pdf"/>
           <field name="subject">{{object.amc}}</field>
           <field name="body_html">
               <p>
                Dear<t t-out="object.client_name.name"/>,
                   <br/>
                   <br/>
                   Good job, this is our first e-mail template!
                   <br/>
               </p>
               Regards,
               <br/>
               <t t-out="object.company_id.name"/>
           </field>
       </record>
   </data>
</odoo>

In the email templates you must add the email_from field and the email_to field, which are the mandatory fields in email templates;

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

Here we have to add the subject of the email that we have to send;

<field name="body_html">
        <p>Email Body</p>          
</field>

Inside the ‘body_html’ field we have to add the contents, messages, etc.. of that email.

Sent email by Button Click

We can add a button named “sent by Email” to send the email to customers or others.

Example code for adding a button on a form 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>
               <header>
                       <button name="action_send_email" string="Send by email" type="object"/>
</header>
    <sheet>
         <group>
             <group>
                 <field name="name"/>
                      <field name="responsible_id"/>
                           <field name="age"/>
                           <field    name="appointment_count"/>
                       </group>
                   </group>
                </sheet>
            </form>
       </field>
   </record>

We can see a sent by email button on the form view;

how-to-send-email-from-code-in-odoo-15-cybrosys

We can define an action for that button named “action_sent_mail”. Moreover, the action is been defined in the Python file.

def action_send_email(self):
   mail_template = self.env.ref('om_hospital.email_template_name')
   mail_template.send_mail(self.id, force_send=True)

We can send an email by calling the method action_send_email(). The ”email_template_name” is the email template that we created.

Sending email with the help of an email template is a very simple and easy method and creating email templates is also simple.


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