Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rc/only block conditonal alerts with ff #35511

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
10 changes: 6 additions & 4 deletions corehq/apps/hqwebapp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def mark_subevent_gateway_error(messaging_event_id, error, retrying=False):
bind=True, default_retry_delay=15 * 60, max_retries=10, acks_late=True)
def send_mail_async(self, subject, message, recipient_list, from_email=settings.DEFAULT_FROM_EMAIL,
messaging_event_id=None, filename: str = None, content=None, domain: str = None,
use_domain_gateway=False):
use_domain_gateway=False, is_conditional_alert=False):
""" Call with send_mail_async.delay(*args, **kwargs)
- sends emails in the main celery queue
- if sending fails, retry in 15 min
Expand All @@ -74,7 +74,7 @@ def send_mail_async(self, subject, message, recipient_list, from_email=settings.
get_valid_recipients,
mark_local_bounced_email,
)
filtered_recipient_list = get_valid_recipients(recipient_list, domain)
filtered_recipient_list = get_valid_recipients(recipient_list, domain, is_conditional_alert)
bounced_recipients = list(set(recipient_list) - set(filtered_recipient_list))
if bounced_recipients and messaging_event_id:
mark_local_bounced_email(bounced_recipients, messaging_event_id)
Expand Down Expand Up @@ -144,7 +144,8 @@ def send_html_email_async(self, subject, recipient, html_content,
smtp_exception_skip_list=None,
messaging_event_id=None,
domain=None,
use_domain_gateway=False):
use_domain_gateway=False,
is_conditional_alert=False):
""" Call with send_HTML_email_async.delay(*args, **kwargs)
- sends emails in the main celery queue
- if sending fails, retry in 15 min
Expand All @@ -163,7 +164,8 @@ def send_html_email_async(self, subject, recipient, html_content,
smtp_exception_skip_list=smtp_exception_skip_list,
messaging_event_id=messaging_event_id,
domain=domain,
use_domain_gateway=use_domain_gateway
use_domain_gateway=use_domain_gateway,
is_conditional_alert=is_conditional_alert
)
except Exception as e:
recipient = list(recipient) if not isinstance(recipient, str) else [recipient]
Expand Down
8 changes: 4 additions & 4 deletions corehq/ex-submodules/dimagi/utils/django/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ def mark_local_bounced_email(bounced_addresses, message_id):
)


def get_valid_recipients(recipients, domain=None):
def get_valid_recipients(recipients, domain=None, is_conditional_alert=False):
"""
This filters out any emails that have reported hard bounces or complaints to
Amazon SES
:param recipients: list of recipient emails
:return: list of recipient emails not marked as bounced
"""
from corehq.toggles import BLOCKED_DOMAIN_EMAIL_SENDERS
if domain and BLOCKED_DOMAIN_EMAIL_SENDERS.enabled(domain):
if is_conditional_alert and domain and BLOCKED_DOMAIN_EMAIL_SENDERS.enabled(domain):
# don't sent email if domain is blocked
metrics_gauge('commcare.bounced_email', len(recipients), tags={
'email_domain': domain,
Expand All @@ -84,9 +84,9 @@ def send_HTML_email(subject, recipient, html_content, text_content=None,
cc=None, email_from=settings.DEFAULT_FROM_EMAIL,
file_attachments=None, bcc=None,
smtp_exception_skip_list=None, messaging_event_id=None,
domain=None, use_domain_gateway=False):
domain=None, use_domain_gateway=False, is_conditional_alert=False):
recipients = list(recipient) if not isinstance(recipient, str) else [recipient]
filtered_recipients = get_valid_recipients(recipients, domain)
filtered_recipients = get_valid_recipients(recipients, domain, is_conditional_alert)
bounced_addresses = list(set(recipients) - set(filtered_recipients))
if bounced_addresses and messaging_event_id:
mark_local_bounced_email(bounced_addresses, messaging_event_id)
Expand Down
2 changes: 1 addition & 1 deletion corehq/messaging/scheduling/models/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def create_copy(self):


class Content(models.Model):
# If this this content is being invoked in the context of a case,
# If this content is being invoked in the context of a case,
# for example when a case triggers an alert, this is the case.
case = None

Expand Down
7 changes: 5 additions & 2 deletions corehq/messaging/scheduling/models/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def send(self, recipient, logged_event, phone_entry=None):
logged_subevent.error(MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
return

is_conditional_alert = self.case is not None
metrics_counter('commcare.messaging.email.sent', tags={'domain': domain})
if toggles.RICH_TEXT_EMAILS.enabled(domain) and html_message:
send_html_email_async.delay(
Expand All @@ -198,15 +199,17 @@ def send(self, recipient, logged_event, phone_entry=None):
text_content=message,
messaging_event_id=logged_subevent.id,
domain=domain,
use_domain_gateway=True)
use_domain_gateway=True,
is_conditional_alert=is_conditional_alert)
else:
send_mail_async.delay(
subject,
message,
[email_address],
messaging_event_id=logged_subevent.id,
domain=domain,
use_domain_gateway=True)
use_domain_gateway=True,
is_conditional_alert=is_conditional_alert)

email = Email(
domain=domain,
Expand Down
11 changes: 9 additions & 2 deletions corehq/util/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,12 @@ def get_traceback_string():
return f.getvalue()


def send_HTML_email(subject, recipient, html_content, *args, **kwargs):
return _send_HTML_email(subject, recipient, html_content, *args, **kwargs)
def send_HTML_email(subject, recipient, html_content, is_conditional_alert=False, *args, **kwargs):
return _send_HTML_email(
subject,
recipient,
html_content,
is_conditional_alert=is_conditional_alert,
*args,
**kwargs
)
2 changes: 1 addition & 1 deletion corehq/util/tests/test_bounced_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,6 @@ def test_get_valid_recipients(self):
recipients
)
self.assertEqual(
get_valid_recipients(recipients, domain=self.bad_domain),
get_valid_recipients(recipients, domain=self.bad_domain, is_conditional_alert=True),
[]
)
Loading