mirror of https://github.com/zulip/zulip.git
emails: Log emails that are queued or sent in dev environment.
Tweaked by tabbott to add some comments and clarify the code.
This commit is contained in:
parent
bb1b771c44
commit
f58c87917e
|
@ -0,0 +1,16 @@
|
|||
{% if failed %}
|
||||
<p>{{template}} failed to render: {{ reason }}</p>
|
||||
{% else %}
|
||||
<h4>From: {{ from_email }}</h4>
|
||||
<h4>To:
|
||||
{% for recipient in recipients %}
|
||||
{{ recipient }}
|
||||
{% endfor %}
|
||||
</h4>
|
||||
<h4>Subject: {{subject}}</h4>
|
||||
{% autoescape off %}
|
||||
{{ html_message }}
|
||||
{% endautoescape %}
|
||||
<pre>{{ body }}</pre>
|
||||
{% endif %}
|
||||
<hr/>
|
|
@ -21,6 +21,35 @@ class FromAddress(object):
|
|||
SUPPORT = parseaddr(settings.ZULIP_ADMINISTRATOR)[1]
|
||||
NOREPLY = parseaddr(settings.NOREPLY_EMAIL_ADDRESS)[1]
|
||||
|
||||
def log_email(email, template_prefix):
|
||||
# type: (EmailMultiAlternatives, str) -> None
|
||||
"""Used in development to record sent emails in a nice HTML log"""
|
||||
html_message = 'Missing HTML message'
|
||||
if len(email.alternatives) > 0:
|
||||
html_message = email.alternatives[0][0]
|
||||
|
||||
context = {
|
||||
'template': template_prefix + ".html",
|
||||
'subject': email.subject,
|
||||
'from_email': email.from_email,
|
||||
'recipients': email.to,
|
||||
'body': email.body,
|
||||
'html_message': html_message
|
||||
}
|
||||
|
||||
new_email = loader.render_to_string('zerver/email.html', context)
|
||||
|
||||
# Read in the pre-existing log, so that we can add the new entry
|
||||
# at the top.
|
||||
try:
|
||||
with open(settings.EMAIL_CONTENT_LOG_PATH, "r") as f:
|
||||
previous_emails = f.read()
|
||||
except FileNotFoundError:
|
||||
previous_emails = ""
|
||||
|
||||
with open(settings.EMAIL_CONTENT_LOG_PATH, "w+") as f:
|
||||
f.write(new_email + previous_emails)
|
||||
|
||||
def build_email(template_prefix, to_user_id=None, to_email=None, from_name=None,
|
||||
from_address=None, reply_to_email=None, context=None):
|
||||
# type: (str, Optional[int], Optional[Text], Optional[Text], Optional[Text], Optional[Text], Optional[Dict[str, Any]]) -> EmailMultiAlternatives
|
||||
|
@ -80,6 +109,9 @@ def send_email(template_prefix, to_user_id=None, to_email=None, from_name=None,
|
|||
template = template_prefix.split("/")[-1]
|
||||
logger.info("Sending %s email to %s" % (template, mail.to))
|
||||
|
||||
if settings.DEVELOPMENT:
|
||||
log_email(mail, template_prefix)
|
||||
|
||||
if mail.send() == 0:
|
||||
logger.error("Error sending %s email to %s" % (template, mail.to))
|
||||
raise EmailNotDeliveredException
|
||||
|
@ -95,6 +127,14 @@ def send_future_email(template_prefix, to_user_id=None, to_email=None, from_name
|
|||
email_fields = {'template_prefix': template_prefix, 'to_user_id': to_user_id, 'to_email': to_email,
|
||||
'from_name': from_name, 'from_address': from_address, 'context': context}
|
||||
|
||||
if settings.DEVELOPMENT:
|
||||
mail = build_email(template_prefix, to_user_id=to_user_id, to_email=to_email, from_name=from_name,
|
||||
from_address=from_address, context=context)
|
||||
# Technically, this will be called. But we currently don't
|
||||
# run the `manage.py deliver_email` backend job in the
|
||||
# development environment.
|
||||
log_email(mail, template_prefix)
|
||||
|
||||
assert (to_user_id is None) ^ (to_email is None)
|
||||
if to_user_id is not None:
|
||||
to_field = {'user_id': to_user_id} # type: Dict[str, Any]
|
||||
|
|
|
@ -313,7 +313,7 @@ class LoginTest(ZulipTestCase):
|
|||
with queries_captured() as queries:
|
||||
self.register(self.nonreg_email('test'), "test")
|
||||
# Ensure the number of queries we make is not O(streams)
|
||||
self.assert_length(queries, 67)
|
||||
self.assert_length(queries, 69)
|
||||
user_profile = self.nonreg_user('test')
|
||||
self.assertEqual(get_session_dict_user(self.client.session), user_profile.id)
|
||||
self.assertFalse(user_profile.enable_stream_desktop_notifications)
|
||||
|
|
|
@ -6,6 +6,7 @@ import os
|
|||
from typing import Set
|
||||
|
||||
LOCAL_UPLOADS_DIR = 'var/uploads'
|
||||
EMAIL_LOG_DIR = "/var/log/zulip/email.log"
|
||||
# Default to subdomains disabled in development until we can update
|
||||
# the development documentation to make sense with subdomains.
|
||||
REALMS_HAVE_SUBDOMAINS = False
|
||||
|
|
|
@ -1007,6 +1007,7 @@ ZULIP_PATHS = [
|
|||
("EMAIL_LOG_PATH", "/var/log/zulip/send_email.log"),
|
||||
("EMAIL_MIRROR_LOG_PATH", "/var/log/zulip/email_mirror.log"),
|
||||
("EMAIL_DELIVERER_LOG_PATH", "/var/log/zulip/email-deliverer.log"),
|
||||
("EMAIL_CONTENT_LOG_PATH", "/var/log/zulip/email_content.log"),
|
||||
("LDAP_SYNC_LOG_PATH", "/var/log/zulip/sync_ldap_user_data.log"),
|
||||
("QUEUE_ERROR_DIR", "/var/log/zulip/queue_error"),
|
||||
("STATS_DIR", "/home/zulip/stats"),
|
||||
|
|
Loading…
Reference in New Issue