Odoo Email Templates
Today we are going to discuss about how to send the emails in Odoo. For sending an email we need to create an email template which we are going to be delivered while sending an email. There are two ways to create an email template. One way is creating template using XML and another is creating email template using the Python code. we are going to discuss both the ways.
Creating the template using XML.
An email template contains the following fields.
1. name = name of the email template
2 . email_from = sender sender of the email
3. subject = subject of the email.
4. email_to = comma separated address of the receivers
5.model_id = comma separated id of the partners
6.auto_delete = permanently delete the mail after sending it.
7.lang = Optional translation language(ISO code) to select when sending an email.
8.body_html = message to be sent to via email.
9.report_name = Name of the generated report file.
10.report_template = Report template if sent in attachment etc.
Here is an example.
<record id="email_template_edi_invoice" model="mail.template">
<field name="name">Invoice - Send by Email</field>
<field name="email_from">${(object.user_id.email and '%s <%s>' % (object.user_id.name, object.user_id.email) or '')|safe}</field>
<field name="subject">${object.company_id.name} Invoice (Ref ${object.number or 'n/a'})</field>
<field name="partner_to">${object.partner_id.id}</field>
<field name="model_id" ref="account.model_account_invoice"/>
<field name="auto_delete" eval="True"/>
<field name="report_template" ref="account_invoices"/>
<field name="report_name">Invoice_${(object.number or '').replace('/','_')}_${object.state == 'draft' and 'draft' or ''}</field>
<field name="lang">${object.partner_id.lang}</field>
<field name="body_html"><![CDATA[
Message you need to deliver
]]></field>
</record>
Creating a template from python Code.
Create a simple template using odoo orm create method in email.template or create a simple template using XML . After a template is created we can add or modify the fields in the template using the python code as follows.
def send_email(self,cr,uid,ids,context=None):
template_obj = self.pool.get('mail.template')
ir_model_data = self.pool.get('ir.model.data')
# Create a template id by either of the ways.
#template_id = template_obj.create(cr, uid,{'name':'Template Name','model_id':'Your model id'})
template_id= ir_model_data.get_object_reference(cr, uid, 'module_name', 'xml_template_id')[1]
if template_id:
#------------ if a template id is created -------------------
values = template_obj.generate_email(cr, uid, template_id, ids[0], context=context)
# Set/Modify the values for the template.
values['subject'] = subject you want to show
values['email_to'] = receiver of the email
values['partner_to'] = partner ids
values['body'] = body_html
.....
.....
#--------------------------------------------------------------
#----------------if template id is not created-----------------
values = {
'subject': 'subect ',
'body_html': 'Message to be sent',
'email_to': receiver email,
'email_from': 'sender_email',
values['partner_to'] : partner ids
......
......
}
#---------------------------------------------------------------
mail_obj = self.pool.get('mail.mail')
msg_id = mail_obj.create(cr, uid, values, context=context)
if msg_id:
mail_obj.send(cr, uid, [msg_id], context=context)
return True