How to create email notification function in Odoo?

房东的猫 提交于 2019-12-24 01:44:48

问题


I have calender.event module. In that I have one date field ending_date. I wanted to create function when ending date exceeds current date it should send notification to all employees.

This function at calender.event inherited file

def send_birthday_email(self, cr, uid, ids=None, context=None):
        sobj = self.pool.get('calendar.event').browse(cr,uid,ids,context=context)
            ir_model_data = self.pool.get('ir.model.data')
            template_obj = self.pool.get('email.template')
            cc_text = ''
            if sobj.attendee_ids:
                    for cc_obj in sobj.attendee_ids:
                        if cc_obj.email:
                                cc_text += cc_obj.email + ','
                    for rec in sobj:
                            template_id = ir_model_data.get_object_reference(cr,uid,'calander_extended', 'calendar_notify')[1]
                    self.pool.get('email.template').write(cr,uid,template_id,{'email_to' : cc_text,})
                    self.pool.get('email.template').send_mail(cr,uid,template_id,rec.id,force_send=True,context=context)
                    return True 

This is email template written for it

<?xml version="1.0"?>
<openerp>
    <data>
        <record id="calendar_notify" model="email.template">
            <field name="name">Email Notification</field>
            <field name="email_from">${object.event_id.user_id.email or ''}</field>
            <field name="subject">${object.event_id.name}</field>
            <field name="model_id" ref="calander_extended.model_calendar_event"/>
        <field name="email_to" >${('' if object.partner_id and object.partner_id.email and object.partner_id.email==object.email else object.email|safe)}</field>
        <field name="partner_to">${object.partner_id and object.partner_id.email and object.partner_id.email==object.email and object.partner_id.id or False }</field>
            <field name="auto_delete" eval="True"/>

            <field name="body_html"><![CDATA[
                <html>
                    <head>
                        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
                        <title>${object.event_id.name}</title>
                        <style> 
                            span.oe_mail_footer_access {
                                display:block;    
                                text-align:center;
                                color:grey;                                
                            }
                        </style>
                    </head>
                    <body>
                        <div style="border-radius: 2px; max-width: 1200px; height: auto;margin-left: auto;margin-right: auto;background-color:#f9f9f9;">
                            <div style="height:auto;text-align: center;font-size : 30px;color: #8A89BA;">
                                <strong>${object.event_id.name}</strong>
                            </div>

                        </div>
                    </body>
                </html>
                ]]>
                </field>
        </record>
    </data>
</openerp>

When i invoke that function by clicking button then its not getting any error .but no getting mail also to attendees


回答1:


When I have to write a similar function, I wrote a condition checker and email sender function, and the cron call it regularly!
It works well!




回答2:


You can create a cron, as the calendar module does [1]:

<record forcecreate="True" id="ir_cron_scheduler_alarm" model="ir.cron">
    <field name="name">Run Event Reminder</field>
    <field eval="False" name="active" />
    <field name="user_id" ref="base.user_root" />
    <field name="interval_number">30</field>
    <field name="interval_type">minutes</field>
    <field name="numbercall">-1</field>
    <field eval="False" name="doall" />
    <field eval="'calendar.alarm_manager'" name="model" />
    <field eval="'get_next_mail'" name="function" />
    <!--<field eval="'(False,)'" name="args" />-->
</record>

In this case is calling the method get_next_mail on calendar.alarm_manager model. In your method you can do whatever you want. Also, this model looks like the right place where to put your code by extending it.

[1] https://github.com/OCA/OCB/blob/9.0/addons/calendar/calendar_cron.xml



来源:https://stackoverflow.com/questions/37107786/how-to-create-email-notification-function-in-odoo

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