Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Artificial Intelligence
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Property Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

Correct way to e-mail custom HTML record in function?

Subscribe

Get notified when there's activity on this post

This question has been flagged
emailemail_templatemail.wallodoo8.0
2 Replies
23031 Views
Avatar
Yenthe Van Ginneken (Mainframe Monkey)

Hi guys,

I've been trying to create a function to send out HTML e-mails but I always run stuck.
I first created a new model 'odoo.environment.email' which will be the older for my e-mail templates like this:

# -*- coding: utf-8 -*- from openerp import models, fields, api, _ from openerp.exceptions import Warning class odoo_environment_emails(models.Model): _name = 'odoo.environment.email' name = fields.Char('E-mail name', required=True) email = fields.Char('E-mailadress primary person', required=True) email_cc = fields.Char('Emailadress CC persons') email_title = fields.Char('E-mail title', required=True) message = fields.Html('Message', required=True)

I then created a custom template in XML which contains HTML content:

<openerp> <data noupdate="1"> <record id="odoo_email_installation" model="odoo.environment.email"> <field name="name">Title</field> <field name="email">example@example.com</field> <field name="email_cc">example2@example.com</field> <field name="email_title">New record(${object.odoo_username}) created!</field> <field name="message"><![CDATA[ <p>My custom text: ${object.name}</p> </field> </record> </data> </openerp>


On another model 'odoo.environment' I've created a Many2many where you can add all the e-mail templates you like, which are created on the model 'odoo.environment.email':

odoo_emails_to_send = fields.Many2many('odoo.environment.email', string='E-mails to send after installation', default=_get_default_emails)

Now I have a Python function that is triggered when the user clicks on a function. This function will loop over all items added in the Many2many and every e-mail template added should throw out an own e-mail. What I have now:

def send_required_mails(self, cr, uid, ids, record, context=None): # Get e-mail server ir_mail_server = self.pool.get('ir.mail_server') # Get all selected e-mail templates email_template_ids = record.odoo_emails_to_send # Send out one template at a time for email_id in email_template_ids: email_template = self.pool.get('odoo.environment.email').browse(cr, uid, email_id.id) title = email_template.email_title message = email_template.message msg = ir_mail_server.build_email('from@email.com', [email_template.email], title, message, [email_template.email_cc or ''], [], 'reply_to@email.com') ir_mail_server.send_email(cr, uid, msg


This works pretty nice but I have 1 problem: the HTML is not rendered and all ${object.variable} are not filled / converted. I've read some posts and it seems to be that I need to send the e-mail out with generate_email, so I tried:

def send_required_mails(self, cr, uid, ids, record, context=None): # Send out one template at a time for email_id in email_template_ids: template_id = self.pool.get('ir.model.data').get_object(cr, uid, 'auto_odoo_installer', 'odoo_email_installation').id template_ids = self.pool.get('odoo.environment.email').browse(cr, uid, template_id) email_template_obj = self.pool.get('email.template') if template_ids: values = email_template_obj.generate_email(cr, uid, template_ids[0], ids, context=context) values['subject'] = subject values['email_to'] = email_to values['body_html'] = body_html values['body'] = body_html values['res_id'] = False 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) return True

However this keeps throwing up errors. For example:

File "/odoo/odoo-server/openerp/api.py", line 256, in wrapper return old_api(self, *args, **kwargs) File "/odoo/odoo-server/addons/email_template/email_template.py", line 219, in get_email_template_batch results = dict.fromkeys(res_ids, False) TypeError: unhashable type: 'list


So, how should I correctly send out these e-mails and get the HTML and ${object.variable} converted to plain text in the e-mail?

Thanks,
Yenthe

2
Avatar
Discard
Dr Obx

Got the same problem. Will follow this subject

Avatar
Axel Mendoza
Best Answer

@Yenthe

Take this code of one of our modules as example, The code in bold is like do it all for me with just the object and the context vars:

def send_notifications(self, cr, uid, ids, context=None):

template_pool = self.pool.get('email.template')

template = self.pool.get('ir.model.data').get_object(cr, uid, 'solt_maintenance_preventive', 'solt_maintenance_remind_email')

assert template._name == 'email.template'

if context == None:

context = {}

res_groups_pool = self.pool.get('res.groups')

emails_send = []

group_id = self.pool.get('ir.model.data').get_object_reference(cr, SUPERUSER_ID,

'solt_maintenance', 'group_maintenance_planner')

if group_id:

for users_obj in res_groups_pool.browse(cr,uid,group_id[1],context=context).users:

if users_obj.email:

emails_send.append(users_obj.email)

context['email_send'] = ",".join(emails_send)

for sched in ids:

msg_id = template_pool.send_mail(cr, uid, template.id, sched, True, context=context)

The template used is here:

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

<field name="name">Maintenance Remind Email</field>

<field name="model_id" ref="model_solt_maintenance_schedule" />

<field name="email_from"><![CDATA[${object.object_id.company_id.name} <${object.object_id.company_id.email}>]]></field>

<field name="email_to">${ctx.email_send}</field>

<field name="subject">[Recordatorio] Intervención de mantenimiento para:

${object.object_id.name}</field>

<field name="body_html"><![CDATA[

<p>Se ha determinado que al objetivo ${object.object_id.name} se le debe proporcionar mantenimiento de acuerdo a su programacion.</p>

]]></field>

</record>

2
Avatar
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
I Can't edit my email template header and footer ? Solved
email email_template
Avatar
Avatar
Avatar
Avatar
3
Dec 24
7333
emai_from in Email Template Ignored
email email_template
Avatar
0
Oct 15
5200
Emailing Invoices don't go to or by the right person
email invoicing email_template
Avatar
0
Sep 25
1459
How to indicate the email "to" is my custom model field?
email email_template Email
Avatar
0
Jul 24
2714
Why does the email template editor no longer save raw HTML in Odoo 15?
email html email_template
Avatar
Avatar
Avatar
2
Apr 23
4518
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk Slovenščina Español (América Latina) Español Svenska ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now