mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
emails: Refactor send_email functions to take both a sender name and address.
This will allow for customized senders for emails, e.g. 'Zulip Digest' for digest emails and 'Zulip Missed Messages' for missed message emails. Also: * Converts the sender name to always be "Zulip", if the from_email used to be settings.NOREPLY_EMAIL_ADDRESS or settings.ZULIP_ADMINISTRATOR. * Changes the default value of settings.NOREPLY_EMAIL_ADDRESS in the prod_setting_template to no longer have a display name. The only use of that display name was in the email pathway.
This commit is contained in:
@@ -5,7 +5,7 @@ from django.utils.timezone import now as timezone_now
|
||||
from zerver.models import UserProfile, ScheduledJob
|
||||
|
||||
import datetime
|
||||
from email.utils import parseaddr
|
||||
from email.utils import parseaddr, formataddr
|
||||
import ujson
|
||||
|
||||
from typing import Any, Dict, Iterable, List, Mapping, Optional, Text
|
||||
@@ -17,8 +17,9 @@ def display_email(user):
|
||||
return user.email
|
||||
|
||||
# Intended only for test code
|
||||
def build_email(template_prefix, to_email, from_email=None, reply_to_email=None, context={}):
|
||||
# type: (str, Text, Optional[Text], Optional[Text], Dict[str, Any]) -> EmailMultiAlternatives
|
||||
def build_email(template_prefix, to_email, from_name=None, from_address=None,
|
||||
reply_to_email=None, context={}):
|
||||
# type: (str, Text, Optional[Text], Optional[Text], Optional[Text], Dict[str, Any]) -> EmailMultiAlternatives
|
||||
context.update({
|
||||
'support_email': settings.ZULIP_ADMINISTRATOR,
|
||||
'verbose_support_offers': settings.VERBOSE_SUPPORT_OFFERS,
|
||||
@@ -28,8 +29,12 @@ def build_email(template_prefix, to_email, from_email=None, reply_to_email=None,
|
||||
message = loader.render_to_string(template_prefix + '.txt',
|
||||
context=context, using='Jinja2_plaintext')
|
||||
html_message = loader.render_to_string(template_prefix + '.html', context)
|
||||
if from_email is None:
|
||||
from_email = settings.NOREPLY_EMAIL_ADDRESS
|
||||
|
||||
if from_name is None:
|
||||
from_name = "Zulip"
|
||||
if from_address is None:
|
||||
from_address = parseaddr(settings.NOREPLY_EMAIL_ADDRESS)[1]
|
||||
from_email = formataddr((from_name, from_address))
|
||||
reply_to = None
|
||||
if reply_to_email is not None:
|
||||
reply_to = [reply_to_email]
|
||||
@@ -39,15 +44,16 @@ def build_email(template_prefix, to_email, from_email=None, reply_to_email=None,
|
||||
mail.attach_alternative(html_message, 'text/html')
|
||||
return mail
|
||||
|
||||
def send_email(template_prefix, to_email, from_email=None, reply_to_email=None, context={}):
|
||||
# type: (str, Text, Optional[Text], Optional[Text], Dict[str, Any]) -> bool
|
||||
mail = build_email(template_prefix, to_email, from_email=from_email,
|
||||
reply_to_email=reply_to_email, context=context)
|
||||
def send_email(template_prefix, to_email, from_name=None, from_address=None, reply_to_email=None, context={}):
|
||||
# type: (str, Text, Optional[Text], Optional[Text], Optional[Text], Dict[str, Any]) -> bool
|
||||
mail = build_email(template_prefix, to_email, from_name=from_name,
|
||||
from_address=from_address, reply_to_email=reply_to_email, context=context)
|
||||
return mail.send() > 0
|
||||
|
||||
def send_email_to_user(template_prefix, user, from_email=None, context={}):
|
||||
# type: (str, UserProfile, Optional[Text], Dict[str, Text]) -> bool
|
||||
return send_email(template_prefix, display_email(user), from_email=from_email, context=context)
|
||||
def send_email_to_user(template_prefix, user, from_name=None, from_address=None, context={}):
|
||||
# type: (str, UserProfile, Optional[Text], Optional[Text], Dict[str, Text]) -> bool
|
||||
return send_email(template_prefix, display_email(user), from_name=from_name,
|
||||
from_address=from_address, context=context)
|
||||
|
||||
# Returns None instead of bool so that the type signature matches the third
|
||||
# argument of zerver.lib.queue.queue_json_publish
|
||||
@@ -55,11 +61,11 @@ def send_email_from_dict(email_dict):
|
||||
# type: (Mapping[str, Any]) -> None
|
||||
send_email(**dict(email_dict))
|
||||
|
||||
def send_future_email(template_prefix, to_email, from_email=None, context={},
|
||||
def send_future_email(template_prefix, to_email, from_name=None, from_address=None, context={},
|
||||
delay=datetime.timedelta(0)):
|
||||
# type: (str, Text, Optional[Text], Dict[str, Any], datetime.timedelta) -> None
|
||||
email_fields = {'template_prefix': template_prefix, 'to_email': to_email, 'from_email': from_email,
|
||||
'context': context}
|
||||
# type: (str, Text, Optional[Text], Optional[Text], Dict[str, Any], datetime.timedelta) -> None
|
||||
email_fields = {'template_prefix': template_prefix, 'to_email': to_email, 'from_name': from_name,
|
||||
'from_address': from_address, 'context': context}
|
||||
ScheduledJob.objects.create(type=ScheduledJob.EMAIL, filter_string=parseaddr(to_email)[1],
|
||||
data=ujson.dumps(email_fields),
|
||||
scheduled_timestamp=timezone_now() + delay)
|
||||
|
||||
Reference in New Issue
Block a user