from django.core.management.base import BaseCommand, CommandError from portal.mailer import services from portal.mailer.models import OutboxMail class Command(BaseCommand): help = "Queue a test e-mail and send it now, to check the SMTP settings." def add_arguments(self, parser): parser.add_argument("recipient", help="E-mail address that should receive the test mail") def handle(self, *args, recipient, **options): try: mail = services.enqueue("test", recipient) except ValueError as exc: raise CommandError(str(exc)) from exc result = services.flush() mail.refresh_from_db() self.stdout.write(f"Outbox entry {mail.pk}: {mail.status} ({result})") if mail.status == OutboxMail.Status.SENT: self.stdout.write("The mail server accepted the message.") elif mail.status == OutboxMail.Status.QUEUED: self.stdout.write(f"Not sent yet, will be retried: {mail.last_error}") raise SystemExit(1) else: self.stdout.write(f"Failed: {mail.last_error}") raise SystemExit(1)