This question has been flagged
2 Replies
4940 Views

Hi, guys,

  I'm trying to add email function on my own module, however, when I click the "Send e-mail" button, there always comes out an error called external ID could not found. Any ideas?


here is my code

 >>emailDemo.xml

 <?xml version="1.0" encoding="utf-8"?>
    <odoo>
        <data noupdate="1">
            <record id="ailsaodoo.example_email_template" model="mail.template">
                <field name="name">Example e-mail template</field>
                <field name="email_from">${object.company_id and object.company_id.email or ''}</field>
                <field name="subject">Congratz ${object.name}</field>
                <field name="email_to">${object.email|safe}</field>
                <field name="lang">${object.lang}</field>
                <field name="model_id" ref="base.model_res_partner"/>
                <field name="auto_delete" eval="True"/>
                <field name="body_html">
                    <![CDATA[
                        <p>Dear ${(object.name)},<br/><br/>
                        Good job, you've just created your first e-mail template!<br/></p>
                        Regards,<br/> ${(object.company_id.name)}
                    ]]>
                </field>
            </record>
        </data>
    </odoo>



>>emailBtn.xml

<odoo>

<data>

<record id="send_mail_partner_form_inherit" model="ir.ui.view">

<field name="inherit_id" ref="base.view_partner_form"/>

<field name="model">res.partner</field>

<field name="arch" type="xml">

<!-- Add a header with button to the existing view -->

<xpath expr="//sheet" position="before">

<header>

<button name="send_mail_template" string="Send e-mail" type="object" class="oe_highlight"/>

</header>

</xpath>

</field>

</record>

</data>

</odoo>

>>_manifest_.py

'views/emailDemo.xml',

'views/emailBtn.xml',

>>email.py

from odoo import models,fields, api
class res_partner(models.Model):
    _inherit = 'res.partner'
    
    @api.multi
    def send_mail_template(self):
        template = self.env.ref('mail_template.ailsaodoo_example_email_template')
       self.env['mail.template'].browse(template.id).send_mail(self.id)
Avatar
Discard
Best Answer

You need to write external ID(module_name.external_id_of_email_template) in ref() method.

You can write as following :

    @api.multi
    def send_mail_template(self):
        template = self.env.ref('ailsaodoo.example_email_template')
        self.env['mail.template'].browse(template.id).send_mail(self.id)

Avatar
Discard
Author Best Answer

change email.py like below:

# -*- encoding: utf-8 -*-
from odoo import models, api
class res_partner(models.Model):
    _inherit = 'res.partner'
    @api.multi
    def send_mail_template(self):
        template= self.env['mail.template'].sudo().search([('name', '=', 'Example e-mail template')], limit=1)
        self.env['mail.template'].browse(template.id).send_mail(self.id)
Avatar
Discard