Thursday, 10 August 2017

Odoo how to send email on button click

Most of the time we need to send an email after successfully completion of some tasks or event. To day I will show you how to send an email in odoo using button click.

To send an email in odoo first of all we need create email template. We can create this email template using following two ways.

  1. By using odoo Template Menu Interface
  2. By using xml (code)

By using odoo interface firstly we create a email template by clicking on “Create” button and fill the form.

Here our focus is to create email template using xml. Follow these steps to create email template through code.

  1. Create new xml file in your module
  2. Copy below code and past in your newly created xml file
  3. Register your newly created xml file in

XML Code
<?xml version="1.0" encoding="utf-8"?>



    <record id="create_section_for_thesis_template" model="email.template">

        <field name="name">Create Section for Thesis</field>

        <field name="model_id" ref="model_postgraduate_thesis"></field>

        <!-- <field name="email_from"></field> -->

        <!-- <field name="email_cc">${(self.env.user.login)|safe}</field> -->

        <field name="subject">Create a Section for Thesis </field>

        <field name="body_html"><![CDATA[

            <p><strong>Dear Coordinator(s)</strong></p>

            <p><strong>Department: --department--</strong></p>

            <p>Please do the following actvities in LMS.</p>


                <li>Create a section with subject type thesis for this <strong>--session--</strong>.</li>

                <li>In newly created section, Assigned the <strong>--supervisor--</strong> to the section's faculty.</li>

                <li>Register This <strong>--rollno--</strong> in this thesis section.</li>

                <li>Assign the IP grade to this section.</li>


            <p>For more information, Please review the thesis track for case no <strong>--caseno--</strong>.</p>

            <p>After successfully complestion of above activities, please reply-all to this email thread.</p>


            <p>Oric Department</p>







Now its time to write button method/function to send an email. Follow these steps to create a button and its event/function. 

  1. Create a new xml file and declare a “Button” in that file
  2. Handle button method in file

XML Code
<button string="Send Email"
 name="btn_send_email "
 class="oe_button oe_tree_button oe_highlight"
 confirm="Are you sure you want to Send email to students?"    
    def proposal_title_approved(self):
  template_obj = self.env['email.template'].sudo().search([('name','=','Create Section for Thesis')], limit=1)
  body = template_obj.body_html
  if template_obj:
   mail_values = {
    'subject': template_obj.subject,
    'body_html': body,
    'email_to':';'.join(map(lambda x: x, receipt_list)),
    'email_cc':';'.join(map(lambda x: x, email_cc)),
    'email_from': template_obj.email_from,
   create_and_send_email = self.env['mail.mail'].create(mail_values).send()       


  1. Hello
    so how to Handle button method in file?
    now it return this error
    AttributeError: 'res.letter' object has no attribute 'btn_send_email

  2. Hey,
    You must create button code "btn_send_email" in XML of your "res.letter" model.

  3. can we write btn_send_email instead of proposal_title_approved function?

    If I didn't have an attribute name btn_send_email.

    and It didn't work yet. How it will work please let me know.

  4. Good mornning. Please I get this error when using the above xml file. Can some one help me solve this ?
    ParseError: "External ID not found in the system: sunu__stock.model_postgraduate_thesis"

    1. sunu__stock.model_postgraduate_thesis

      In above line you just need to change your model name like " sunu__stock.model_your_model_goes_here"