def send_notifications(self, instance, form, request=None): recipients = [] emails = [] if not MANDRILL: try: connection = get_connection(fail_silently=False) connection.open() except: # noqa # I use a "catch all" in order to not couple this handler to a specific email backend # different email backends have different exceptions. logger.exception("Could not send notification emails.") return [] conditionals = self.get_conditionals(instance, form, 'email') if conditionals: for conditional in conditionals: emails.append(conditional.prepare_email(form=form)) recipients.append(parseaddr(conditional.action_value)) redirect_emails = self.get_conditionals(instance, form, 'redirect-email') if redirect_emails: for conditional in redirect_emails: emails.append(conditional.prepare_email(form=form)) recipients.append(parseaddr(conditional.action_value)) else: recipients = super(EmailNotificationForm, self).send_notifications( instance, form, request) notifications = instance.email_notifications.select_related('form') for notification in notifications: email = notification.prepare_email(form=form) copy_email = notification.prepare_copy_email(form=form) to_email = email['to'][0]['email'] if MANDRILL else email.to[0] to_copy_email = copy_email['to'][0][ 'email'] if MANDRILL else copy_email.to[0] if is_valid_recipient(to_email): emails.append(email) recipients.append(parseaddr(to_email)) if to_email != to_copy_email and is_valid_recipient( to_copy_email): emails.append(copy_email) recipients.append(parseaddr(to_copy_email)) if MANDRILL: for email in emails: send_constructed_mail(email, MANDRILL_DEFAULT_TEMPLATE) return recipients else: try: connection.send_messages(emails) except: # noqa # again, we catch all exceptions to be backend agnostic logger.exception("Could not send notification emails.") recipients = [] return recipients
def send_notifications(self, instance, form): try: connection = get_connection(fail_silently=False) connection.open() except: # I use a "catch all" in order to not couple this handler to a specific email backend # different email backends have different exceptions. logger.exception("Could not send notification emails.") return [] notifications = instance.email_notifications.select_related('form') emails = [] recipients = [] for notification in notifications: email = notification.prepare_email(form=form) to_email = email.to[0] if is_valid_recipient(to_email): emails.append(email) recipients.append(parseaddr(to_email)) try: connection.send_messages(emails) except: # again, we catch all exceptions to be backend agnostic logger.exception("Could not send notification emails.") recipients = [] return recipients
def get_conditionals(self, instance, form, action_type): conditionals = [] form_data = form.get_serialized_field_dict() for c in instance.conditionals.select_related('form'): value = form_data.get(c.field_name) if c.action_type == action_type and value and c.field_value in value.split( ', '): if action_type in ['email', 'redirect-email']: if is_valid_recipient(c.action_value): conditionals.append(c) else: conditionals.append(c) return conditionals
def send_notifications(self, instance, form): notifications = instance.email_notifications.select_related('form') emails = [] recipients = [] for notification in notifications: email = notification.prepare_email(form=form) to_email = email.to[0] if is_valid_recipient(to_email): emails.append(email) recipients.append(parseaddr(to_email)) try: with get_connection(fail_silently=False) as connection: connection.send_messages(emails) except: # noqa # we catch all exceptions to be backend agnostic logger.exception("Could not send notification emails.") recipients = [] return recipients