2016-07-26 44 views
0

我有以下代码。当调度程序运行时,我收到错误消息。有人帮我解决代码中的错误Odoo运行发送预定邮件的cron作业时丢失错误

MissingError 您试图访问的文件之一已被删除,请刷新后再试一次。

def send_followup_mail(self, cr, uid, context=None): 
     quot_ids=self.search(cr, uid, [('state','=','amend_quote')]) 
     for quot_id in quot_ids: 
      if quot_id: 
       quot_obj=self.browse(cr, uid, quot_id ,context=context) 
       quotation_since=quot_obj.quotation_since 
       for email_template_line in quot_obj.temp_tag_id.crm_campaign_id.email_template_ids: 
        if quotation_since==email_template_line.delay_days: 
         mail_pool = self.pool.get('mail.mail') 
         mail_id = self.pool.get('email.template').send_mail(cr, uid, email_template_line.id, 1, force_send=True, context=context) 

         if email_template_line.send_mail_to=='to_client': 
          mail_pool.write(cr, uid, mail_id, {'email_to':quot_obj.email_from}, context=context) 

         elif email_template_line.send_mail_to=='to_sale_rep': 
          mail_pool.write(cr, uid, mail_id, {'email_to':quot_obj.sale_rep_id.email}, context=context) 

         if mail_id: 
          mail_pool.send(cr, uid, mail_id, context=context) 
       self.write(cr, uid, quot_id,{'quotation_since':quotation_since+1}, context=None) 
     return True 
+0

这就是您从Odoo获得的所有错误消息吗?似乎你正在尝试使用那部分代码的一些记录,现在已经不在db了。 – CZoellner

+0

我正面对这一行的错误 - mail_id = self.pool.get('email.template')。send_mail(cr,uid,email_template_line.id,1,force_send = True,context = context) – sfx

+0

该参数' 1'告诉Odoo使用模板中定义的模型的ID为1的记录用于值表达式。似乎数据库中没有该特定模型的ID为1的记录。为什么该参数设置为1?它不应该是'quot_id'吗? – CZoellner

回答

0

这是修改过的代码并且工作正常

def send_followup_mail(self, cr, uid, context=None): 
      quot_ids=self.search(cr, uid, [('state','=','amend_quote')]) 
      for quot_id in quot_ids: 
       if quot_id: 
        quot_obj=self.browse(cr, uid, quot_id ,context=context) 
        quotation_since=quot_obj.quotation_since 
        for email_template_line in quot_obj.temp_tag_id.crm_campaign_id.email_template_ids: 
         if quotation_since==email_template_line.delay_days: 
          mail_pool = self.pool.get('mail.mail') 
          template_id = email_template_line.id 
          template_pool = self.pool.get('email.template') 
          sale_id=self.pool.get('sale.order').search(cr, uid, [], limit=1, context=context) 
          mail_id = template_pool.send_mail(cr, uid, template_id, sale_id[0], force_send=True, context=context) 

          if email_template_line.send_mail_to=='to_client': 
           mail_pool.write(cr, uid, mail_id, {'email_to':quot_obj.email_from}, context=context) 

          elif email_template_line.send_mail_to=='to_sale_rep': 
           mail_pool.write(cr, uid, mail_id, {'email_to':quot_obj.sale_rep_id.email}, context=context) 

          if mail_id: 
           mail_pool.send(cr, uid, mail_id, context=context) 
        self.write(cr, uid, quot_id,{'quotation_since':quotation_since+1}, context=None) 
      return True 
0

的使用新的API所有写代码的第一位。

为了得到模板使用obj = self.env.ref('template_record_id')然后发送obj.send_mail(model.obj.id, force_send=True),如果你想设置邮件再发送前obj.email_to = '[email protected]'

最终代码:

某处在XML:

<record id="email_template_record_id" model="email.template"> 
     <field name="name">Name</field> 
     <field name="email_from">[email protected]</field> 
     <field name="subject">Subject</field> 
     <field name="email_to">False</field> 
     <field name="auto_delete" eval="True"/> 
     <field name="model_id" ref="model_model_name"/> 
     <field name="body_html"> 
      <![CDATA[ 
      <html> 
       <head> 
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
        <title>Custom title</title> 
       </head> 
       <body> 
        <p>Dear Sir,</p> 
        <p>Text body.</p> 
        <p>This is automated mail, don't reply.</p> 
       </body> 
      </html> 
      ]]> 
     </field> 
    </record> 
在Python代码

template = self.env.ref('email_template_record_id') 
    template.email_to = some_obj.obj_related_field.email 
    try: 
     template.send_mail(ombject_id, force_send=True) 
    except Exception: 
     _logger.error("Custom Error Message") 
    template.email_to = False 
+0

我支持你使用新api的意图,但为什么你应该在每次调用时都设置template.email_to?这将改变数据库中的模板记录。 Odoo的电子邮件模板支持占位符/值表达式。该电子邮件应该从模板用于的记录中获取。 – CZoellner

+0

我现在只有这个工作解决方案。无论如何感谢您的更正 –

+0

@CZoellner我有问题,我的解决方案是立即发送电子邮件,并且send_email没有电子邮件的参数,它从模板中获取它,这就是为什么我要为其分配电子邮件。但在上面的例子中,它将邮件添加到池中,并且它的性能是否更好? –