Create Email Template and Send Email Programmatically in Odoo 1x

Odoo has a powerful email sending module that allows you to create and send emails programmatically. In this blog post, we will show you how to create an email template and send it using the Odoo email sending module.

Create Email Template

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">
        <!--Email template -->
        <record id="my_email_template" model="mail.template">
            <field name="name">First Email Template</field>
            <field name="email_from">${object.name and object.company_id.email or ''}</field>
            <field name="subject">New Feedback from ${object.company_id.name}</field>
            <field name="model_id" ref="my_module.model_custom_model"/>
            <field name="auto_delete" eval="True"/>            
            <field name="lang">${object.address_home_id.lang}</field>
            <field name="body_html"><![CDATA[

   Dear ${(object.name)}, <br/><br/>
   There is a new feedback on your website. You can read it here.

            ]]></field>
        </record>
    </data>
</odoo>

Send Email Programmatically

class MyModel(models.Model):
    _name = 'my.model'
    _description = 'My Model'

    @api.multi
    def send_email(self):
        body_html = 'Hi {},<br/><br/>'.format(someone_name)        
        body_html += '<div>The body goes here</div>'

        email_values = {
            'email_to': 'myemail@mysite.com',
            'email_from': 'from_someone_email@mysite.com',
            'body_html': body_html,
        }
        
        template_id = self.env['mail.template'].search([('model', '=', 'custom.model')], limit=1)
        template_id.send_mail(template_id.id, force_send=True, email_values=email_values)

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close