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

Email template in custom module - passing data

By
Dr Obx
on 1/13/16, 5:18 AM 1,578 views

Hi guys, how can I do it ?

My template contains:


<openerp>

<data noupdate="1">

<record id="stock_check_form" model="email.template">

<field name="name">Order form</field>

<field name="email_from">odoo@rs.com</field>

<field name="subject">Order</field>

<field name="model_id" ref="stock.check"/>

<field name="email_to" >rob@com.com</field>

<field name="partner_to"></field>

<field name="auto_delete" eval="False"/>

<field name="body_html">

<![CDATA[

<style>

span.oe_mail_footer_access {

display:block;

text-align:center;

color:grey;

}

</style>

<div style="border-radius: 2px; max-width: 800px; height: auto;margin-left: auto;margin-right: auto;background-color:#f9f9f9;">

<div style="height: auto;margin-left:12px;margin-top:30px;">

<p>U R G E N T !</p>

</div>

</div>

]]>

</field>

</record>

</data>

</openerp>

If I put data directly into template it look  like

tags only even if data in html_body field are formatted like that:

== email >>>>>=========================

[(0, 0, {'eelms': 132.0, 'eeqty_required': 90.0, 'eeproduct_id': 3, 'eemail_id': 39, 'eeproduct_qty': 85.0, 'eecurrent_stock': 0, 'eemwosres': 14}), (0, 0, {'eelms': 8.0, 'eeqty_required': 5.0, 'eeproduct_id': 18, 'eemail_id': 39, 'eeproduct_qty': 6.0, 'eecurrent_stock': 1, 'eemwosres': 1}), (0, 0, {'eelms': 14.0, 'eeqty_required': 8.0, 'eeproduct_id': 300, 'eemail_id': 39, 'eeproduct_qty': 13.0, 'eecurrent_stock': 1, 'eemwosres': 2}), (0, 0, {'eelms': 42.0, 'eeqty_required': 30.0, 'eeproduct_id': 909, 'eemail_id': 39, 'eeproduct_qty': 25.0, 'eecurrent_stock': 3, 'eemwosres': 4}), (0, 0, {'eelms': 39.0, 'eeqty_required': 27.0, 'eeproduct_id': 1214, 'eemail_id': 39, 'eeproduct_qty': 24.0, 'eecurrent_stock': 1, 'eemwosres': 4})]

== body >>>>>>=================================

[<tr><td><b>0</b></td><td>0</td><td>{""eelms"":132.0""eeqty_required"":90.0""eeproduct_id"":3""eemail_id"":39""eeproduct_qty"":85.0""eecurrent_stock"":0""eemwosres"":14}</td></tr><tr><td><b>0</b></td><td>0</td><td>{""eelms"":8.0""eeqty_required"":5.0""eeproduct_id"":18""eemail_id"":39""eeproduct_qty"":6.0""eecurrent_stock"":1""eemwosres"":1}</td></tr><tr><td><b>0</b></td><td>0</td><td>{""eelms"":14.0""eeqty_required"":8.0""eeproduct_id"":300""eemail_id"":39""eeproduct_qty"":13.0""eecurrent_stock"":1""eemwosres"":2}</td></tr><tr><td><b>0</b></td><td>0</td><td>{""eelms"":42.0""eeqty_required"":30.0""eeproduct_id"":909""eemail_id"":39""eeproduct_qty"":25.0""eecurrent_stock"":3""eemwosres"":4}</td></tr><tr><td><b>0</b></td><td>0</td><td>{""eelms"":39.0""eeqty_required"":27.0""eeproduct_id"":1214""eemail_id"":39""eeproduct_qty"":24.0""eecurrent_stock"":1""eemwosres"":4}</td></tr>]

Don't know how to ..... ;(

Why I'm getting this:  [<tr><td><b>0</b></td><td>0</td><td>{""eelms"":132.0""eeqty_required"":90.0""eeproduct_id"":3""eemail_id"":39""eeproduct_qty"":85.0""eecurrent_stock"":0""eemwosres"":14}

instead of:

  [<tr><td><b>eelms:132.0 </b></td><td>eeqty_required:90.0 </td><td>.....]

or

  [<tr><td><b>132.0 </b></td><td>90.0 </td><td>.....]  

?

1

Yenthe

--Yenthe--
6706
| 7 7 8
Turnhout, Belgium
--Yenthe--

My Odoo blog: http://www.odoo.yenthevg.com/

