Bhuwan Khadka
Bhuwan Khadka

Reputation: 51

Sending mail to multiple users of specific group, odoo12

I have an email template which is sent whenever a new record is created. This email is sent to only one person as I have specified only one email id in the email_to field.

I have also created a custom group for users only. I wanted to send an email notification to all the users of the users' group by checking if email id is present or not iteratively after the new record is created. How can I get email id of users from users' group in email_to field in email template and sent automatically?

Upvotes: 0

Views: 1901

Answers (3)

PIYUSH CHAUHAN
PIYUSH CHAUHAN

Reputation: 1

First create a field(change field name according to you):

student_mail=fields.Char(compute="get_email_to")

After than declare a function:

def get_email_to(self):
    mail_id= self.env['school.management'].search([])

    filter_mail = [i.email for i in mail_id if i.college_id]
    self.student_mail=",".join(filter_mail)
    result=",".join(filter_mail)
    self.student_mail=result.replace('"','')

And in XML:

<field name="email_to">${object.student_mail}</field>

In my case I filter student base on college id...

Upvotes: 0

Mihir Dabgar
Mihir Dabgar

Reputation: 311

in your mail template

<field name="email_to">${object.get_email_to()}</field>

python method like this

@api.model
def get_email_to(self):
    user_group = self.env.ref("res.group_res_user")
    email_list = [
        usr.partner_id.email for usr in user_group.users if usr.partner_id.email]
    return ",".join(email_list)

hope this is working well with your requirement..

Upvotes: 2

Mihir Dabgar
Mihir Dabgar

Reputation: 311

first you overwrite your model create method like this..

@api.model
def create(self, vals):
    res = super(ClassName, self).create(vals)
    template = self.env.ref('module_name.template_id')
    template.send_mail(res.id, force_send=True)
    return res

it will send the mail from create new record. You can define the to_email in mail template that you want from record..

Upvotes: 1

Related Questions