Wednesday 19 April 2017

Send mail On Click Button In Odoo

Send mail On Click Button In Odoo

Please First Put your button in your view.
I will give example for when we click confirm order in sale order at that time send mail.
You can use your button insted of confirm button.

First You have to create email template like as below

            <record id="email_template_id" model="email.template">
            <field name="name">Template Name</field>
            <field name="email_from">${object.user_id.email or object.company_id.email or                    'noreply@localhost'|safe}</field>
            <field name="subject">Add Subject Name ${object.name}</field>
            <field name="model_id" ref="sale.model_sale_order"/> #You can give your own model name
            <field name="auto_delete" eval="True"/>
            <field name="body_html"><![CDATA[
<div>
    <p>Hello ${object.partner_id.name},</p>
    <p>Thank you for your order ${object.name}, We appreciate your Business!</p>
</div>
            ]]></field>
            </record>


@api.multi
def action_button_confirm(self):
    template_id = self.env['ir.model.data'].get_object_reference('module_name', 'your_email_template_name')[1]
    template_id.email_to = 'email id'
    template_id.email_from = 'Give your mail id'
    template_id.send_mail()
    Ex.
    template_id = self.env['ir.model.data'].get_object_reference('sale_order_enhance', 'email_template_id')[1]
    template_id.email_to = 'abc@gmail.com'
    template_id.email_from = self.company_id.email
    template_id.send_mail()


  • Without Template Send Mail.


Create Template detail and store in one variable like as below.

body_html = '''
    <div>
        <p>
        Hello,
        <br/><br/>
            <br/><br/>
            The details of template is as below.
            <br/><br/>
        </p>
        <table border="1" cellpadding="5" cellspacing="1">
            <tbody>
                <tr>
                    <td><strong>Value 1:</strong></td><td>''' + do + '''</td>
                </tr>
                <tr>
                    <td><strong>Value 2:</strong></td><td>''' + self.field name + '''</td>
                </tr>
                <tr>
                    <td><strong>Value 3:</strong></td><td>''' + self.field name + '''</td>
                </tr>
                <tr>
                    <td><strong>Value 4:</strong></td><td>''' + self.field name + '''</td>
                </tr>
            </tbody>
        </table>
    </div>
    '''
mail_values = {
            'email_from': 'From mail id',
            'email_to': 'To mail id',
            'subject': 'Subject',
            'body_html': body_html,
            'state': 'outgoing',
            'type': 'email',
        }
        mail_id = mail_mail.create(mail_values)
        if mail_id:
            mail_id.send()

Odoo 17 New Features

  Odoo 17 new Features 1) Duplicate  multiple records from List view. Please have look below screenshots first you need to select records th...