My Twitter: https://twitter.com/Yenthe666

Yenthe
On 1/13/16, 5:27 AM

Hi Dr Obx,

Create a new XML file and add it to the __openerp__.py file as dependency. An example of such a template:

<openerp>
<data noupdate="1">
<record id="your_template_email" model="mail.template">
<field name="name">Template title</field>
<field name="email_from">${object.event_id.user_id.email or ''}</field>
<field name="subject">${object.event_id.name} - Reminder</field>
<field name="model_id" ref="calendar.model_calendar_attendee"/>
<field name="email_to" >${('' if object.partner_id and object.partner_id.email and object.partner_id.email==object.email else object.email|safe)}</field>
<field name="partner_to">${object.partner_id and object.partner_id.email and object.partner_id.email==object.email and object.partner_id.id or False }</field>
<field name="auto_delete" eval="True"/>
<field name="body_html"><![CDATA[
<style>
span.oe_mail_footer_access {
display:block;
text-align:center;
color:grey;
}
</style>
<div style="border-radius: 2px; max-width: 1200px; height: auto;margin-left: auto;margin-right: auto;background-color:#f9f9f9;">
<div style="height: auto;margin-left:12px;margin-top:30px;">
<p>ANY TEXT YOU WANT HERE!</p>
</div>
</div>
]]>
</field>
</record>
</data>
</openerp>


As you can see you can add CSS etc in it. An important note is to add data noupdate="1" in it though! So the template is never reloaded.

Yenthe

Thank you Yanthe, If I can ask you one more question, how can I populate a table in email with data from the list I created ? For example: as a result of my mtehod I'm getting this:

email.append((0, 0, {'eproduct_id': rec[0],'eproduct_qty':rec[1],'emwosres':mwosres,'elms': lms, 'eqty_required':qtyreq,'ecurrent_stock': int(rec[2])}))
Inside this list is for example 5 products and I would like to put them in table straight into email .... how can I do it ?
Dr Obx
on 1/13/16, 6:59 AM

@Yenthe How to include the xml file in __init__.py? Normally in __openerp__.py with 'data' : ['xx.xml'],?

Mario Gielissen | Openworx
on 5/31/16, 6:21 AM

@Mario my bad, I did mean the __openerp__.py file, I corrected my answer. :)

Yenthe
on 6/2/16, 9:44 AM
1

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 1/14/16, 12:38 AM

Rob,

to loop throught the data which is not in current object, you can do that in python only, and send it to the template body...

you can get string of all the product details like this:

for lists in YOUR_DICTIONARY:

product_desc += """

<tr>

<td><b>%s</b></td>

<td> %s</td>

<td> %s</td>

.............[more_data]

</tr>

""" % (list['eproduct_id'], list['eproduct_qty'], list['emwosres']..........[more_data_reference])

then append it to the body
body = """

Hi,</br>

Please find Product details below:</br>

  <b><u>Product Details</u></b>: </br>

  <p>%s</p>

""" % (product_desc)

IF YOU DIRECTLY WANT TO SEND MAIL WITHOUT USING ANY TEMPLATE YOU CAN USE BELOW LOGIC

values = {}

values.update({'subject': 'Subject'})

values.update({'email_to': '[Email]'})

values.update({'body_html': body})

values.update({'body': body})

values.update({'res_id': ['CURRENT RECORD ID TO POST MESSAGE ON RECORD WALL']})

mail_mail_obj = self.pool.get('mail.mail')

msg_id = mail_mail_obj.create( cr, uid, values, context=context)

if msg_id:

mail_mail_obj.send(cr, uid, [msg_id], context=context)

OTHERWISE UPDATE THE BODY VALUE TO THE TEMPLATE AND SEND USING TEMPLATE ONLY

HOPE YOU GOT ME!!

I would prefer to use template, much easier to customize.

Dr Obx
on 1/14/16, 4:49 AM
0
Dr Obx
On 1/14/16, 4:45 AM

By any chance, can I send an internal Odoo message instead or notification ?


if you are having object in list[1], then use list[1].name, or if list[1] contains only product id then browse product master using this id and then get the product name before forming string...

Pawan
on 1/14/16, 7:07 AM

Sounds complicated :(

Dr Obx
on 1/14/16, 8:47 AM

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: 1/13/16, 5:18 AM
Seen: 1578 times
Last updated: 1/27/16, 9:15 AM