notifications: Change sender arg of send_future_email to from_email.

This commit is a step towards the goal of replacing most of the
send_future_email pathway with a call to send_email.

Note that this commit changes the default value of sender from "Zulip
<NOREPLY_EMAIL_ADDRESS>" to "NOREPLY_EMAIL_ADDRESS". NOREPLY_EMAIL_ADDRESS
will soon be changed to have the Zulip in front.
This commit is contained in:
Rishi Gupta
2017-05-03 17:47:55 -07:00
committed by Tim Abbott
parent e46cbaffa2
commit d70e09b41d
5 changed files with 15 additions and 26 deletions

View File

@@ -204,7 +204,6 @@ def handle_digest_email(user_profile_id, cutoff):
template_payload["new_users"] = new_users
recipients = [{'email': user_profile.email, 'name': user_profile.full_name}]
sender = {'email': settings.NOREPLY_EMAIL_ADDRESS, 'name': 'Zulip'}
# We don't want to send emails containing almost no information.
if enough_traffic(template_payload["unread_pms"],
@@ -212,5 +211,5 @@ def handle_digest_email(user_profile_id, cutoff):
new_streams_count, new_users_count):
logger.info("Sending digest email for %s" % (user_profile.email,))
# Send now, as a ScheduledJob
send_future_email('zerver/emails/digest', recipients, sender=sender,
send_future_email('zerver/emails/digest', recipients,
context=template_payload, tags=["digest-emails"])

View File

@@ -378,25 +378,22 @@ def log_digest_event(msg):
logging.basicConfig(filename=settings.DIGEST_LOG_PATH, level=logging.INFO)
logging.info(msg)
def send_future_email(template_prefix, recipients, sender=None, context={},
def send_future_email(template_prefix, recipients, from_email=None, context={},
delay=datetime.timedelta(0), tags=[]):
# type: (str, List[Dict[str, Any]], Optional[Dict[str, Text]], Dict[str, Any], datetime.timedelta, Iterable[Text]) -> None
# type: (str, List[Dict[str, Any]], Optional[Text], Dict[str, Any], datetime.timedelta, Iterable[Text]) -> None
subject = loader.render_to_string(template_prefix + '.subject', context).strip()
email_text = loader.render_to_string(template_prefix + '.txt', context)
email_html = loader.render_to_string(template_prefix + '.html', context)
# SMTP mail delivery implementation
if sender is None:
# This may likely overridden by settings.DEFAULT_FROM_EMAIL
sender = {'email': settings.NOREPLY_EMAIL_ADDRESS, 'name': 'Zulip'}
if from_email is None:
from_email = settings.NOREPLY_EMAIL_ADDRESS
for recipient in recipients:
email_fields = {'email_html': email_html,
'email_subject': subject,
'email_text': email_text,
'recipient_email': recipient.get('email'),
'recipient_name': recipient.get('name'),
'sender_email': sender['email'],
'sender_name': sender['name']}
'from_email': from_email}
ScheduledJob.objects.create(type=ScheduledJob.EMAIL, filter_string=recipient.get('email'),
data=ujson.dumps(email_fields),
scheduled_timestamp=timezone_now() + delay)
@@ -405,9 +402,11 @@ def enqueue_welcome_emails(email, name):
# type: (Text, Text) -> None
from zerver.context_processors import common_context
if settings.WELCOME_EMAIL_SENDER is not None:
sender = settings.WELCOME_EMAIL_SENDER # type: Dict[str, Text]
# line break to avoid triggering lint rule
from_email = '%(name)s <%(email)s>' % \
settings.WELCOME_EMAIL_SENDER
else:
sender = {'email': settings.ZULIP_ADMINISTRATOR, 'name': 'Zulip'}
from_email = settings.ZULIP_ADMINISTRATOR
user_profile = get_user_profile_by_email(email)
unsubscribe_link = one_click_unsubscribe_link(user_profile, "welcome")
@@ -418,11 +417,11 @@ def enqueue_welcome_emails(email, name):
})
send_future_email(
"zerver/emails/followup_day1", [{'email': email, 'name': name}],
sender=sender, context=context, delay=datetime.timedelta(hours=1),
from_email=from_email, context=context, delay=datetime.timedelta(hours=1),
tags=["followup-emails"])
send_future_email(
"zerver/emails/followup_day2", [{'email': email, 'name': name}],
sender=sender, context=context, delay=datetime.timedelta(days=1),
from_email=from_email, context=context, delay=datetime.timedelta(days=1),
tags=["followup-emails"])
def convert_html_to_markdown(html):

View File

@@ -44,20 +44,12 @@ def get_recipient_as_string(dictionary):
return dictionary["recipient_email"]
return format_html(u"\"{0}\" <{1}>", dictionary["recipient_name"], dictionary["recipient_email"])
def get_sender_as_string(dictionary):
# type: (Dict[str, str]) -> str
if dictionary["sender_email"]:
return dictionary["sender_email"] if not dictionary["sender_name"] else format_html(u"\"{0}\" <{1}>",
dictionary["sender_name"],
dictionary["sender_email"])
return settings.DEFAULT_FROM_EMAIL
def send_email_job(job):
# type: (ScheduledJob) -> bool
data = loads(job.data)
subject = data["email_subject"]
message = data["email_text"]
from_email = get_sender_as_string(data)
from_email = data["from_email"]
to_email = get_recipient_as_string(data)
if data["email_html"]:

View File

@@ -739,7 +739,7 @@ so we didn't send them an invitation. We did send invitations to everyone else!"
with self.settings(EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend'):
send_future_email(
"zerver/emails/invitation_reminder", [{'email': data["email"], 'name': ""}],
sender={'email': settings.ZULIP_ADMINISTRATOR, 'name': 'Zulip'},
from_email=settings.ZULIP_ADMINISTRATOR,
context=context, tags=["invitation-reminders"])
email_jobs_to_deliver = ScheduledJob.objects.filter(
type=ScheduledJob.EMAIL,
@@ -856,7 +856,6 @@ class EmailUnsubscribeTests(ZulipTestCase):
context = defaultdict(str) # type: Dict[str, Any]
context['new_streams'] = defaultdict(str)
send_future_email('zerver/emails/digest', [{'email': email, 'name': user_profile.full_name}],
sender={'email': settings.NOREPLY_EMAIL_ADDRESS, 'name': 'Zulip'},
context=context, tags=["digest-emails"])
self.assertEqual(1, len(ScheduledJob.objects.filter(

View File

@@ -166,7 +166,7 @@ class ConfirmationEmailWorker(QueueProcessingWorker):
send_future_email(
"zerver/emails/invitation_reminder",
[{'email': data["email"], 'name': ""}],
sender={'email': settings.ZULIP_ADMINISTRATOR, 'name': 'Zulip'},
from_email=settings.ZULIP_ADMINISTRATOR,
context=context,
delay=datetime.timedelta(days=2),
tags=["invitation-reminders"])