Help

9

how to send email in odoo12

Avatar
Abraham Qureshi

How to send email from odoo module using email template

1 Comment
Avatar
Discard
2 Answers
3
Best Answer

Hi,

The first thing that you need to do is add a dependency to other modules that you’ll need,


'depends': ['mail'],



The next step is to create a new XML record like given are below,


 <record id="email_template" model="mail.template">
<field name="name">EMAIL TEMPLATE</field>
<field name="model_id" ref="emai_template.model_example.mail"/>
<field name="auto_delete" eval="True"/>
<field name="email_from">${(object.res_user_id.email}</field>
<field name="email_to">${object.client_name.email}</field>
<field name="report_template" ref="action_example_pdf"/>
<field name="subject">${object.amc}</field>
<field name="body_html"><![CDATA[
<p>Dear ${(object.client_name.name)},<br/><br/>
Good job, you've just created your first e-mail template!<br/></p>
Regards,<br/>
${(object.company_id.name)} ]]></field>
</record>


Odoo e-mail templates come with jinja2 by default. This means that you can access any value on a record and fill it in on the e-mail automatically,



eg:-

<field name="subject">${object.amc}</field>


The next thing is we want to create python function like given below.


@api.multi
def action_send_amc(self):
self.ensure_one()
ir_model_data = self.env['ir.model.data']
try:
template_id = ir_model_data.get_object_reference('module_name', 'template_name')[1]
except ValueError:
template_id = False
try:
compose_form_id = ir_model_data.get_object_reference('mail', 'email_compose_message_wizard_form')[1]
except ValueError:
compose_form_id = False
ctx = {
'default_model': 'exmaple.email',
'default_res_id': self.ids[0],
'default_use_template': bool(template_id),
'default_template_id': template_id,
'default_composition_mode': 'comment',
'mark_so_as_sent': True,
'force_email': True
}
return {
'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,
}



Thanks

Avatar
Discard
1
Avatar
Hilar AK
Best Answer

Hi, I don't know what type of answer you need, Here I can give you the tutorials.

In technical part you have to the following steps ;

self.env['mail.templates'].browse(template_id).send_mail(mail_data)

Here template id can be browsed according to your mail type and you can use the send mail function from the same browsable object. The mail data is a Dict{} with email values including to, from, body...

self.env['mail.mail'].send(mail_data)

This method can be used to send mail without the template.

Go through the following links to set up the mail server in general settings. Here you can set the outgoing and incoming mail server data.


1. https://www.odoo.com/documentation/user/12.0/discuss/email_servers.html

2. https://www.odoo.yenthevg.com/configure-outgoing-mailservers/

3. https://webkul.com/blog/outgoing-mail-servers/

Avatar
Discard