2019-11-16 01:29:16 +01:00
|
|
|
from argparse import ArgumentParser
|
2020-04-11 13:15:57 +02:00
|
|
|
from typing import Any
|
2019-11-16 01:29:16 +01:00
|
|
|
|
|
|
|
from zerver.lib.management import CommandError, ZulipBaseCommand
|
2020-04-11 13:15:57 +02:00
|
|
|
from zerver.lib.send_email import send_custom_email
|
2019-11-16 01:29:16 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2019-11-16 01:29:16 +01:00
|
|
|
class Command(ZulipBaseCommand):
|
2020-04-14 19:55:04 +02:00
|
|
|
help = """
|
|
|
|
Send a custom email with Zulip branding to the specified users.
|
|
|
|
|
|
|
|
Useful to send a notice to all users of a realm or server.
|
|
|
|
|
2020-08-11 01:47:49 +02:00
|
|
|
The From and Subject headers can be provided in the body of the Markdown
|
2020-04-14 19:55:04 +02:00
|
|
|
document used to generate the email, or on the command line."""
|
2019-11-16 01:29:16 +01:00
|
|
|
|
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'--entire-server', action="store_true", help="Send to every user on the server."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--markdown-template-path',
|
|
|
|
'--path',
|
|
|
|
required=True,
|
|
|
|
help='Path to a Markdown-format body for the email.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--subject',
|
|
|
|
help='Subject for the email. It can be declared in Markdown file in headers',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--from-name',
|
|
|
|
help='From line for the email. It can be declared in Markdown file in headers',
|
|
|
|
)
|
|
|
|
parser.add_argument('--reply-to', help='Optional reply-to line for the email')
|
|
|
|
parser.add_argument(
|
|
|
|
'--admins-only', help='Send only to organization administrators', action='store_true'
|
|
|
|
)
|
|
|
|
|
|
|
|
self.add_user_list_args(
|
|
|
|
parser,
|
|
|
|
help="Email addresses of user(s) to send emails to.",
|
|
|
|
all_users_help="Send to every user on the realm.",
|
|
|
|
)
|
2019-11-16 01:29:16 +01:00
|
|
|
self.add_realm_args(parser)
|
|
|
|
|
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
|
|
|
if options["entire_server"]:
|
2021-02-12 08:19:30 +01:00
|
|
|
users = UserProfile.objects.filter(is_active=True, is_bot=False, is_mirror_dummy=False)
|
2019-11-16 01:29:16 +01:00
|
|
|
else:
|
|
|
|
realm = self.get_realm(options)
|
|
|
|
try:
|
|
|
|
users = self.get_users(options, realm, is_bot=False)
|
|
|
|
except CommandError as error:
|
|
|
|
if str(error) == "You have to pass either -u/--users or -a/--all-users.":
|
2021-02-12 08:19:30 +01:00
|
|
|
raise CommandError(
|
|
|
|
"You have to pass -u/--users or -a/--all-users or --entire-server."
|
|
|
|
)
|
2019-11-16 01:29:16 +01:00
|
|
|
raise error
|
|
|
|
|
|
|
|
send_custom_email(users, options)
|