コード例 #1
0
def send_new_answer_payload(sender, instance, created, **kwargs):
    answer = instance
    writeitinstance = answer.message.writeitinstance

    if created:
        connection = writeitinstance.config.get_mail_connection()
        new_answer_template = writeitinstance.new_answer_notification_template

        with override(None, deactivate=True):
            message_url = reverse('thread_read', subdomain=writeitinstance.slug, kwargs={'slug': answer.message.slug})

        context = {
            'author_name': answer.message.author_name,
            'person': answer.person.name,
            'subject': answer.message.subject,
            'content': answer.content,
            'message_url': message_url,
            'site_name': answer.message.writeitinstance.name,
            }

        subject = new_answer_template.get_subject_template().format(**context)
        text_content = new_answer_template.get_content_template().format(**context)
        html_content = new_answer_template.template_html.format(**escape_dictionary_values(context))

        if settings.SEND_ALL_EMAILS_FROM_DEFAULT_FROM_EMAIL:
            from_email = settings.DEFAULT_FROM_EMAIL
        else:
            from_domain = writeitinstance.config.custom_from_domain or settings.DEFAULT_FROM_DOMAIN
            from_email = "%s@%s" % (
                writeitinstance.slug,
                from_domain,
                )

        subscribers = answer.message.subscribers.all()

        if writeitinstance.config.notify_owner_when_new_answer:
            subscribers = chain(subscribers, (writeitinstance.owner,))

        for subscriber in subscribers:
            msg = EmailMultiAlternatives(
                subject.strip(),
                text_content,
                from_email,
                [subscriber.email],
                connection=connection,
                )
            if html_content:
                msg.attach_alternative(html_content, "text/html")
            msg.send()

        # Webhooks
        payload = {
            'message_id': '/api/v1/message/{0}/'.format(answer.message.id),
            'content': answer.content,
            'person': answer.person.name,
            'person_id': answer.person.popit_url,
            }

        for webhook in writeitinstance.answer_webhooks.all():
            requests.post(webhook.url, data=payload)
コード例 #2
0
    def send(self, outbound_message):
        # Here there should be somewhere the contacts
        # Returns a tuple with the result_of_sending, fatal_error
        # so False, True means that there was an error sending and you should not try again
        try:
            writeitinstance = outbound_message.message.writeitinstance
            template = writeitinstance.mailit_template
        except:
            return False, False

        author_name = outbound_message.message.author_name
        context = {
            'subject': outbound_message.message.subject,
            'content': outbound_message.message.content,
            'content_indented': process_content(outbound_message.message.content),
            'person': outbound_message.contact.person.name,
            'author': author_name,
            'site_url': writeitinstance.get_absolute_url(),
            'message_url': outbound_message.message.get_absolute_url(),
            'site_name': writeitinstance.name,
            'owner_email': writeitinstance.owner.email,
            }
        try:
            text_content = template.get_content_template().format(**context)
            html_content = template.content_html_template.format(**escape_dictionary_values(context))
            subject = template.subject_template.format(**context)
        except KeyError, error:
            log = "Error with templates for instance %(instance)s and the error was '%(error)s'"
            log = log % {
                'instance': writeitinstance.name,
                'error': error.__unicode__()
                }
            mail_admins("Problem sending an email", log)
            logging.info(log)
            return False, True
