Skip to Content
Menu
This question has been flagged
3 Replies
9815 Views

Hello,


I'm trying to create a function to send an email but I can't find anywhere how to do it with the new API.

I created the template that should be sent already.


@api.one

def action_email(self):

     if not self.customer:

         raise except_orm("...", ...")

    customer_email = self.customer.email

     if not customer_email or len(customer_email.strip()) <= 0:

         raise except_orm("...", ...")

    ?????




Thanks.

Avatar
Discard
Best Answer

You inherit model mail.thread in your model

 _inherit = ['mail.thread'] 

and create mail by way like this (example from account_invoice.py)

    @api.multi
def action_invoice_sent(self):
""" Open a window to compose an email, with the edi invoice template
message loaded by default
"""
assert len(self) == 1, 'This option should only be used for a single id at a time.'
template = self.env.ref('account.email_template_edi_invoice', False)
compose_form = self.env.ref('mail.email_compose_message_wizard_form', False)
ctx = dict(
default_model='account.invoice',
default_res_id=self.id,
default_use_template=bool(template),
default_template_id=template.id,
default_composition_mode='comment',
mark_invoice_as_sent=True,
)
return {
'name': _('Compose Email'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(compose_form.id, 'form')],
'view_id': compose_form.id,
'target': 'new',
'context': ctx,
}

Avatar
Discard

In my case I had to remove "default_template_id=template.id," on Odoo 9? Should this be a external id?

Thanks! Your answer got me out of a bind.

Best Answer

It seems the model name email.template has changed to mail template in Odoo 9:

<!-- email_template for meetings -->
 <data noupdate="1">
 <record id="email_template_scrum_meeting" model="mail.template">
 <field name="name">Email Template for Meeting</field>
 <field name="email_from">${object.user_id_meeting or ''}</field>
 <field name="subject"> ${object.name or ''}</field>
 <field name="model_id" ref="project_scrum.model_project_scrum_meeting"/>
 <field name="auto_delete" eval="True"/>
 <field name="email_to">${object.user_id_meeting or ''}</field>
 <field name="body_html">
 <![CDATA[
 <div style="font-family: 'Lucica Grande', Ubuntu, Arial, Verdana, sans-serif;
 font-size: 16px; color: rgb(34, 34, 34); background-color: rgb(255, 255, 255); ">
 <p><p>Date: ${object.start_datetime}</p> <br/>
 <p> ${object.agenda}</p>
 <br/><br/>
 </div>
 ]]>
 </field>
 </record>
 </data>

There are some old template with model="email.template" around in the forum.

Avatar
Discard
Related Posts Replies Views Activity
2
Jul 19
4266
1
Aug 17
3312
8
Sep 21
17533
2
May 20
2406
5
Apr 18
9614