Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
3 Replies
2946 Tampilan

Hi,

I have a custom module that sends Approved email to leave applicant.
The issue when I click Approve button, it sends the default and custom approved email to the applicant. 
Is there a way I can send only custom email template to the applicant using python code?

Somebody help!

Avatar
Buang
Penulis Jawaban Terbai

Thank you for your response. I forgot to mention that the issue is on odoo 12 leave management. I know how to crate and send custom emails. I the issue is when I click the approve button, the system sends both the original(default) and custom(new) approve email to the applicant. I only want to send the custom approved email without the default(disable or stop default from sending).

Avatar
Buang
Penulis

I solved it by adding my custom email template in def _track_subtype function using super and finally added return False. I don't know if it's the best approach but it worked for me.

Jawaban Terbai

Hi,

Refer the blog for creating a custom mail template and to send it through send_mail method.

https://www.cybrosys.com/blog/how-to-send-email-from-code-in-odoo-15

Regards

Avatar
Buang
Jawaban Terbai

Yes, you can send only the custom email template to the leave applicant by using the send_mail method from the mail.template model.

Here is an example of how you can do this in your module:

from odoo import api, models

class LeaveRequest(models.Model):
    _name = 'leave.request'

    @api.multi
    def approve_leave(self):
        for leave in self:
            # Send custom email template to leave applicant
            template = self.env.ref('your_module.your_custom_email_template_id')
            template.send_mail(leave.id, force_send=True)

            # Set leave status to Approved
            leave.write({'state': 'approved'})

In the above code, we are first getting the reference to the custom email template using its id, then we use the send_mail method of the mail.template model to send the email to the leave applicant.

After sending the email, we update the leave request status to "Approved".

You can adjust the code according to your needs and requirements.

I hope this helps.

Avatar
Buang