how can I use a email template in the following function?

元气小坏坏 提交于 2019-12-14 02:05:18

问题


I am trying to send mail function using the following code:

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' : "abc@gmail.com",
                      '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

I wanted to know how i can use a template and send the mail for multiple people. Anyone has any idea on this ?


回答1:


You can use this way. Add new template for mail(like view xml-file). Example:

<?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">test@gmail.com</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> 

After installing\updating your module you can find(and edit) template here: (top menu)Settings -> (left menu) email -> template(you must use developer mode to see this menu item).

How we can use this template in python code:

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)


来源:https://stackoverflow.com/questions/34585222/how-can-i-use-a-email-template-in-the-following-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!