コード例 #3
0
ファイル: __init__.py プロジェクト: schlos/write-it
    def send(self, outbound_message):
        # Here there should be somewhere the contacts
        # Returns a tuple with the result_of_sending, fatal_error
        # so False, True means that there was an error sending and you should not try again
        try:
            writeitinstance = outbound_message.message.writeitinstance
            template = writeitinstance.mailit_template
        except:
            return False, False

        author_name = outbound_message.message.author_name
        context = {
            'subject': outbound_message.message.subject,
            'content': outbound_message.message.content,
            'content_indented': process_content(outbound_message.message.content),
            'person': outbound_message.contact.person.name,
            'author': author_name,
            'writeit_url': writeitinstance.get_absolute_url(),
            'message_url': outbound_message.message.get_absolute_url(),
            'writeit_name': writeitinstance.name,
            'owner_email': writeitinstance.owner.email,
            }
        text_content = template.get_content_template().format(**context)
        html_content = template.content_html_template.format(**escape_dictionary_values(context))
        subject = template.subject_template.format(**context)

        if settings.SEND_ALL_EMAILS_FROM_DEFAULT_FROM_EMAIL:
            from_email = author_name + " <" + settings.DEFAULT_FROM_EMAIL + ">"
        else:
            from_domain = writeitinstance.config.custom_from_domain or settings.DEFAULT_FROM_DOMAIN
            from_email = (
                author_name + " <" + writeitinstance.slug +
                "+" + outbound_message.outboundmessageidentifier.key +
                '@' + from_domain + ">"
                )

        # There there should be a try and except looking
        # for errors and stuff
        try:
            to_email = writeitinstance.owner.email if writeitinstance.config.testing_mode else outbound_message.contact.value

            msg = EmailMultiAlternatives(
                subject,
                text_content,
                from_email,
                [to_email],
                connection=writeitinstance.config.get_mail_connection(),
                )
            if html_content:
                msg.attach_alternative(html_content, "text/html")
            msg.send(fail_silently=False)
            log = "Mail sent from %(from)s to %(to)s"

            log = log % {
                'from': from_email,
                'to': outbound_message.contact.value,
                }
            logging.info(log)
        except SMTPServerDisconnected, e:
            return False, False
コード例 #4
0
def send_confirmation_email(sender, instance, created, **kwargs):
    confirmation = instance
    if created:
        confirmation_url = reverse(
            'confirm',
            subdomain=confirmation.message.writeitinstance.slug,
            kwargs={'slug': confirmation.key},
        )
        message_full_url = confirmation.message.get_absolute_url()
        plaintext = confirmation.message.writeitinstance.confirmationtemplate.get_content_template(
        )
        htmly = confirmation.message.writeitinstance.confirmationtemplate.content_html
        subject = confirmation.message.writeitinstance.confirmationtemplate.get_subject_template(
        )
        subject = subject.rstrip()

        context = {
            'author_name': confirmation.message.author_name,
            'site_name': confirmation.message.writeitinstance.name,
            'subject': confirmation.message.subject,
            'content': confirmation.message.content,
            'recipients':
            u', '.join([x.name for x in confirmation.message.people]),
            'confirmation_url': confirmation_url,
            'message_url': message_full_url,
        }

        text_content = template_with_wrap(plaintext, context)
        subject = subject.format(**context)
        html_content = htmly.format(**escape_dictionary_values(context))

        if settings.SEND_ALL_EMAILS_FROM_DEFAULT_FROM_EMAIL:
            from_email = settings.DEFAULT_FROM_EMAIL
        else:
            from_domain = confirmation.message.writeitinstance.config.custom_from_domain\
                or settings.DEFAULT_FROM_DOMAIN
            from_email = "%s@%s" % (
                confirmation.message.writeitinstance.slug,
                from_domain,
            )
        connection = confirmation.message.writeitinstance.config.get_mail_connection(
        )

        msg = EmailMultiAlternatives(
            subject,
            text_content,
            from_email,
            [confirmation.message.author_email],
            connection=connection,
        )

        if html_content:
            msg.attach_alternative(html_content, "text/html")

        try:
            msg.send()
        except:
            pass
コード例 #5
0
ファイル: models.py プロジェクト: mysociety/write-it
def send_confirmation_email(sender, instance, created, **kwargs):
    confirmation = instance
    if created:
        confirmation_url = reverse(
            'confirm',
            subdomain=confirmation.message.writeitinstance.slug,
            kwargs={'slug': confirmation.key},
        )
        message_full_url = confirmation.message.get_absolute_url()
        plaintext = confirmation.message.writeitinstance.confirmationtemplate.get_content_template()
        htmly = confirmation.message.writeitinstance.confirmationtemplate.content_html
        subject = confirmation.message.writeitinstance.confirmationtemplate.get_subject_template()
        subject = subject.rstrip()

        context = {
            'author_name': confirmation.message.author_name_for_display,
            'site_name': confirmation.message.writeitinstance.name,
            'subject': confirmation.message.subject,
            'content': confirmation.message.content,
            'recipients': u', '.join([x.name for x in confirmation.message.people]),
            'confirmation_url': confirmation_url,
            'message_url': message_full_url,
            }

        text_content = template_with_wrap(plaintext, context)
        subject = subject.format(**context)
        html_content = htmly.format(**escape_dictionary_values(context))

        if settings.SEND_ALL_EMAILS_FROM_DEFAULT_FROM_EMAIL:
            from_email = settings.DEFAULT_FROM_EMAIL
        else:
            from_domain = confirmation.message.writeitinstance.config.custom_from_domain\
                or settings.DEFAULT_FROM_DOMAIN
            from_email = "%s@%s" % (
                confirmation.message.writeitinstance.slug,
                from_domain,
                )
        real_name = \
            confirmation.message.writeitinstance.config.real_name_for_site_emails
        if real_name:
            from_email = u'{real_name} <{email}>'.format(
                real_name=real_name, email=from_email)

        connection = confirmation.message.writeitinstance.config.get_mail_connection()

        msg = EmailMultiAlternatives(
            subject,
            text_content,
            from_email,
            [confirmation.message.author_email],
            connection=connection,
            )

        if html_content:
            msg.attach_alternative(html_content, "text/html")

        msg.send()
