如何创建使用python从openerp自动发送邮件的程序 [英] How to create program for sending mail automatically from openerp using python

查看:227
本文介绍了如何创建使用python从openerp自动发送邮件的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python自动从openerp发送邮件程序?



我已经创建了openerp模块。当客户端id生成时,我正在尝试向客户端发送邮件。



在sale.py中的销售文件夹。当转换成客户端时,我想发送邮件给客户端。所以在sale.py我添加了以下代码行。

  self.pool.get('email.template')。send_mail(cr,uid ,email_template_id,object_id,False,context = context)

我正在从email_template数据库获取email_Template_id。



解释什么是email_template_id,object_id?
什么是模板?
什么是消息?



我很困惑。
有什么其他方式可以自动发送邮件吗?



提前感谢

解决方案

您可以为此目的使用服务器操作。您可以通过访问设置»»技术»»操作»»服务器操作或在模块上创建XML来在OpenERP中创建服务器操作。



我将在这里留下一个服务器操作的示例,当用户在正在开发的模块上达到某个状态时,用户向用户发送电子邮件:

 <?xml version =1.0encoding =UTF-8?> 
< openerp>
< data>
< record id =ir_actions_server_send_email_when_closed_nconfmodel =ir.actions.server>
< field name =name>关闭请求时的自动电子邮件,未确认< / field>
< field name =model_idref =model_generic_request/>
< field name =state> email< / field>
< field name =type> ir.actions.server< / field>
< field name =condition> True< / field>
< field name =email> object.requestor.email< / field>
< field name =subject>您的请求object.name已关闭(未确认)< / field>
< field name =message><![CDATA [
这是一个自动的电子邮件。请勿回复。

您好,

我们在这里通知您有关于[[object.request_date]]上提交的请求[[object.name]]和以下数据:

|请求 - 详细
| =======================
|数字:[[object.id]]
| =======================
|负责人:[[object.responsible_name.name]]
|请求描述:[[object.request_description]]
|说明原因:[[object.stating_reasons]]
| =======================
|注意:[[object.notes]]


尚未确认并关闭。

如果您有任何问题,请不要犹豫与您的主管联系。

谢谢!]]>
< / field>
< / record>
< / data>
< / openerp>

此操作是从工作流程中调用的。在您的情况下,您可以在保存表单时使用它(state = draft,也许?)。



所以你必须在你的工作流活动定义:

 < record model =workflow.activityid =act_closed_nconf> 
< field name =wkf_idref =wkf_request/>
< field name =name> request_closed_nconf< / field>
< field name =action_idref =ir_actions_server_send_email_when_closed_nconf/>
< field name =kind> function< / field>
< field name =action> close_nconf_request()< / field>
< field name =flow_stop> True< / field>
< / record>

希望这有帮助!



----一个小的编辑到一个更广泛的答案-----



好的,我会尽量使一个简短的几乎功能例如



在您的python文件中,如果还没有,您将不得不添加一些状态,以使工作流工作。

  class whatever(osv.osv):
_name ='whatever'
_description ='whatever'
_columns = {
'name':fields.char('whatever',size = 64,required = True),
'state':fields.selection([('draft','Draft'),
('sent','Sent'),
('closed','Closed'),
],
'Status',readonly = True,track_visibility ='onchange',
),
(...这里的其他一些字段)
}
_defaults = {
'state':'draft',
}

#these 3个功能是c通过工作流
def draft(self,cr,uid,id,context = None):
self.write(cr,uid,ids,{'state':'draft'})
$ True

