Ibrahim Rahimi
Ibrahim Rahimi

Reputation: 539

How to send an email template to multiple users (email addresses) in Odoo 12

I want to send an email to multiple users in Odoo 12 when a scheduler runs with custom data in email. I created my email template in the XML file. Also, I have a method which returns all my email addresses as a list. How can I loop over this list in email template in "email_to" field and get all email addresses and send the email to all of those emails?

Following is my email template:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">
        <record id="repayment_due_date_reminder_email_template" model="mail.template">
            <field name="name">Repayment Due Date Reminder Email Template</field>
            <field name="email_from">[email protected]</field>
            <field name="subject">Repayment Due Date Reminder</field>
            <field name="email_to">${for email in object.get_groups_usesr_email():
                                        }</field>
            <field name="lang">${object.lang}</field>
            <field name="model_id" ref="hr_payroll_advance_salary.model_loan_request"/>
            <field name="body_html">
                <![CDATA[ 
                <p>The following loan installment is due for repayment:<br/>
                Loan Recepient: ${(object.employee_id.name)}<br/>
                Total Loan Amount: ${(object.loan_amount)} ${(object.currency)} <br/>
                Installment Due: ${(object.loan_amount)} ${(object.currency)} <br/>
                Repayment Date: ${(object.repayment_schedule_ids.repayment_date)} <br/>
                You can view the loan record here for further details </p>
                ]]> 
            </field>
        </record>
    </data>
</odoo>

I call the email template using the below code:

template_rec = self.env.ref('hr_payroll_advance_salary.repayment_due_date_reminder_email_template')
template_rec.send_mail(loan.id, force_send=True)

And this is the method which returns emails:

@api.multi
    def get_groups_usesr_email(self):
        emails = []
        user_ids = self.get_users_from_groups('group_nl_finance_manager')
        emails = self.get_users_email('group_nl_finance_manager', user_ids)
        user_ids = self.get_users_from_groups('group_nl_ceo')
        new_emails = self.get_users_email('group_nl_ceo', user_ids)
        for email in new_emails:
            emails.append(email)
        return emails

I'm doing the above things in order to send an email to all users of two different groups and I am not sure if it's the appropriate way or not. Is it possible to do what I want to do or not or it can be done using another way?

Upvotes: 2

Views: 4397

Answers (2)

Ibrahim Rahimi
Ibrahim Rahimi

Reputation: 539

I sent the email template to all the email addresses using a loop on emails with below code and that works fine in my case.

all_eamils = loan.get_groups_usesr_email()
    for email in all_eamils:
        template_rec = self.env.ref('hr_payroll_advance_salary.repayment_due_date_reminder_email_template')
        template_rec.write({'email_to': email})
        template_rec.send_mail(loan.id, force_send=True)

Upvotes: 1

Saloua SAHNOUNE
Saloua SAHNOUNE

Reputation: 205

You can use the mass_mailing (Email Marketing By Odoo S.A) module to do that.

Upvotes: 2

Related Questions