Projet

Général

Profil

0001-utils-do-not-try-to-send-mail-to-clearly-unusable-em.patch

Benjamin Dauvergne, 04 juillet 2022 10:13

Télécharger (3,62 ko)

Voir les différences:

Subject: [PATCH] utils: do not try to send mail to clearly unusable email
 addresses (#62354)

 .../commands/clean-unused-accounts.py         |  3 +-
 src/authentic2/utils/misc.py                  | 29 +++++++++++++++++--
 2 files changed, 27 insertions(+), 5 deletions(-)
src/authentic2/management/commands/clean-unused-accounts.py
129 129
        else:
130 130
            logger.debug('sending mail to %s', user.email)
131 131
            if not self.fake:
132
                email = user.email
133 132

  
134 133
                def send_mail():
135
                    send_templated_mail(email, prefix, ctx)
134
                    send_templated_mail(user, prefix, ctx)
136 135

  
137 136
                transaction.on_commit(send_mail)
138 137

  
src/authentic2/utils/misc.py
32 32
from django.contrib.auth import authenticate as dj_authenticate
33 33
from django.contrib.auth import get_user_model
34 34
from django.contrib.auth import login as auth_login
35
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
35
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured, ValidationError
36 36
from django.core.mail import EmailMessage, send_mail
37 37
from django.forms.utils import ErrorList, to_current_timezone
38 38
from django.http import HttpResponse, HttpResponseRedirect
......
47 47
from django.utils.translation import ngettext
48 48

  
49 49
from authentic2.saml.saml2utils import filter_attribute_private_key, filter_element_private_key
50
from authentic2.validators import EmailValidator
50 51

  
51 52
from .. import app_settings, constants, crypto, plugins
52 53
from .cache import GlobalCache
......
662 663
    return template.template.render(make_context(ctx, request=request, autoescape=False))
663 664

  
664 665

  
666
class SendEmailError(Exception):
667
    pass
668

  
669

  
665 670
def send_templated_mail(
666 671
    user_or_email,
667 672
    template_names,
......
691 696
            new_template_names.append(template)
692 697
        template_names = new_template_names
693 698
    if hasattr(user_or_email, 'email'):
694
        user_or_email = user_or_email.email
699
        email = user_or_email.email
700
        user = user_or_email
701
    else:
702
        email = user_or_email
703
        user = None
704

  
705
    # check email is syntaxically valid before trying to send it
706
    try:
707
        EmailValidator()(email)
708
    except ValidationError as e:
709
        logger = logging.getLogger(__name__)
710
        extra = {}
711
        if user:
712
            extra['user'] = user
713
        logger.warning(
714
            'send_templated_email: user=%s email=%r templates=%s error=%s', user, email, template_names, e
715
        )
716
        return
717

  
695 718
    if not request:
696 719
        request = middleware.StoreRequestMiddleware().get_request()
697 720

  
......
732 755
            subject,
733 756
            body,
734 757
            from_email or settings.DEFAULT_FROM_EMAIL,
735
            [user_or_email],
758
            [email],
736 759
            html_message=html_body,
737 760
            **kwargs,
738 761
        )
739
-