跳至內容
選單
此問題已被標幟
2 回覆
996 瀏覽次數

I want an email to automatically appear in the chatter of a module after it was sent. I create the email by overriding 

def _send_mail(self, answer):

res = super()._send_mail(answer)

email_values = {

"auto_delete": False,

"model": "survey.survey",

"res_id": self.survey_id.id,

}


res.update(email_values)

return res

How can I achieve the desired result? After the email was sent, it doesn't appear in the chatter of the survey. Thanks in advance!

頭像
捨棄
最佳答案

Hi,

Please refer to the code:

def _send_mail(self, answer):

        # Call the original send_mail method

        res = super()._send_mail(answer)


        # Create a mail.message to appear in the chatter

        self.message_post(

            body="Email sent to the participant",

            subject="Survey email sent",

            message_type='email',  # can be 'comment' or 'email'

            subtype_id=self.env.ref('mail.mt_note').id,

            email_from=self.env.user.email,  # optional

        )


        return res

message_post automatically links the message to the record (self) in the chatter.


Hope it helps.

頭像
捨棄
最佳答案

Hii,

You're overriding _send_mail() and returning res, which may be an email template or dict of values, but you're not actually calling message_post, nor guaranteeing the email is sent with the right linkage to log in the chatter.

Here’s how to ensure the email appears in the chatter of the related survey.survey record:

from odoo import models class YourModel(models.Model): _inherit = 'your.model' def _send_mail(self, answer): res = super()._send_mail(answer) survey = self.survey_id # Prepare email values email_values = { 'auto_delete': False, 'model': 'survey.survey', 'res_id': survey.id, } # If res is a mail.template: if isinstance(res, dict): res.update(email_values) elif hasattr(res, 'write'): res.write(email_values) # Optional: Post a message to chatter manually (if not posted automatically) survey.message_post( body="An email was sent to the user.", message_type="notification", subtype_xmlid="mail.mt_note", ) return res

i hope it is use full

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
2
8月 24
1939
3
10月 18
7894
0
12月 16
4273
2
2月 25
3627
2
2月 25
1989