2
votes

I'm working on an odoo project and I need to send emails automatically every day at 10 am. I have created the scheduled action but I've got a problem. This is the method I use to perform sending action

@api.model
def send_mails(self):
    domain = [['name', 'like', 'Production Report']]
    template = self.env['email.template'].search(domain)[0]
    template.send_mail(self.id, True)

After the execution of the method it gives this traceback


    2016-11-03 17:35:15,158 4912 ERROR sintramdb openerp.addons.base.ir.ir_cron: Call of self.pool.get('production.ouvrages').send_mails(cr, uid, *()) failed in Job 9
    Traceback (most recent call last):
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\openerp\addons\base\ir\ir_cron.py", line 138, in _callback
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\.\openerp\api.py", line 268, in wrapper
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\.\openerp\api.py", line 372, in old_api
      File "C:\Users\3D SKILLS\AppData\Local\OpenERP S.A.\Odoo\addons\8.0\production\models.py", line 347, in send_mails
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\.\openerp\api.py", line 266, in wrapper
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\.\openerp\api.py", line 549, in new_api
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\openerp\addons\email_template\email_template.py", line 558, in send_mail
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\.\openerp\api.py", line 268, in wrapper
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\openerp\addons\email_template\email_template.py", line 595, in generate_email
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\.\openerp\api.py", line 268, in wrapper
      File "C:\Program Files (x86)\Odoo 8.0-20160929\server\openerp\addons\email_template\email_template.py", line 484, in generate_email_batch
    AttributeError: 'bool' object has no attribute 'lang'

If i replace the @api.model by @api.one or @api.multi i get the following error


    TypeError: old_api() takes at least 4 arguments (3 given)

Note: With @api.one or @api.multi annotation it wokrs perfectly if i use a button click to call the method.

2
def send_mails(cr,uid,context=None): domain = [['name', 'like', 'Production Report']] template = self.pool.get('email.template').search(domain)[0] template.send_mail(cr,uid,[self.id], True,context=None)Phillip Stack
Try just using the old api. It might be the quickest solution. Although annoying.Phillip Stack
If you look at the file email_template.py however it looks for the lang attribute of the template which it thinks is a boolean in the above error. I would assume False. It is possible the template is not being passed to the method.Phillip Stack

2 Answers

2
votes

First of all thanks guys for your quick replies!!

After i tried your fixes i wasn't still able to solve the problem. So i decided to look deeply into the email_template.py file in email_template addon and after being stuck with some errors i finally got something working. Bellow is my solution (with the old api style of course)

    def send_mails(self, cr, uid, *args, **kwargs):
        domain = [['name', 'like', 'Production Report']]
        template_id = self.pool.get('email.template').search(cr, uid,domain,offset=0, limit=None, order=None, context=None, count=False)[0]
        template_obj = self.pool.get('email.template').browse(cr, uid, template_id)
        template_obj.send_mail(3, True, None)

I hope it will help someone else

1
votes

Try this

@api.v8
@api.model
def send_mails(self):
    domain = [['name', 'like', 'Production Report']]
    template = self.env['email.template'].search(domain)[0]
    template.send_mail(self.id, True)

if that doesn't work try this solution.

@api.model    
def send_mails(self):
    domain = [['name', 'like', 'Production Report']]
    template = self.env['email.template'].search(domain)[0]
    template.send_mail(self._cr,self._uid,self.id, True)