How to create a program to automatically send mail with openerp using python

How to create a program to automatically send mail with openerp using python?

I created the openerp module. I am trying to send mail to a client when a client ID is generated.

In the sale folder in sale.py. When you convert the output to a client, I want to send mail to the client. So at sale.py. I have added the following line of code.

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

I get email_Template_id from the email_template database.

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

I am completely confused. Is there a way to send mail automatically?

Thanks in advance.

+7
email openerp
source share
2 answers

You can use the server action for this purpose. You can create a server action in OpenERP by gaining access to the settings "Technical" "Actions" "Server actions or creating XML in your module.

I will give an example of the server action that I use to send an email to the user when the object reaches a certain status in the module that I am 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 a workflow. In your case, you can call it when your form is saved (state = draft, maybe?).

Thus, you must add a call to the server action in determining the activity of the workflow:

  <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!

------ Small editing of a more extended answer -----

Well, I will try to make a short almost functional example.

In your python file, if this is not the case, you need to add some states to work the workflow.

 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 need a definition of the workflow that will work on your object, this will be the contents 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 string <field name="action_id" ref="send_automatic_email"/> in the activity declaration triggers a server action with the identifier "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 three files (and some changes in it!) You can do what you want.

Do not forget that you need to restart the OpenERP server (in order to recompile your changes in the python files) and update the module for loading XML files!

Good luck

- Almost forgot!

In your xml file, you need to add these buttons in the form to trigger 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> 
+10
source share

If you want to send an HTML email, the only way I have found is to send an email template using a server action.

Once you have created the template, open its template identifier by looking at the template and seeing that the id parameter is in the URL.

Then create a new server action, set the object to match the template, and set the type in python code. Paste the following code:

self.pool.get('email.template').send_mail(cr, uid, <TEMPLATE_ID>, context['active_id'],True, context=context)

Replacing <TEMPLATE_ID> with the corresponding template identifier.

Just explaining some other fields

  • context['active_id'] gives the current line / account identifier / etc, which is used to create the template using
  • The fifth True value causes the email to be sent immediately, rather than being added to the queue. It would be better to leave it as False , but due to an error in version 7, it may lose email addresses when added to the queue.

You can look in addons / email_template / email_template.py and find the send_mail function for more information.

+1
source share

All Articles