Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to send an email via python with the new API ?

By
Nicolas
on 5/29/15, 8:58 AM 1,486 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.

2

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 5/30/15, 1:10 PM

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

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

Lucas Huber
on 3/15/16, 12:15 PM

Thanks! Your answer got me out of a bind.

Thinkwell Designs, Dave Burkholder
on 7/20/16, 9:49 PM
0
Lucas Huber
On 3/15/16, 12:45 PM

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.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/29/15, 8:58 AM
Seen: 1486 times
Last updated: 3/15/16, 12:45 PM