def send_connection_email(user_email, body_template='emails/login_token.txt'): """Send a login email to the user. The email contains a token that can be used once to login. We use the default django token generator, that is usually used for password resets. """ try: user = User.objects.get(email=user_email) except User.DoesNotExist: # In case we could not find any valid user with the given email # we don't raise any exception, because we can't give any hints # about whether or not any particular email has an account # on our site. return user_uid = urlsafe_base64_encode(force_bytes(user.pk)) login_token = default_token_generator.make_token(user) login_url = reverse('token_login', args=[user_uid, login_token]) base_url = get_base_url() full_login_url = '{base_url}{url}'.format(base_url=base_url, url=login_url) login_email_body = render_to_string( body_template, { 'base_url': base_url, 'user_name': user.full_name, 'full_login_url': full_login_url }) send_email(subject=LOGIN_SUBJECT, body=login_email_body, recipient_list=[user.email], from_email=settings.DEFAULT_FROM_EMAIL, tags=['connexion', settings.ENV_NAME], fail_silently=False)
def send_alert_confirmation_email(user_email, alert_token): """Send an alert confirmation link to the user. The email contains a token that can be used to validate the email ownership. """ try: alert = Alert.objects.get(token=alert_token) except (Alert.DoesNotExist): # In case we could not find any valid user with the given email # we don't raise any exception, because we can't give any hints # about whether or not any particular email has an account # on our site. return # Use the search form to parse the search querydict and # extract the perimeter querydict = QueryDict(alert.querystring) search_form = AidSearchForm(querydict) if search_form.is_valid(): perimeter = search_form.cleaned_data.get('perimeter', None) else: perimeter = None base_url = get_base_url() alert_validation_link = reverse('alert_validate_view', args=[alert_token]) alert_date = '{:%d/%m/%Y %H:%M:%S}'.format(alert.date_created) subject = _('Please confirm your Aides-territoires alert (%(date)s)') % { 'date': alert_date } if alert.alert_frequency == Alert.FREQUENCIES.daily: frequency = _( 'You will receive a daily email whenever new matching aids will be published.' ) # noqa else: frequency = _( 'You will receive a weekly email whenever new matching aids will be published.' ) # noqa text_body = render_to_string( TEMPLATE, { 'base_url': base_url, 'alert': alert, 'frequency': frequency, 'perimeter': perimeter, 'alert_validation_link': '{}{}'.format(base_url, alert_validation_link) }) send_email(subject=subject, body=text_body, recipient_list=[user_email], from_email=settings.DEFAULT_FROM_EMAIL, tags=['alerte_confirmation', settings.ENV_NAME], fail_silently=False)
def send_contact_form_email(form_dict, body_template='emails/contact_form.txt'): # noqa """Send the contact form content to the AT team.""" subject_display = dict(ContactForm.SUBJECT_CHOICES).get(form_dict['subject']) # noqa contact_form_email_body = render_to_string(body_template, { 'form_dict': form_dict, 'subject_display': subject_display }) send_email( subject=CONTACT_SUBJECT_PREFIX + subject_display, body=contact_form_email_body, recipient_list=[settings.DEFAULT_FROM_EMAIL], from_email=settings.DEFAULT_FROM_EMAIL, reply_to=[form_dict['email']], tags=['contact', form_dict['subject'], settings.ENV_NAME], fail_silently=False)
def send_alert(self, alert, new_aids): """Send an email alert with a summary of the newly published aids.""" site = Site.objects.get_current() domain = site.domain domain_with_subdomain = build_host_with_subdomain( site.domain, alert.source) in_minisite = is_subdomain(alert.source) # alert_url is different if on a minisite or not alert_url = alert.get_absolute_url(in_minisite=in_minisite) # delete_url always redirects to the main site delete_url = reverse('alert_delete_view', args=[alert.token]) email_context = { 'domain_with_subdomain': domain_with_subdomain, 'nb_aids': len(new_aids), 'new_aids': new_aids[:3], 'alert_title': alert.title, 'alert_url': f'https://{domain_with_subdomain}{alert_url}', 'alert_email_feedback_form_url': settings.ALERT_EMAIL_FEEDBACK_FORM_URL, 'alert_delete_url': f'https://{domain}{delete_url}', } text_body = render_to_string('alerts/alert_body.txt', email_context) html_body = render_to_string('alerts/alert_body.html', email_context) email_subject_prefix = settings.EMAIL_SUBJECT_PREFIX if alert.title == settings.ADDNA_ALERT_TITLE: email_subject_prefix = settings.ADDNA_ALERT_EMAIL_SUBJECT_PREFIX email_subject = '{}{:%d/%m/%Y} — De nouvelles aides correspondent à vos recherches'.format( email_subject_prefix, timezone.now()) email_from = settings.DEFAULT_FROM_EMAIL email_to = [alert.email] try: send_email(subject=email_subject, body=text_body, recipient_list=email_to, from_email=email_from, html_body=html_body, tags=['alerte', alert.source, settings.ENV_NAME], fail_silently=False) except Exception as e: capture_exception(e)
def send_alert(self, alert, new_aids): """Send an email alert with a summary of the newly published aids.""" delete_url = reverse('alert_delete_view', args=[alert.token]) site = Site.objects.get_current() email_context = { 'domain': site.domain, 'alert': alert, 'nb_aids': len(new_aids), 'new_aids': new_aids[:3], 'delete_url': delete_url, 'contact_phone': settings.CONTACT_PHONE, } text_body = render_to_string('alerts/alert_body.txt', email_context) html_body = render_to_string('alerts/alert_body.html', email_context) email_subject_prefix = settings.EMAIL_SUBJECT_PREFIX if alert.title == settings.ADDNA_ALERT_TITLE: email_subject_prefix = settings.ADDNA_ALERT_EMAIL_SUBJECT_PREFIX email_subject = '{}{:%d/%m/%Y} — De nouvelles aides correspondent à ' \ 'vos recherches'.format( email_subject_prefix, timezone.now()) email_from = settings.DEFAULT_FROM_EMAIL email_to = [alert.email] try: send_email(subject=email_subject, body=text_body, recipient_list=email_to, from_email=email_from, html_body=html_body, tags=['alerte', settings.ENV_NAME], fail_silently=False) except Exception as e: capture_exception(e)