def sendgrid_handler(self, queue_message, to_addrs_to_email_messages_map):
        self.logger.info(
            "Sending account:%s policy:%s %s:%s email:%s to %s" %
            (queue_message.get('account', ''), queue_message['policy']['name'],
             queue_message['policy']['resource'],
             str(len(queue_message['resources'])), queue_message['action'].get(
                 'template', 'default'), to_addrs_to_email_messages_map))

        from_email = Email(self.config['from_address'])
        subject = get_message_subject(queue_message)
        email_format = queue_message['action'].get('template_format', None)
        if not email_format:
            email_format = queue_message['action'].get(
                'template', 'default').endswith('html') and 'html' or 'plain'

        for email_to_addrs, email_content in six.iteritems(
                to_addrs_to_email_messages_map):
            for to_address in email_to_addrs:
                to_email = Email(to_address)
                content = Content("text/" + email_format, email_content)
                mail = Mail(from_email, subject, to_email, content)
                try:
                    self.sendgrid_client.client.mail.send.post(
                        request_body=mail.get())
                except (exceptions.UnauthorizedError,
                        exceptions.BadRequestsError) as e:
                    self.logger.warning(
                        "\n**Error \nPolicy:%s \nAccount:%s \nSending to:%s \n\nRequest body:"
                        "\n%s\n\nRequest headers:\n%s\n\n mailer.yml: %s" %
                        (queue_message['policy'],
                         queue_message.get('account', ''), email_to_addrs,
                         e.body, e.headers, self.config))
                    return False
        return True
    def sendgrid_handler(self, queue_message, to_addrs_to_email_messages_map):
        self.logger.info("Sending account:%s policy:%s %s:%s email:%s to %s" % (
            queue_message.get('account', ''),
            queue_message['policy']['name'],
            queue_message['policy']['resource'],
            str(len(queue_message['resources'])),
            queue_message['action'].get('template', 'default'),
            to_addrs_to_email_messages_map))

        from_address = self.config.get('from_address', '')
        subject = get_message_subject(queue_message)
        email_format = queue_message['action'].get('template_format', None)
        if not email_format:
            email_format = queue_message['action'].get(
                'template', 'default').endswith('html') and 'html' or 'plain'

        for email_to_addrs, message in six.iteritems(to_addrs_to_email_messages_map):
            for to_address in email_to_addrs:
                if email_format == "html":
                    mail = Mail(
                        from_email=From(from_address),
                        to_emails=To(to_address),
                        subject=subject,
                        html_content=message.as_string())
                else:
                    mail = Mail(
                        from_email=From(from_address),
                        to_emails=To(to_address),
                        subject=subject,
                        plain_text_content=message.as_string())
                try:
                    self.sendgrid_client.send(mail)
                except (exceptions.UnauthorizedError, exceptions.BadRequestsError) as e:
                    self.logger.warning(
                        "\n**Error \nPolicy:%s \nAccount:%s \nSending to:%s \n\nRequest body:"
                        "\n%s\n\nRequest headers:\n%s\n\n mailer.yml: %s" % (
                            queue_message['policy'],
                            queue_message.get('account', ''),
                            email_to_addrs,
                            e.body,
                            e.headers,
                            self.config
                        )
                    )
                    return False
        return True
    def sendgrid_handler(self, queue_message, to_addrs_to_email_messages_map):
        self.logger.info("Sending account:%s policy:%s %s:%s email:%s to %s" % (
            queue_message.get('account', ''),
            queue_message['policy']['name'],
            queue_message['policy']['resource'],
            str(len(queue_message['resources'])),
            queue_message['action'].get('template', 'default'),
            to_addrs_to_email_messages_map))

        from_email = Email(self.config.get('from_address', ''))
        subject = get_message_subject(queue_message)
        email_format = queue_message['action'].get('template_format', None)
        if not email_format:
            email_format = queue_message['action'].get(
                'template', 'default').endswith('html') and 'html' or 'plain'

        for email_to_addrs, email_content in six.iteritems(to_addrs_to_email_messages_map):
            for to_address in email_to_addrs:
                to_email = Email(to_address)
                content = Content("text/" + email_format, email_content)
                mail = Mail(from_email, subject, to_email, content)
                try:
                    self.sendgrid_client.client.mail.send.post(request_body=mail.get())
                except (exceptions.UnauthorizedError, exceptions.BadRequestsError) as e:
                    self.logger.warning(
                        "\n**Error \nPolicy:%s \nAccount:%s \nSending to:%s \n\nRequest body:"
                        "\n%s\n\nRequest headers:\n%s\n\n mailer.yml: %s" % (
                            queue_message['policy'],
                            queue_message.get('account', ''),
                            email_to_addrs,
                            e.body,
                            e.headers,
                            self.config
                        )
                    )
                    return False
        return True
예제 #4
0
 def test_get_message_subject(self):
     subject = utils.get_message_subject(SQS_MESSAGE_1)
     self.assertEqual(subject,
     SQS_MESSAGE_1['action']['subject'].replace('{{ account }}', SQS_MESSAGE_1['account']))