authentik.stages.email.management.commands.test_email

Send a test-email with global settings

 1"""Send a test-email with global settings"""
 2
 3from uuid import uuid4
 4
 5from django.core.management.base import no_translations
 6
 7from authentik.stages.email.models import EmailStage
 8from authentik.stages.email.tasks import send_mail
 9from authentik.stages.email.utils import TemplateEmailMessage
10from authentik.tenants.management import TenantCommand
11
12
13class Command(TenantCommand):
14    """Send a test-email with global settings"""
15
16    @no_translations
17    def handle_per_tenant(self, *args, **options):
18        """Send a test-email with global settings"""
19        delete_stage = False
20        if options["stage"]:
21            stages = EmailStage.objects.filter(name=options["stage"])
22            if not stages.exists():
23                self.stderr.write(f"Stage '{options['stage']}' does not exist")
24                return
25            stage = stages.first()
26        else:
27            stage = EmailStage.objects.create(
28                name=f"temp-global-stage-{uuid4()}", use_global_settings=True
29            )
30            delete_stage = True
31        message = TemplateEmailMessage(
32            subject="authentik Test-Email",
33            to=[("", options["to"])],
34            template_name="email/setup.html",
35            template_context={},
36        )
37        try:
38            if not stage.use_global_settings:
39                message.from_email = stage.from_address
40
41            send_mail.send(message.__dict__, stage.pk).get_result(block=True)
42
43            self.stdout.write(self.style.SUCCESS(f"Test email sent to {options['to']}"))
44        finally:
45            if delete_stage:
46                stage.delete()
47
48    def add_arguments(self, parser):
49        parser.add_argument("to", type=str)
50        parser.add_argument("-S", "--stage", type=str)
class Command(authentik.tenants.management.TenantCommand):
14class Command(TenantCommand):
15    """Send a test-email with global settings"""
16
17    @no_translations
18    def handle_per_tenant(self, *args, **options):
19        """Send a test-email with global settings"""
20        delete_stage = False
21        if options["stage"]:
22            stages = EmailStage.objects.filter(name=options["stage"])
23            if not stages.exists():
24                self.stderr.write(f"Stage '{options['stage']}' does not exist")
25                return
26            stage = stages.first()
27        else:
28            stage = EmailStage.objects.create(
29                name=f"temp-global-stage-{uuid4()}", use_global_settings=True
30            )
31            delete_stage = True
32        message = TemplateEmailMessage(
33            subject="authentik Test-Email",
34            to=[("", options["to"])],
35            template_name="email/setup.html",
36            template_context={},
37        )
38        try:
39            if not stage.use_global_settings:
40                message.from_email = stage.from_address
41
42            send_mail.send(message.__dict__, stage.pk).get_result(block=True)
43
44            self.stdout.write(self.style.SUCCESS(f"Test email sent to {options['to']}"))
45        finally:
46            if delete_stage:
47                stage.delete()
48
49    def add_arguments(self, parser):
50        parser.add_argument("to", type=str)
51        parser.add_argument("-S", "--stage", type=str)

Send a test-email with global settings

def handle_per_tenant(*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):
49    def add_arguments(self, parser):
50        parser.add_argument("to", type=str)
51        parser.add_argument("-S", "--stage", type=str)

Entry point for subclassed commands to add custom arguments.