Search code examples
pythonodoo

How to send email to applicants in first stage; recruitment odoo 14


I wanna send emails to applicants in the first stage of recruitment in Odoo 14. How can go about it using python? Here's my code.


def job_applicant_acknowledgments(self):
    for rec in self:
        get_first_stage = self.env['hr.recruitment.stage'].search([
            '|',
            ('job_ids', '=', False),
            ('job_ids', '=', self.id)], order='sequence asc', limit=1)
        if self.partner_id and get_first_stage and not emai_sent:
                ctx = {}
                ctx['email_to'] = rec.partner_id.email
                # ctx['email_from'] = self.env.user.user_id.email
                # ctx['email_from'] = self.env.user.employee_id.work_email
                ctx['send_email'] = True
                ctx['partner_id'] = rec.partner_id.id
                template = self.env.ref(
                    'hr_recruitment_custom.job_applicant_acknowledgment')
                template.with_context(ctx).send_mail(
                    rec.id, force_send=True, raise_exception=False)


Solution

  • This is what I needed. This is the python code that sends an email to the applicant either when they apply for a position or if you create a position id recruitment, odoo.

     def job_applicant_acknowledgments(self):
            for rec in self:
                ctx = {}
                ctx['email_to'] = rec.partner_id.email
                ctx['email_from'] = self.env.user.user_id.email
                ctx['send_email'] = True
                ctx['partner_id'] = rec.partner_id.id
                template = self.env.ref('hr_recruitment_custom.job_applicant_acknowledgment')
                template.with_context(ctx).send_mail(
                    rec.id, force_send=True, raise_exception=False)
    
    

    The code I added:

        def job_applicant_acknowledgments_cron(self):
            partner_ids = self.env['hr.applicant'].search([('email_sent', '=', False)])
            for applicant in partner_ids:
                if applicant.email_sent is False:
                    applicant.job_applicant_acknowledgments()
                    applicant.email_sent = True