Example #1
0
    def as_message(self, email):
        """
        Django email code relies on a combination of the EMAIL_BACKEND
        class itself and an instance of a message-style class that the
        backend uses to build the actual payload.

        This function is an opportunity for EMAIL_BACKENDs to hook in
        and create a Message object of its own designs.
        """
        from . import backends
        is_grapevine_backend = issubclass(self.kls, backends.base.GrapevineEmailBackend)
        implements_as_message = hasattr(self.kls, 'as_message')

        if is_grapevine_backend and implements_as_message:
            # Provider-specific EMAIL_BACKENDS will
            # likely implement this method and thus create their
            # own "message" obj
            msg = self.kls.as_message(email)
        else:
            # OTS Django or other SMTP-based EMAIL_BACKENDS will use
            # this standard functionality
            msg = EmailMultiAlternatives(
                subject=email.subject,
                body=email.text_body,
                from_email=email.from_email,
                to=email.to,
                cc=email.cc,
                bcc=email.bcc,
                headers=self.get_headers(email),
            )
            if email.html_body:
                msg.attach_alternative(email.html_body, "text/html")

        msg._email = email
        return msg
    def test_prepare_data(self):
        email_message = EmailMultiAlternatives(
            subject="Hello old friend!",
            body="Seriously, long time no see.",
            from_email="Marco Polo <*****@*****.**>",
            to=["*****@*****.**"],
            cc=[],
            bcc=["Top Secret <*****@*****.**>", "Top Secret2 <*****@*****.**>"],
            headers={},
        )
        email_message._email = Email().ensure_guid()

        data = MailGunEmailBackend().prepare_data(email_message)

        self.assertEquals(data["to"], "*****@*****.**")
        self.assertNotIn("cc", data.keys())
        self.assertEquals(data["bcc"], "Top Secret <*****@*****.**>, Top Secret2 <*****@*****.**>")
        self.assertEquals(data["from"], email_message.from_email)
Example #3
0
    def test_prepare_data(self):
        email_message = EmailMultiAlternatives(
            subject="Hello old friend!",
            body="Seriously, long time no see.",
            from_email="Marco Polo <*****@*****.**>",
            to=["*****@*****.**"],
            cc=[],
            bcc=[
                "Top Secret <*****@*****.**>", "Top Secret2 <*****@*****.**>"
            ],
            headers={},
        )
        email_message._email = Email().ensure_guid()

        data = MailGunEmailBackend().prepare_data(email_message)

        self.assertEquals(data["to"], "*****@*****.**")
        self.assertNotIn("cc", data.keys())
        self.assertEquals(
            data["bcc"],
            "Top Secret <*****@*****.**>, Top Secret2 <*****@*****.**>")
        self.assertEquals(data["from"], email_message.from_email)
Example #4
0
    def as_message(self, email):
        """
        Django email code relies on a combination of the EMAIL_BACKEND
        class itself and an instance of a message-style class that the
        backend uses to build the actual payload.

        This function is an opportunity for EMAIL_BACKENDs to hook in
        and create a Message object of its own designs.
        """
        from . import backends
        is_grapevine_backend = issubclass(self.kls,
                                          backends.base.GrapevineEmailBackend)
        implements_as_message = hasattr(self.kls, 'as_message')

        if is_grapevine_backend and implements_as_message:
            # Provider-specific EMAIL_BACKENDS will
            # likely implement this method and thus create their
            # own "message" obj
            msg = self.kls.as_message(email)
        else:
            # OTS Django or other SMTP-based EMAIL_BACKENDS will use
            # this standard functionality
            msg = EmailMultiAlternatives(
                subject=email.subject,
                body=email.text_body,
                from_email=email.from_email,
                to=email.to,
                cc=email.cc,
                bcc=email.bcc,
                headers=self.get_headers(email),
            )
            if email.html_body:
                msg.attach_alternative(email.html_body, "text/html")

        msg._email = email
        return msg