コード例 #6
0
    def send(self, outbound_message):
        # Here there should be somewhere the contacts
        # Returns a tuple with the result_of_sending, fatal_error
        # so False, True means that there was an error sending and you should not try again
        try:
            writeitinstance = outbound_message.message.writeitinstance
            template = writeitinstance.mailit_template
        except:
            return False, False

        with override(writeitinstance.config.default_language):
            author_name = outbound_message.message.author_name
            context = {
                'subject':
                outbound_message.message.subject,
                'content':
                outbound_message.message.content,
                'content_indented':
                process_content(outbound_message.message.content),
                'person':
                outbound_message.contact.person.name,
                'author':
                author_name,
                'site_url':
                writeitinstance.get_absolute_url(),
                'message_url':
                outbound_message.message.get_absolute_url(),
                'site_name':
                writeitinstance.name,
                'owner_email':
                writeitinstance.owner.email,
            }
            try:
                text_content = template.get_content_template().format(
                    **context)
                html_content = template.content_html_template.format(
                    **escape_dictionary_values(context))
                subject = template.subject_template.format(**context)
            except KeyError, error:
                log = "Error with templates for instance %(instance)s and the error was '%(error)s'"
                log = log % {
                    'instance': writeitinstance.name,
                    'error': error.__unicode__()
                }
                mail_admins("Problem sending an email", log)
                logging.info(log)
                return False, True
コード例 #7
0
def send_new_answer_payload(sender, instance, created, **kwargs):
    answer = instance
    writeitinstance = answer.message.writeitinstance

    if created:
        connection = writeitinstance.config.get_mail_connection()
        new_answer_template = writeitinstance.new_answer_notification_template

        with override(None, deactivate=True):
            message_url = reverse('thread_read',
                                  subdomain=writeitinstance.slug,
                                  kwargs={'slug': answer.message.slug})

        context = {
            'author_name': answer.message.author_name,
            'person': answer.person.name,
            'subject': answer.message.subject,
            'content': answer.content,
            'message_url': message_url,
            'site_name': answer.message.writeitinstance.name,
        }

        subject = new_answer_template.get_subject_template().format(**context)
        text_content = new_answer_template.get_content_template().format(
            **context)
        html_content = new_answer_template.template_html.format(
            **escape_dictionary_values(context))

        if settings.SEND_ALL_EMAILS_FROM_DEFAULT_FROM_EMAIL:
            from_email = settings.DEFAULT_FROM_EMAIL
        else:
            from_domain = writeitinstance.config.custom_from_domain or settings.DEFAULT_FROM_DOMAIN
            from_email = "%s@%s" % (
                writeitinstance.slug,
                from_domain,
            )

        subscribers = answer.message.subscribers.all()

        if writeitinstance.config.notify_owner_when_new_answer:
            subscribers = chain(subscribers, (writeitinstance.owner, ))

        for subscriber in subscribers:
            msg = EmailMultiAlternatives(
                subject.strip(),
                text_content,
                from_email,
                [subscriber.email],
                connection=connection,
            )
            if html_content:
                msg.attach_alternative(html_content, "text/html")
            msg.send()

        # Webhooks
        payload = {
            'message_id': '/api/v1/message/{0}/'.format(answer.message.id),
            'content': answer.content,
            'person': answer.person.name,
            'person_id': answer.person.popit_url,
        }

        for webhook in writeitinstance.answer_webhooks.all():
            requests.post(webhook.url, data=payload)