2015-03-07 20:55:59 +08:00
|
|
|
import socket
|
|
|
|
|
2015-06-16 20:46:56 +08:00
|
|
|
from django.core.mail import mail_admins, mail_managers, send_mail
|
2015-03-21 05:57:11 +08:00
|
|
|
from django.core.management.base import BaseCommand
|
2015-06-16 20:46:56 +08:00
|
|
|
from django.utils import timezone
|
2015-03-07 20:55:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Sends a test email to the email addresses specified as arguments."
|
2015-06-16 20:46:56 +08:00
|
|
|
missing_args_message = "You must specify some email recipients, or pass the --managers or --admin options."
|
2015-03-21 03:23:43 +08:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'email', nargs='*',
|
|
|
|
help='One or more email addresses to send a test email to.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--managers', action='store_true', dest='managers', default=False,
|
|
|
|
help='Send a test email to the addresses specified in settings.MANAGERS.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--admins', action='store_true', dest='admins', default=False,
|
|
|
|
help='Send a test email to the addresses specified in settings.ADMINS.',
|
|
|
|
)
|
2015-03-07 20:55:59 +08:00
|
|
|
|
|
|
|
def handle(self, *args, **kwargs):
|
2015-06-16 20:46:56 +08:00
|
|
|
subject = 'Test email from %s on %s' % (socket.gethostname(), timezone.now())
|
|
|
|
|
2015-03-07 20:55:59 +08:00
|
|
|
send_mail(
|
2015-06-16 20:46:56 +08:00
|
|
|
subject=subject,
|
2015-03-07 20:55:59 +08:00
|
|
|
message="If you\'re reading this, it was successful.",
|
|
|
|
from_email=None,
|
2015-03-21 03:23:43 +08:00
|
|
|
recipient_list=kwargs['email'],
|
2015-03-07 20:55:59 +08:00
|
|
|
)
|
2015-06-16 20:46:56 +08:00
|
|
|
|
|
|
|
if kwargs['managers']:
|
|
|
|
mail_managers(subject, "This email was sent to the site managers.")
|
|
|
|
|
|
|
|
if kwargs['admins']:
|
|
|
|
mail_admins(subject, "This email was sent to the site admins.")
|