def fire_email_event(reminder, handler, recipients, verified_numbers,
                     logged_event):
    current_event = reminder.current_event
    case = reminder.case
    template_params = get_message_template_params(case)
    email_usage = EmailUsage.get_or_create_usage_record(reminder.domain)
    is_trial = domain_is_on_trial(reminder.domain)

    uses_custom_content_handler, content_handler = get_custom_content_handler(
        handler, logged_event)
    if uses_custom_content_handler and not content_handler:
        return

    for recipient in recipients:
        _add_recipient_to_template_params(recipient, template_params)
        logged_subevent = logged_event.create_subevent(handler, reminder,
                                                       recipient)

        try:
            lang = recipient.get_language_code()
        except Exception:
            lang = None

        if content_handler:
            subject, message = content_handler(reminder, handler, recipient)
        else:
            subject = current_event.subject.get(
                lang, current_event.subject[handler.default_lang])
            message = current_event.message.get(
                lang, current_event.message[handler.default_lang])
            try:
                subject = Message.render(subject, **template_params)
                message = Message.render(message, **template_params)
            except Exception:
                logged_subevent.error(
                    MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
                continue

        subject = subject or '(No Subject)'
        if message:
            try:
                email_address = recipient.get_email()
            except:
                email_address = None

            if email_address:
                if is_trial and EmailUsage.get_total_count(
                        reminder.domain) >= TRIAL_MAX_EMAILS:
                    logged_subevent.error(
                        MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
                    continue
                send_mail_async.delay(subject, message,
                                      settings.DEFAULT_FROM_EMAIL,
                                      [email_address])
                email_usage.update_count()
            else:
                logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
                continue

        logged_subevent.completed()
Example #2
0
    def send(self, recipient, logged_event, phone_entry=None):
        email_usage = EmailUsage.get_or_create_usage_record(logged_event.domain)
        is_trial = domain_is_on_trial(logged_event.domain)
        domain_obj = Domain.get_by_name(logged_event.domain)

        logged_subevent = logged_event.create_subevent_from_contact_and_content(
            recipient,
            self,
            case_id=self.case.case_id if self.case else None,
        )

        subject = self.get_translation_from_message_dict(
            domain_obj,
            self.subject,
            recipient.get_language_code()
        )

        message = self.get_translation_from_message_dict(
            domain_obj,
            self.message,
            recipient.get_language_code()
        )

        try:
            subject, message = self.render_subject_and_message(subject, message, recipient)
        except:
            logged_subevent.error(MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
            return

        subject = subject or '(No Subject)'
        if not message:
            logged_subevent.error(MessagingEvent.ERROR_NO_MESSAGE)
            return

        email_address = recipient.get_email()
        if not email_address:
            logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
            return

        if is_trial and EmailUsage.get_total_count(logged_event.domain) >= self.TRIAL_MAX_EMAILS:
            logged_subevent.error(MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
            return

        send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address], logged_subevent.id)

        email = Email(
            domain=logged_event.domain,
            date=datetime.utcnow(),
            couch_recipient_doc_type=logged_subevent.recipient_type,
            couch_recipient=logged_subevent.recipient_id,
            messaging_subevent_id=logged_subevent.pk,
            recipient_address=email_address,
            subject=subject,
            body=message,
        )
        email.save()

        email_usage.update_count()
Example #3
0
def fire_email_event(reminder, handler, recipients, verified_numbers, logged_event):
    current_event = reminder.current_event
    case = reminder.case
    template_params = get_message_template_params(case)
    email_usage = EmailUsage.get_or_create_usage_record(reminder.domain)
    is_trial = domain_is_on_trial(reminder.domain)

    uses_custom_content_handler, content_handler = get_custom_content_handler(handler, logged_event)
    if uses_custom_content_handler and not content_handler:
        return

    for recipient in recipients:
        logged_subevent = logged_event.create_subevent(handler, reminder, recipient)

        try:
            lang = recipient.get_language_code()
        except Exception:
            lang = None

        if content_handler:
            subject, message = content_handler(reminder, handler, recipient)
        else:
            subject = current_event.subject.get(lang, current_event.subject[handler.default_lang])
            message = current_event.message.get(lang, current_event.message[handler.default_lang])
            try:
                subject = Message.render(subject, **template_params)
                message = Message.render(message, **template_params)
            except Exception:
                logged_subevent.error(MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
                continue

        subject = subject or '(No Subject)'
        if message:
            try:
                email_address = recipient.get_email()
            except:
                email_address = None

            if email_address:
                if is_trial and EmailUsage.get_total_count(reminder.domain) >= TRIAL_MAX_EMAILS:
                    logged_subevent.error(MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
                    continue
                send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address])
                email_usage.update_count()
            else:
                logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
                continue

        logged_subevent.completed()
Example #4
0
    def send(self, recipient, logged_event, phone_entry=None):
        email_usage = EmailUsage.get_or_create_usage_record(logged_event.domain)
        is_trial = domain_is_on_trial(logged_event.domain)

        logged_subevent = logged_event.create_subevent_from_contact_and_content(
            recipient,
            self,
            case_id=self.case.case_id if self.case else None,
        )

        subject = self.get_translation_from_message_dict(
            logged_event.domain,
            self.subject,
            recipient.get_language_code()
        )

        message = self.get_translation_from_message_dict(
            logged_event.domain,
            self.message,
            recipient.get_language_code()
        )

        try:
            subject, message = self.render_subject_and_message(subject, message, recipient)
        except:
            logged_subevent.error(MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
            return

        subject = subject or '(No Subject)'
        if not message:
            logged_subevent.error(MessagingEvent.ERROR_NO_MESSAGE)
            return

        email_address = recipient.get_email()
        if not email_address:
            logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
            return

        if is_trial and EmailUsage.get_total_count(logged_event.domain) >= self.TRIAL_MAX_EMAILS:
            logged_subevent.error(MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
            return

        send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address])
        email_usage.update_count()
        logged_subevent.completed()
Example #5
0
    def send(self, recipient, logged_event):
        email_usage = EmailUsage.get_or_create_usage_record(logged_event.domain)
        is_trial = domain_is_on_trial(logged_event.domain)

        logged_subevent = logged_event.create_subevent_from_contact_and_content(
            recipient,
            self,
            case_id=self.case.case_id if self.case else None,
        )

        subject = self.get_translation_from_message_dict(
            logged_event.domain,
            self.subject,
            recipient.get_language_code()
        )

        message = self.get_translation_from_message_dict(
            logged_event.domain,
            self.message,
            recipient.get_language_code()
        )

        try:
            subject, message = self.render_subject_and_message(subject, message, recipient)
        except:
            logged_subevent.error(MessagingEvent.ERROR_CANNOT_RENDER_MESSAGE)
            return

        subject = subject or '(No Subject)'
        if not message:
            logged_subevent.error(MessagingEvent.ERROR_NO_MESSAGE)
            return

        email_address = recipient.get_email()
        if not email_address:
            logged_subevent.error(MessagingEvent.ERROR_NO_EMAIL_ADDRESS)
            return

        if is_trial and EmailUsage.get_total_count(logged_event.domain) >= self.TRIAL_MAX_EMAILS:
            logged_subevent.error(MessagingEvent.ERROR_TRIAL_EMAIL_LIMIT_REACHED)
            return

        send_mail_async.delay(subject, message, settings.DEFAULT_FROM_EMAIL, [email_address])
        email_usage.update_count()
        logged_subevent.completed()