testemailsetup.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  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 handle(self, *args, **options):
  9. try:
  10. if len(args) != 1:
  11. raise ValueError()
  12. email = args[0]
  13. validate_email(email)
  14. self.send_message(email)
  15. except ValueError:
  16. self.stderr.write("Command accepts exactly "
  17. "one argument (e-mail address)")
  18. except ValidationError:
  19. self.stderr.write("This isn't valid e-mail address")
  20. def send_message(self, email):
  21. mail.send_mail(
  22. 'Test Message',
  23. ("This message was sent to test if your "
  24. "site e-mail is configured correctly."),
  25. settings.DEFAULT_FROM_EMAIL,
  26. [email],
  27. fail_silently=False)
  28. self.stdout.write("Test message was sent to %s" % email)