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 openerp.py

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

<openerp>

<data>       

    <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">lms@uet.edu.pk</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>

            <ol>

                <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>

            </ol>

            <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>Regards,</p>

            <p>Oric Department</p>

            <p><strong>--campus--</strong></p>

        ]]>

        </field>

    </record>

</data>

</openerp>       
 




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 models.py file

XML Code
<button string="Send Email"
 type="object"
 name="btn_send_email "
 class="oe_button oe_tree_button oe_highlight"
 confirm="Are you sure you want to Send email to students?"    
/>     
 

Models.py
    @api.one
    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
  body=body.replace('--department--',self.department_id.name)
  body=body.replace('--session--',self.session_id.name)
  body=body.replace('--supervisor--',self.supervisor_id.name)
  body=body.replace('--rollno--',self.student_id.roll_no)
  body=body.replace('--caseno--',self.case_no)
  body=body.replace('--campus--',self.campus_id.name)
  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()       
 


No comments:

Post a Comment