def test_send_mail_django(self): """ Ensures that the given arguments and settings trigger the default `django.core.mail`. """ email_args = ('subject', 'html_content', '*****@*****.**', ['*****@*****.**']) email_kwargs = {'from_name': None, 'attachments': None} send_mail(*email_args, **email_kwargs) self.assertEqual(len(mail.outbox), 1, "An email should have been sent") self.assertIn('*****@*****.**', mail.outbox[0].to) self.assertEqual('html_content', mail.outbox[0].body)
def transfer_attachments(job_application_id, attachments, destinations): for dst in destinations: if is_email(dst): send_mail( "job application {}".format(job_application_id), "", settings.JEPOSTULE_NO_REPLY, dst, attachments=attachments, ) else: for attachment in attachments: with open(os.path.join(dst, attachment.name), 'wb') as f: f.write(attachment.content)
def send_answer_to_candidate(job_application_id): job_application = models.JobApplication.objects.get(id=job_application_id) subject = get_subject(job_application) template, context = get_answer_message_template(job_application.answer) message = get_template(template).render(context) reply_to = None if hasattr(context['answer_details'], 'employer_email'): reply_to = context['answer_details'].employer_email message_id = send_mail( subject, message, job_application.platform_attribute('contact_email'), [job_application.candidate_email], reply_to=reply_to, monitoring_category=topics.SEND_ANSWER, ) event = job_application.events.create( name=models.JobApplicationEvent.ANSWERED, ) models.Email.objects.create( event=event, message_id=message_id[0], status=models.Email.SENT, )
def test_send_template_mail_mailjet(self): """ Ensures that the given arguments and settings trigger the right payload to the mailjet API. """ email_args = ('subject', 'html_content', '*****@*****.**', ['*****@*****.**']) email_kwargs = { 'mailjet_template_id': settings.MAILJET_TEMPLATES['SEND_APPLICATION_TO_EMPLOYER'], 'mailjet_template_data': { 'template_data': 'foo' }, } # Patch `requests.post` since it's used by mailjet_rest's Client. with mock.patch.object(MAILJET_CLIENT, "post_api") as mock_post_api: send_mail(*email_args, **email_kwargs) mock_post_api.assert_called_with({ "Messages": [{ "From": { "Email": "*****@*****.**", }, "To": [{ "Email": "*****@*****.**", }], "Subject": "subject", "Attachments": [], "TemplateID": email_kwargs['mailjet_template_id'], "TemplateLanguage": True, "Variables": email_kwargs['mailjet_template_data'], }] })
def test_send_mail_mailjet(self): """ Ensures that the given arguments and settings trigger the right payload to the mailjet API. """ email_args = ('subject', 'html_content', '*****@*****.**', ['*****@*****.**']) email_kwargs = {'from_name': None, 'attachments': None} # Patch `requests.post` since it's used by mailjet_rest's Client. with mock.patch.object(MAILJET_CLIENT, "post_api") as mock_post_api: send_mail(*email_args, **email_kwargs) mock_post_api.assert_called_with({ "Messages": [{ "From": { "Email": "*****@*****.**", }, "To": [{ "Email": "*****@*****.**", }], "Subject": "subject", "HTMLPart": "html_content", "Attachments": [], }] })
def send_confirmation_to_candidate(job_application_id): """ Args: job_application_id (int): id of a JobApplication entry attachments (list of Attachment objects) """ job_application = models.JobApplication.objects.get(id=job_application_id) from_email = job_application.platform_attribute('contact_email') message_id = send_mail( "Votre candidature a bien été envoyée", render_confirmation_email(job_application), from_email, [job_application.candidate_email], from_name=job_application.platform_attribute('name'), monitoring_category=topics.SEND_CONFIRMATION, ) event = job_application.events.create( name=models.JobApplicationEvent.CONFIRMED_TO_CANDIDATE, ) models.Email.objects.create( event=event, message_id=message_id[0], status=models.Email.SENT, )
def send_application_to_employer(job_application_id, attachments=None, send_confirmation=True): """ Args: job_application_id (int): id of a JobApplication entry attachments (list of Attachment objects) send_confirmation (bool) """ job_application = models.JobApplication.objects.get(id=job_application_id) topics.delay(ratelimits.Sender.delay(job_application.candidate_email)) from_email = job_application.platform_attribute('contact_email') # Actually send or not a real email to the employer depending on the platform # - lbb - yes (production and local dev) # - demo - yes (local dev - so that we can test the whole flow using our backdoor links) # - test - yes (production - so that we can test the whole flow using our backdoor links) # - lbb-staging - no (applications coming from LBB staging into production JePostule should not send emails to employers for real) # - id - yes (test environment) if job_application.client_platform.client_id in ['lbb-staging']: message_id = None elif job_application.client_platform.client_id in ['lbb', 'demo', 'test', 'id']: message_id = send_mail( f"Candidature spontanée - {job_application.job}", render_application_email(job_application), from_email, [job_application.employer_email], from_name=job_application.candidate_name, reply_to=job_application.candidate_email, attachments=attachments, mailjet_template_id=settings.MAILJET_TEMPLATES['SEND_APPLICATION_TO_EMPLOYER'], mailjet_template_data=job_application.get_email_template_data(), monitoring_category=topics.SEND_APPLICATION, )[0] else: raise ValueError('Unknown client_id.') event = job_application.events.create( name=models.JobApplicationEvent.SENT_TO_EMPLOYER, ) models.Email.objects.create( event=event, message_id=message_id, status=models.Email.SENT, ) ratelimits.Sender.add(job_application.candidate_email) forward_application_to_memo.run_async(job_application_id) # Actually forward or not the application to AMI API for real depending on the platform. # - lbb - yes (production and local dev) # - demo - no (local dev backdoor links) # - test - no (production backdoor links) # - lbb-staging - yes (applications coming from LBB staging into production JePostule) # - id - yes (test environment) if job_application.client_platform.client_id in ['demo', 'test']: pass elif job_application.client_platform.client_id in ['lbb', 'lbb-staging', 'id']: if job_application.candidate_peam_access_token: forward_application_to_ami.run_async(job_application_id) else: raise ValueError('Unknown client_id.') if send_confirmation: send_confirmation_to_candidate.run_async(job_application_id)