2016-01-04 41 views
1

我嘗試使用下面的代碼來發送郵件功能:如何在以下功能中使用電子郵件模板?

def button_confirm_mom(self,cr,uid,ids,context=None): 
     sobj = self.browse(cr, uid, ids) 
     msg_pool = self.pool.get('mail.mail') 
     cc_text = '' 
     msg_vals = { 
         'subject' : "MoM has been created", 

         'email_from' : "[email protected]", 
         'reply_to' : False, 
         'state'  : 'outgoing', 
         'model'  : False, 
         'res_id'  : False, 
         'auto_delete' : False, 
      } 


     if sobj.matp: 
      for cc_obj in sobj.matp: 
       if cc_obj.empname.work_email: 
        cc_text += cc_obj.empname.work_email + ',' 

     if sobj.newa: 
      for cc_obj1 in sobj.newa: 
       if cc_obj1.empname.work_email: 
        cc_text += cc_obj1.empname.work_email + ',' 
      msg_vals['email_cc'] = cc_text 
     self.pool.get('mail.mail').create(cr,uid,msg_vals) 
     return True 

我想知道我可以使用模板和發送郵件多個人。任何人都有這個想法嗎?

回答

1

您可以使用這種方式。爲郵件添加新模板(如查看xml文件)。例如:

<?xml version="1.0"?> 
<openerp> 
    <data noupdate="1"> 
     <record id="event_YOUR_ID_mail_template" model="mail.template"> 
      <field name="name">Name of template</field> 
      <!-- for example model - res.users --> 
      <field name="model_id" ref="your_module.model_res_users"/> 
      <field name="email_from">[email protected]</field> 
      <field name="email_to" >${object.email|safe}</field> 
      <field name="lang"></field> 
      <field name="subject">Your subject</field> 
      <field name="auto_delete" eval="True"/> 
      <field name="body_html"><![CDATA[Message of mail. <p>You can use here ${object.name} or any fields of object,</p> ]]></field> 
     </record> 
    </data> 
</openerp> 

安裝\更新模塊後,您可以找到(編輯)的模板這裏:(頂部菜單)設置 - >(左菜單)電子郵件 - >模板(必須使用開發模式看到這個菜單項)。

我們如何利用這個模板在Python代碼:

temp = self.env.ref('your_module.event_YOUR_ID_mail_template') 
if temp: 
    # example: user - instance of res.users 
    temp.sudo().with_context().send_mail(user.id, force_send=True) 
+0

Danilla Ganchar,首先非常感謝你。爲什麼變量user.id存在於send_mail函數中? –

+0

@ShravyaShetty我不知道,但我記得在其他模塊每次使用** ID **的記錄。你可以檢查這個:'/ odoo/addons/rating/models/rating.py' line ** 120 **或'/ odoo/addons/website_forum/models/res_users.py' line ** 79 **。我使用版本9.無論如何,這是工作。我在模塊中使用了這個解決方案。希望這對你有所幫助。 –

+0

Danilla Ganchar,是它的工作 –