def test_get_valid_recipients(self):
     recipients = [
         '*****@*****.**',
         '*****@*****.**',
         '*****@*****.**',
     ]
     self.assertEqual(get_valid_recipients(recipients), recipients)
     self.assertEqual(
         get_valid_recipients(recipients, domain='not-blocked-domain'),
         recipients)
     self.assertEqual(
         get_valid_recipients(recipients, domain=self.bad_domain), [])
示例#2
0
def send_mail_async(self,
                    subject,
                    message,
                    from_email,
                    recipient_list,
                    fail_silently=False,
                    auth_user=None,
                    auth_password=None,
                    connection=None):
    """ Call with send_mail_async.delay(*args, **kwargs)
    - sends emails in the main celery queue
    - if sending fails, retry in 15 min
    - retry a maximum of 10 times
    """
    from corehq.util.soft_assert import soft_assert
    soft_assert('{}@dimagi.com'.format('skelly'))(all(
        recipient for recipient in recipient_list), 'Blank email addresses', {
            'subject': subject,
            'message': message,
            'recipients': recipient_list
        })

    recipient_list = [_f for _f in recipient_list if _f]

    # todo deal with recipients marked as bounced
    from dimagi.utils.django.email import get_valid_recipients
    recipient_list = get_valid_recipients(recipient_list)

    if not recipient_list:
        return
    try:
        send_mail(subject,
                  message,
                  from_email,
                  recipient_list,
                  fail_silently=fail_silently,
                  auth_user=auth_user,
                  auth_password=auth_password,
                  connection=connection)
    except Exception as e:
        notify_exception(None,
                         message="Encountered error while sending email",
                         details={
                             'subject': subject,
                             'recipients': ', '.join(recipient_list),
                             'error': e,
                         })
        self.retry(exc=e)
示例#3
0
def send_mail_async(self,
                    subject,
                    message,
                    from_email,
                    recipient_list,
                    messaging_event_id=None,
                    domain=None):
    """ Call with send_mail_async.delay(*args, **kwargs)
    - sends emails in the main celery queue
    - if sending fails, retry in 15 min
    - retry a maximum of 10 times
    """
    from corehq.util.soft_assert import soft_assert
    soft_assert('{}@dimagi.com'.format('skelly'))(all(
        recipient for recipient in recipient_list), 'Blank email addresses', {
            'subject': subject,
            'message': message,
            'recipients': recipient_list
        })

    recipient_list = [_f for _f in recipient_list if _f]

    # todo deal with recipients marked as bounced
    from dimagi.utils.django.email import get_valid_recipients, mark_local_bounced_email
    filtered_recipient_list = get_valid_recipients(recipient_list, domain)
    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)

    if not filtered_recipient_list:
        return

    headers = {}

    if settings.RETURN_PATH_EMAIL:
        headers['Return-Path'] = settings.RETURN_PATH_EMAIL

    if messaging_event_id is not None:
        headers[COMMCARE_MESSAGE_ID_HEADER] = messaging_event_id
    if settings.SES_CONFIGURATION_SET is not None:
        headers[SES_CONFIGURATION_SET_HEADER] = settings.SES_CONFIGURATION_SET

    try:
        message = EmailMessage(
            subject=subject,
            body=message,
            from_email=from_email,
            to=filtered_recipient_list,
            headers=headers,
        )
        return message.send()
    except SMTPDataError as e:
        # If the SES configuration has not been properly set up, resend the message
        if ("Configuration Set does not exist" in repr(e.smtp_error)
                and SES_CONFIGURATION_SET_HEADER in message.extra_headers):
            del message.extra_headers[SES_CONFIGURATION_SET_HEADER]
            message.send()
            notify_exception(None,
                             message="SES Configuration Set missing",
                             details={'error': e})
        else:
            raise
    except Exception as e:
        notify_exception(None,
                         message="Encountered error while sending email",
                         details={
                             'subject': subject,
                             'recipients': ', '.join(filtered_recipient_list),
                             'error': e,
                             'messaging_event_id': messaging_event_id,
                         })
        if messaging_event_id is not None:
            mark_subevent_gateway_error(messaging_event_id, e, retrying=True)
        try:
            self.retry(exc=e)
        except MaxRetriesExceededError:
            if messaging_event_id is not None:
                mark_subevent_gateway_error(messaging_event_id,
                                            e,
                                            retrying=False)