testemailsetup.py 1.1 KB

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