Skip to main content
 首页 » 编程设计

python之如何在以下功能中使用电子邮件模板

2025年05月04日28birdshome

我正在尝试使用以下代码发送邮件功能:

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 

我想知道如何使用模板并为多人发送邮件。有人对此有任何想法吗?

请您参考如下方法:

你可以这样使用。为邮件添加新模板(如查看 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">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>  

安装\更新您的模块后,您可以在此处找到(和编辑)模板:(顶部菜单)设置 -> (左侧菜单)电子邮件 -> 模板(您必须使用开发人员模式 以查看此菜单项)。

我们如何在 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)