def send(self,cr,uid,ids,context = None)
self.write(cr,uid,ids,{'state'
return True

def close(self,cr,uid,ids,context = None):
self.write(cr,uid,ids,{'state' :'closed'})
return True
whatever()

然后,您将必须有一个工作流定义可以在您的对象上工作,这将是您的xml的内容:

  ?xml version =1.0?> 
< openerp>
< data>
< record model =workflowid =wkf_whatever>
< field name =name> whatever.wkf< / field>
< field name =osv> whatever< / field>
< field name =on_create> True< / field>
< / record>

<! - 活动 - >
< record model =workflow.activityid =act_draft>
< field name =wkf_idref =wkf_whatever/>
< field name =flow_start> True< / field>
< field name =name> draft< / field>
< field name =action_idref =send_automatic_email/>
< field name =kind> function< / field>
< field name =action> draft()< / field>
< / record>
< record model =workflow.activityid =act_send>
< field name =wkf_idref =wkf_whatever/>
< field name =name> send< / field>
< field name =kind> function< / field>
< field name =action> send()< / field>
< / record>
< record model =workflow.activityid =act_close>
< field name =wkf_idref =wkf_whatever/>
< field name =flow_stop> True< / field>
< field name =name> close< / field>
< field name =kind> function< / field>
< field name =action> close()< / field>
< / record>

<! - transitions - >
< record model =workflow.transitionid =whatever_t1>
< field name =act_fromref =act_draft/>
< field name =act_toref =act_send/>
< field name =signal> draft< / field>
< / record>
< record model =workflow.transitionid =whatever_t2>
< field name =act_fromref =act_send/>
< field name =act_toref =act_close/>
< field name =signal> close< / field>
< / record>
< / data>
< / openerp>

< field name =action_idref =send_automatic_email /> 在活动声明中调用ID为send_automatic_email的服务器动作



而您的操作服务器:

 <?xml version =1.0encoding =UTF-8?> 
< openerp>
< data>
< record id =send_automatic_emailmodel =ir.actions.server>
< field name =name>发送自动电子邮件< / field>
< field name =model_idref =model_whatever/>
< field name =state> email< / field>
< field name =type> ir.actions.server< / field>
< field name =condition> True< / field>
< field name =email> object.requestor.email< / field>
< field name =subject>您的任何:object.name已创建< / field>
< field name =message><![CDATA [
这是一个自动的电子邮件。请勿回复。

你好,

bla bla bla bla

在这里你会写任何你想要的,并且可以访问数据库中存储的数据,例如[[object.name]]来访问字段name

< / field>
< / record>
< / data>
< / openerp>

使用这3个文件(和其中的一些变化),您应该能够做所需不要忘记,您必须重新启动OpenERP服务器(为了重新编译您的python文件中的更改),并更新您的模块以加载XML文件!



祝你好运!



-
几乎忘了!



在您的xml视图文件中,您必须在表单视图中添加这些按钮以调用工作流操作:

 <报头GT; 
< button name =sendclass =oe_highlightstring =Sendtype =workflowstates =draft/>
< button name =closeclass =oe_highlightstring =Closetype =workflowstates =sent/>
< field name =statewidget =statusbarstatusbar_visible =draft,sent,closed/>
< / header>


How to create program for sending mail automatically from openerp using python?

I have created openerp module. I am trying to send mail to a client, when client id is generated.

In sale folder in sale.py. When convert lead into client, I want to send mail to client. So in sale.py. I have added following line of code.

self.pool.get('email.template').send_mail(cr, uid, email_template_id, object_id,False, context=context)

I am getting email_Template_id from email_template database.

Explain what is email_template_id, object_id? What is template? What is message?

I am totally confused. Is there any other way to send mail automatically?

Thanks in advance.

解决方案

You can use a server action for that purpose. You can create a server action in OpenERP by accessing Settings »» Technical »» Actions »» Server Actions or creating a XML on your module.

I'll leave here an example of a server action I'm using to send an email to user when an object reaches a certain status on a module I'm developing:

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
        <record id="ir_actions_server_send_email_when_closed_nconf" model="ir.actions.server">
            <field name="name">Auto-email when request is closed, not confirmed</field>
            <field name="model_id" ref="model_generic_request"/>
            <field name="state">email</field>
            <field name="type">ir.actions.server</field>
            <field name="condition">True</field>
            <field name="email">object.requestor.email</field>
            <field name="subject">Your request object.name has been closed (not confirmed)</field>
            <field name="message"><![CDATA[
THIS IS AN AUTOMATED EMAIL. DO NOT REPLY.

Hello,

We are here to inform you that the request [[object.name]] you submitted on [[object.request_date]] with the following data:

        | Request - Details
        |=========================
        | Number: [[object.id]]
        |=========================
        | Responsible Person: [[object.responsible_name.name]]
        | Request description: [[object.request_description]]
        | Stating reasons: [[object.stating_reasons]]
        |=========================
        | Notes: [[object.notes]]


Has not been confirmed and is closed.

If you have any question, do not hesitate to contact your supervisor.

Thank you!]]>
            </field>            
        </record>
    </data>
</openerp>

This action is called from workflow. In your case, you can call it when your form is saved (state = draft, maybe?).

So you have to add the call to the server action in your workflow activity definition:

    <record model="workflow.activity" id="act_closed_nconf">
        <field name="wkf_id" ref="wkf_request" />
        <field name="name">request_closed_nconf</field>
        <field name="action_id" ref="ir_actions_server_send_email_when_closed_nconf"/>
        <field name="kind">function</field>
        <field name="action">close_nconf_request()</field>
        <field name="flow_stop">True</field>
    </record>

Hope this helps!

------ A little edit to a more extended answer -----

Ok, I'll try to make a short almost functional example.

In your python file if not already, you'll have to add some states in order to put the workflow working.

class whatever(osv.osv):
    _name='whatever'
    _description='whatever'
    _columns={
        'name': fields.char('whatever', size=64, required=True),
        'state': fields.selection([('draft','Draft'),
            ('sent','Sent'),
            ('closed','Closed'),
            ],
            'Status', readonly=True, track_visibility='onchange',
        ),
        (... some other fields in here...)
    }
    _defaults={
        'state': 'draft',
    }

    #these 3 functions are called by the workflow
    def draft(self, cr, uid, ids, context=None):
        self.write(cr, uid, ids, {'state': 'draft'})
        return True

    def send(self, cr, uid, ids, context=None):
        self.write(cr, uid, ids, {'state': 'sent'})
        return True

    def close(self, cr, uid, ids, context=None):
        self.write(cr, uid, ids, {'state': 'closed'})
        return True    
whatever()

Then, you will have to have a workflow definition that will work on your object, this will be the content of your xml:

<?xml version="1.0"?>
<openerp>
    <data>
        <record model="workflow" id="wkf_whatever">
            <field name="name">whatever.wkf</field>
            <field name="osv">whatever</field>
            <field name="on_create">True</field>
        </record>

        <!-- activities -->
        <record model="workflow.activity" id="act_draft">
            <field name="wkf_id" ref="wkf_whatever" />
            <field name="flow_start">True</field>
            <field name="name">draft</field>
            <field name="action_id" ref="send_automatic_email"/>
            <field name="kind">function</field>
            <field name="action">draft()</field>
        </record>
        <record model="workflow.activity" id="act_send">
            <field name="wkf_id" ref="wkf_whatever" />
            <field name="name">send</field>
            <field name="kind">function</field>
            <field name="action">send()</field>
        </record>
        <record model="workflow.activity" id="act_close">
            <field name="wkf_id" ref="wkf_whatever" />
            <field name="flow_stop">True</field>
            <field name="name">close</field>
            <field name="kind">function</field>
            <field name="action">close()</field>
        </record>

        <!-- transitions -->
        <record model="workflow.transition" id="whatever_t1">
            <field name="act_from" ref="act_draft" />
            <field name="act_to" ref="act_send" />
            <field name="signal">draft</field>
        </record>
        <record model="workflow.transition" id="whatever_t2">
            <field name="act_from" ref="act_send" />
            <field name="act_to" ref="act_close" />
            <field name="signal">close</field>
        </record>
    </data>
</openerp>

The line <field name="action_id" ref="send_automatic_email"/> in the declaration of the activity calls the server action with id "send_automatic_email"

And your action server:

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
        <record id="send_automatic_email" model="ir.actions.server">
            <field name="name">Send automatic email</field>
            <field name="model_id" ref="model_whatever"/>
            <field name="state">email</field>
            <field name="type">ir.actions.server</field>
            <field name="condition">True</field>
            <field name="email">object.requestor.email</field>
            <field name="subject">Your whatever: object.name has been created</field>
            <field name="message"><![CDATA[
THIS IS AN AUTOMATED EMAIL. DO NOT REPLY.

Hello,

bla bla bla bla

In here you will write whatever you want, and can access to data stored in your database with, for example [[object.name]] to access the field "name"

            </field>            
        </record>
    </data>
</openerp>

With these 3 files (and some changes in it!) you should be able to do what you want.

Don't forget that, you have to restart OpenERP server (in order to recompile your changes in python files) and to update your module to load the XML files!

Good luck!

-- Almost forgot!

In your xml view file you have to add these buttons in your form view to call the workflow actions:

<header>
    <button name="send" class="oe_highlight" string="Send" type="workflow" states="draft"/>
    <button name="close" class="oe_highlight" string="Close" type="workflow" states="sent"/>
    <field name="state" widget="statusbar" statusbar_visible="draft,sent,closed" />
</header>

这篇关于如何创建使用python从openerp自动发送邮件的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