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

前端 未结 2 2161
灰色年华
灰色年华 2021-02-09 07:00

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 ge

2条回答
  •  囚心锁ツ
    2021-02-09 07:16

    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:

    
    
        
            
                Auto-email when request is closed, not confirmed
                
                email
                ir.actions.server
                True
                object.requestor.email
                Your request object.name has been closed (not confirmed)
                
                            
            
        
    
    

    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:

        
            
            request_closed_nconf
            
            function
            close_nconf_request()
            True
        
    

    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:

    
    
        
            
                whatever.wkf
                whatever
                True
            
    
            
            
                
                True
                draft
                
                function
                draft()
            
            
                
                send
                function
                send()
            
            
                
                True
                close
                function
                close()
            
    
            
            
                
                
                draft
            
            
                
                
                close
            
        
    
    

    The line in the declaration of the activity calls the server action with id "send_automatic_email"

    And your action server:

    
    
        
            
                Send automatic email
                
                email
                ir.actions.server
                True
                object.requestor.email
                Your whatever: object.name has been created
                            
            
        
    
    

    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:

提交回复
热议问题