authentik.stages.email.management.commands.test_email

Send a test-email with global settings

 1"""Send a test-email with global settings"""
 2
 3from django.core.management.base import BaseCommand, no_translations
 4
 5from authentik.lib.utils.reflection import class_to_path
 6from authentik.stages.email.models import EmailStage
 7from authentik.stages.email.tasks import send_mail
 8from authentik.stages.email.utils import TemplateEmailMessage
 9
10
11class Command(BaseCommand):
12    """Send a test-email with global settings"""
13
14    @no_translations
15    def handle(self, *args, **options):
16        """Send a test-email with global settings"""
17        stage = None
18        if options["stage"]:
19            stages = EmailStage.objects.filter(name=options["stage"])
20            if not stages.exists():
21                self.stderr.write(f"Stage '{options['stage']}' does not exist")
22                return
23            stage = stages.first()
24        message = TemplateEmailMessage(
25            subject="authentik Test-Email",
26            to=[("", options["to"])],
27            template_name="email/setup.html",
28            template_context={},
29        )
30        # Use the class path instead of the class itself for serialization
31        stage_class_path, stage_pk = None, None
32        if stage:
33            stage_class_path = class_to_path(stage.__class__)
34            stage_pk = str(stage.pk)
35        send_mail.send(message.__dict__, stage_class_path, stage_pk).get_result(block=True)
36
37        self.stdout.write(self.style.SUCCESS(f"Test email sent to {options['to']}"))
38
39    def add_arguments(self, parser):
40        parser.add_argument("to", type=str)
41        parser.add_argument("-S", "--stage", type=str)
class Command(django.core.management.base.BaseCommand):
12class Command(BaseCommand):
13    """Send a test-email with global settings"""
14
15    @no_translations
16    def handle(self, *args, **options):
17        """Send a test-email with global settings"""
18        stage = None
19        if options["stage"]:
20            stages = EmailStage.objects.filter(name=options["stage"])
21            if not stages.exists():
22                self.stderr.write(f"Stage '{options['stage']}' does not exist")
23                return
24            stage = stages.first()
25        message = TemplateEmailMessage(
26            subject="authentik Test-Email",
27            to=[("", options["to"])],
28            template_name="email/setup.html",
29            template_context={},
30        )
31        # Use the class path instead of the class itself for serialization
32        stage_class_path, stage_pk = None, None
33        if stage:
34            stage_class_path = class_to_path(stage.__class__)
35            stage_pk = str(stage.pk)
36        send_mail.send(message.__dict__, stage_class_path, stage_pk).get_result(block=True)
37
38        self.stdout.write(self.style.SUCCESS(f"Test email sent to {options['to']}"))
39
40    def add_arguments(self, parser):
41        parser.add_argument("to", type=str)
42        parser.add_argument("-S", "--stage", type=str)

Send a test-email with global settings

def handle(*args, **kwargs):
106    def wrapper(*args, **kwargs):
107        from django.utils import translation
108
109        saved_locale = translation.get_language()
110        translation.deactivate_all()
111        try:
112            res = handle_func(*args, **kwargs)
113        finally:
114            if saved_locale is not None:
115                translation.activate(saved_locale)
116        return res

The type of the None singleton.

def add_arguments(self, parser):
40    def add_arguments(self, parser):
41        parser.add_argument("to", type=str)
42        parser.add_argument("-S", "--stage", type=str)

Entry point for subclassed commands to add custom arguments.