def test_sent_mail_multiple_bodies(self, _): with patch( 'edualert.notifications.utils.emails.EmailMultiAlternatives.send' ) as patched_send: send_mail( "Test subject", { 'text/plain': 'Test plain content', 'text/html': 'Test html content', }, '*****@*****.**', ["*****@*****.**"], ["*****@*****.**"]) self.assertEqual(1, patched_send.call_count) alternatives = SentEmailAlternative.objects.all() sent_at = timezone.datetime(2020, 8, 13).replace(tzinfo=utc) self.assertEqual(2, len(alternatives)) self.assert_sent_email_alternative( alternatives[0], from_addr='*****@*****.**', subject='Test subject', bcc='*****@*****.**', cc='*****@*****.**', mime_type='text/plain', content='Test plain content', sent_at=sent_at, ) self.assert_sent_email_alternative( alternatives[1], from_addr='*****@*****.**', subject='Test subject', bcc='*****@*****.**', cc='*****@*****.**', mime_type='text/html', content='Test html content', sent_at=sent_at, )
def format_and_send_school_situation_email(student_name, time_period, formatted_grades, unfounded_absences_count, school_name, parents): email_title = SCHOOL_SITUATION_EMAIL_TITLE.format(student_name, time_period) if formatted_grades == "": formatted_grades = "-" bodies = { 'text/html': get_template('message.html').render( context={ 'title': email_title, 'body': SCHOOL_SITUATION_EMAIL_BODY.format(student_name, time_period, formatted_grades, unfounded_absences_count), 'show_my_account': False, 'signature': SCHOOL_SITUATION_EMAIL_SIGNATURE.format(school_name) }), } send_mail(email_title, bodies, settings.SERVER_EMAIL, [parent.email for parent in parents])
def format_and_send_notification_task(subject, body, user_profile_ids, prohibit_sending_sms, show_my_account=True): from edualert.profiles.models import UserProfile mails_to_send = [] sms_to_send = [] bodies = { 'text/html': get_template('message.html').render( context={ 'title': subject, 'body': body, 'frontend_url': settings.FRONTEND_URL, 'show_my_account': show_my_account, 'signature': 'Echipa EduAlert' }), } for profile_id in user_profile_ids: profile = UserProfile.objects.filter(id=profile_id).first() if not profile: continue can_send_email = False if profile.email_notifications_enabled and profile.email: mails_to_send.append( [subject, bodies, settings.SERVER_EMAIL, [profile.email]]) can_send_email = True if not prohibit_sending_sms and not can_send_email and profile.sms_notifications_enabled and profile.phone_number: phone_number = profile.phone_number if profile.phone_number.startswith('+') or profile.phone_number.startswith('00') \ else '+4' + profile.phone_number sms_to_send.append((phone_number, body)) if len(mails_to_send) > 1: send_mass_mail(mails_to_send) elif len(mails_to_send) == 1: send_mail(*mails_to_send[0]) if sms_to_send: send_sms(sms_to_send)
def test_sent_mail_without_cc(self): with patch( 'edualert.notifications.utils.emails.EmailMultiAlternatives.send' ) as patched_send: send_mail("Test subject", {'text/plain': 'Test content'}, '*****@*****.**', ["*****@*****.**"]) self.assertEqual(1, patched_send.call_count) alternatives = SentEmailAlternative.objects.all() self.assertEqual(1, len(alternatives)) self.assert_sent_email_alternative( alternatives[0], from_addr='*****@*****.**', subject='Test subject', bcc='*****@*****.**', cc='', mime_type='text/plain', content='Test content', )
def send_reset_password_message(user_profile_id, link): from edualert.profiles.models import UserProfile profile = UserProfile.objects.filter(id=user_profile_id).first() if not profile: return if profile.email: bodies = { 'text/html': get_template('reset_password_message.html').render( context={'link': link}), } send_mail(RESET_PASSWORD_TITLE, bodies, settings.SERVER_EMAIL, [profile.email]) elif profile.phone_number: phone_number = profile.phone_number if profile.phone_number.startswith('+') or profile.phone_number.startswith('00') \ else '+4' + profile.phone_number text_message = RESET_PASSWORD_BODY_SMS.format(link) send_sms([(phone_number, text_message)])