针对状态更改的每个记录发送单独的邮件

时间:2019-03-01 12:13:26

标签: python python-2.7 odoo-10

我正在Odoo v10中工作。我创建了一个请假摘要页面(树视图),其中显示了请假请求列表。每个请假请求在数据库中都有其自己的电子邮件ID(即应用该请假请求的用户的电子邮件ID)。请参见下图。 因此,我希望每当我在此处更改记录状态时,都应将电子邮件发送到该记录的电子邮件地址。

例如,当我将名称列为“ werty”的记录的“状态”从“待批准”更改为“已批准”时,应向该记录所附的电子邮件地址发送一封电子邮件。

我只需要一个函数的代码,当我更改状态时可以触发该函数,并且该函数应获取该记录的电子邮件并以此发送电子邮件。 我已经编写了用于基于电子邮件发送电子邮件的代码。但这仅适用于虚拟的硬编码电子邮件。我需要该记录的电子邮件,我要更改其状态。

this is my leave request page

这是我的代码:

@api.onchange('state')
def test_server_action(self):

    fromaddr = "admin123@gmail.com"
    toaddr = "yyy.gmail.com"

    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "xyz")
    msg['Subject'] = "Leave Request Status"

    if (self.state == 'confirm'):
        body = "Your leave Request has been approved by Dept. Waiting for final approval from office."
        msg.attach(MIMEText(body, 'plain'))
        text = msg.as_string()
        server.sendmail(fromaddr, toaddr, text)

    if (self.state == 'forward'):
        body = "Your leave request is more than 15 days. It has been forwarded to higher authority for approval."
        msg.attach(MIMEText(body, 'plain'))
        text = msg.as_string()
        server.sendmail(fromaddr, toaddr, text)

    if (self.state == 'refuse'):
        body = "Your leave request has been refused. Contact your department."
        msg.attach(MIMEText(body, 'plain'))
        text = msg.as_string()
        server.sendmail(fromaddr, toaddr, text)

    if(self.state == 'pending'):
        body = "Your leave is in pending."
        msg.attach(MIMEText(body, 'plain'))
        text = msg.as_string()
        server.sendmail(fromaddr, toaddr, text)

    server.quit()

0 个答案:

没有答案