Debian Codegraph @

django/core/management/commands/sendtestemail.py

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