You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sendtestemail.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import socket
  2. from django.core.mail import mail_admins, mail_managers, send_mail
  3. from django.core.management.base import BaseCommand
  4. from django.utils import timezone
  5. class Command(BaseCommand):
  6. help = "Sends a test email to the email addresses specified as arguments."
  7. missing_args_message = "You must specify some email recipients, or pass the --managers or --admin options."
  8. def add_arguments(self, parser):
  9. parser.add_argument(
  10. 'email', nargs='*',
  11. help='One or more email addresses to send a test email to.',
  12. )
  13. parser.add_argument(
  14. '--managers', action='store_true',
  15. help='Send a test email to the addresses specified in settings.MANAGERS.',
  16. )
  17. parser.add_argument(
  18. '--admins', action='store_true',
  19. help='Send a test email to the addresses specified in settings.ADMINS.',
  20. )
  21. def handle(self, *args, **kwargs):
  22. subject = 'Test email from %s on %s' % (socket.gethostname(), timezone.now())
  23. send_mail(
  24. subject=subject,
  25. message="If you\'re reading this, it was successful.",
  26. from_email=None,
  27. recipient_list=kwargs['email'],
  28. )
  29. if kwargs['managers']:
  30. mail_managers(subject, "This email was sent to the site managers.")
  31. if kwargs['admins']:
  32. mail_admins(subject, "This email was sent to the site admins.")