testemailsetup.py 993 B

123456789101112131415161718192021222324252627282930
  1. from django.conf import settings
  2. from django.core import mail
  3. from django.core.exceptions import ValidationError
  4. from django.core.management.base import BaseCommand
  5. from django.core.validators import validate_email
  6. class Command(BaseCommand):
  7. help = 'Sends test e-mail to given address'
  8. def add_arguments(self, parser):
  9. parser.add_argument('email', type=str)
  10. def handle(self, *args, **options):
  11. try:
  12. email = options['email']
  13. validate_email(email)
  14. self.send_message(email)
  15. except ValidationError:
  16. self.stderr.write("This isn't valid e-mail address")
  17. def send_message(self, email):
  18. mail.send_mail(
  19. 'Test Message',
  20. ("This message was sent to test if your "
  21. "site e-mail is configured correctly."),
  22. settings.DEFAULT_FROM_EMAIL,
  23. [email],
  24. fail_silently=False)
  25. self.stdout.write("Test message was sent to %s" % email)