2021-06-02 20:28:49 +02:00
|
|
|
import io
|
|
|
|
import smtplib
|
|
|
|
from contextlib import redirect_stderr
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2018-05-14 21:01:48 +02:00
|
|
|
from django.conf import settings
|
2017-01-20 08:12:49 +01:00
|
|
|
from django.core.mail import mail_admins, mail_managers, send_mail
|
2018-05-14 21:01:48 +02:00
|
|
|
from django.core.management import CommandError
|
2017-01-20 08:12:49 +01:00
|
|
|
from django.core.management.commands import sendtestemail
|
2016-04-02 17:03:29 +02:00
|
|
|
|
2022-02-10 18:54:29 +01:00
|
|
|
from zerver.lib.send_email import FromAddress, log_email_config_errors
|
2017-07-02 05:27:01 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-01-20 08:12:49 +01:00
|
|
|
class Command(sendtestemail.Command):
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **kwargs: str) -> None:
|
2018-05-14 21:01:48 +02:00
|
|
|
if settings.WARN_NO_EMAIL:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise CommandError(
|
|
|
|
"Outgoing email not yet configured, see\n "
|
|
|
|
"https://zulip.readthedocs.io/en/latest/production/email.html"
|
|
|
|
)
|
2022-02-10 18:54:29 +01:00
|
|
|
|
|
|
|
log_email_config_errors()
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if len(kwargs["email"]) == 0:
|
2021-02-12 08:19:30 +01:00
|
|
|
raise CommandError(
|
|
|
|
"Usage: /home/zulip/deployments/current/manage.py "
|
|
|
|
"send_test_email username@example.com"
|
|
|
|
)
|
2019-03-13 22:22:20 +01:00
|
|
|
|
|
|
|
print("If you run into any trouble, read:")
|
|
|
|
print()
|
|
|
|
print(" https://zulip.readthedocs.io/en/latest/production/email.html#troubleshooting")
|
|
|
|
print()
|
|
|
|
print("The most common error is not setting `ADD_TOKENS_TO_NOREPLY_ADDRESS=False` when")
|
|
|
|
print("using an email provider that doesn't support that feature.")
|
|
|
|
print()
|
|
|
|
print("Sending 2 test emails from:")
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
message = (
|
|
|
|
"Success! If you receive this message (and a second with a different subject), "
|
|
|
|
"you've successfully configured sending emails from your Zulip server. "
|
|
|
|
"Remember that you need to restart "
|
|
|
|
"the Zulip server with /home/zulip/deployments/current/scripts/restart-server "
|
|
|
|
"after changing the settings in /etc/zulip before your changes will take effect."
|
|
|
|
)
|
2021-06-02 20:28:49 +02:00
|
|
|
with redirect_stderr(io.StringIO()) as f:
|
|
|
|
smtplib.SMTP.debuglevel = 1
|
|
|
|
try:
|
|
|
|
sender = FromAddress.SUPPORT
|
|
|
|
print(f" * {sender}")
|
|
|
|
send_mail("Zulip email test", message, sender, kwargs["email"])
|
|
|
|
|
|
|
|
noreply_sender = FromAddress.tokenized_no_reply_address()
|
|
|
|
print(f" * {noreply_sender}")
|
|
|
|
send_mail("Zulip noreply email test", message, noreply_sender, kwargs["email"])
|
|
|
|
except smtplib.SMTPException as e:
|
|
|
|
print(f"Failed to send mails: {e}")
|
|
|
|
print()
|
|
|
|
print("Full SMTP log follows:")
|
|
|
|
print(f.getvalue())
|
|
|
|
raise CommandError("Email sending failed!")
|
2019-03-13 22:22:20 +01:00
|
|
|
print()
|
2021-02-12 08:20:45 +01:00
|
|
|
print("Successfully sent 2 emails to {}!".format(", ".join(kwargs["email"])))
|
2017-01-20 08:12:49 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if kwargs["managers"]:
|
2018-05-27 20:13:23 +02:00
|
|
|
mail_managers("Zulip manager email test", "This email was sent to the site managers.")
|
2016-04-02 17:03:29 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if kwargs["admins"]:
|
2018-05-27 20:13:23 +02:00
|
|
|
mail_admins("Zulip admins email test", "This email was sent to the site admins.")
|