Example #1
0
    def test_attach_as_html(self):
        from tgext.mailer.message import Message
        from tgext.mailer.message import Attachment

        charset = 'iso-8859-1'
        text_encoded = b'LaPe\xf1a'
        html_encoded = b'<p>' + text_encoded + b'</p>'
        html_text = html_encoded.decode(charset)
        transfer_encoding = 'quoted-printable'
        html = Attachment(
            data=html_text,
            transfer_encoding=transfer_encoding
            )
        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            recipients=["*****@*****.**"],
            html=html,
            )

        html_part = msg.to_message()

        self.assertEqual(
            html_part['Content-Type'],
            'text/html; charset="iso-8859-1"'
            )
        self.assertEqual(
            html_part['Content-Transfer-Encoding'],
            transfer_encoding
            )
        self.assertEqual(
            html_part.get_payload(),
            _qencode(html_encoded).decode('ascii')
            )
Example #2
0
 def test_to_message_multipart_with_qpencoding(self):
     from tgext.mailer.message import Message, Attachment
     response = Message(
         recipients=['To'],
         sender='From',
         subject='Subject',
         body='Body',
         html='Html'
         )
     this = os.path.abspath(__file__)
     with open(this, 'rb') as data:
         attachment = Attachment(
             filename=this,
             content_type='application/octet-stream',
             disposition='disposition',
             transfer_encoding='quoted-printable',
             data=data,
             )
         response.attach(attachment)
         message = response.to_message()
         payload = message.get_payload()[1]
     self.assertEqual(
         payload.get('Content-Transfer-Encoding'),
         'quoted-printable'
         )
     self.assertEqual(
         payload.get_payload(),
         _qencode(self._read_filedata(this,'rb')).decode('ascii')
         )
Example #3
0
    def test_attach(self):

        from tgext.mailer.message import Message
        from tgext.mailer.message import Attachment

        msg = Message(
            subject="testing",
            recipients=["*****@*****.**"],
            sender='sender',
            body="testing"
            )

        attachment = Attachment(
            data=b"this is a test",
            content_type="text/plain"
            )

        msg.attach(attachment)

        a = msg.attachments[0]

        self.assertTrue(a.filename is None)
        self.assertEqual(a.disposition, 'attachment')
        self.assertEqual(a.content_type, "text/plain")
        self.assertEqual(a.data, b"this is a test")
Example #4
0
    def test_attach_as_body(self):
        from tgext.mailer.message import Message
        from tgext.mailer.message import Attachment

        charset = 'iso-8859-1'
        text_encoded = b'LaPe\xf1a'
        text = text_encoded.decode(charset)
        transfer_encoding = 'quoted-printable'
        body = Attachment(
            data=text,
            transfer_encoding=transfer_encoding
            )
        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            recipients=["*****@*****.**"],
            body=body
            )
        body_part = msg.to_message()

        self.assertEqual(
            body_part['Content-Type'],
            'text/plain; charset="iso-8859-1"'
            )
        self.assertEqual(
            body_part['Content-Transfer-Encoding'],
            transfer_encoding
            )
        self.assertEqual(
            body_part.get_payload(),
            _qencode(text_encoded).decode('ascii')
            )
Example #5
0
    def test_attach_as_body_and_html_utf8(self):
        from tgext.mailer.message import Message
        from tgext.mailer.message import Attachment

        charset = 'utf-8'
        # greek small letter iota with dialtyika and tonos; this character
        # cannot be encoded to either ascii or latin-1, so utf-8 is chosen
        text_encoded =  b'\xce\x90'
        text = text_encoded.decode(charset)
        html_encoded = b'<p>' + text_encoded + b'</p>'
        html_text = html_encoded.decode(charset)
        transfer_encoding = 'quoted-printable'
        body = Attachment(
            data=text,
            transfer_encoding=transfer_encoding,
            )
        html = Attachment(
            data=html_text,
            transfer_encoding=transfer_encoding,
            )
        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            recipients=["*****@*****.**"],
            body=body,
            html=html,
            )
        message = msg.to_message()
        body_part, html_part = message.get_payload()

        self.assertEqual(
            body_part['Content-Type'],
            'text/plain; charset="utf-8"'
            )
        
        self.assertEqual(
            body_part['Content-Transfer-Encoding'],
            transfer_encoding
            )

        payload = body_part.get_payload()
        self.assertEqual(
            payload,
            _qencode(text_encoded).decode('ascii')
            )

        self.assertEqual(
            html_part['Content-Type'],
            'text/html; charset="utf-8"'
            )
        self.assertEqual(
            html_part['Content-Transfer-Encoding'],
            transfer_encoding
            )
        payload = html_part.get_payload()
        self.assertEqual(
            payload,
            _qencode(html_encoded).decode('ascii')
            )
Example #6
0
    def test_add_bcc(self):

        from tgext.mailer.message import Message

        msg = Message("testing")
        msg.add_bcc("*****@*****.**")

        self.assertEqual(msg.bcc, ["*****@*****.**"])
Example #7
0
    def test_add_recipient(self):

        from tgext.mailer.message import Message

        msg = Message("testing")
        msg.add_recipient("*****@*****.**")

        self.assertEqual(msg.recipients, ["*****@*****.**"])
Example #8
0
    def test_is_bad_headers_if_subject_empty(self):
        from tgext.mailer.message import Message
        msg = Message(
            sender="*****@*****.**",
            body="testing",
            recipients=["*****@*****.**"]
            )

        self.assertFalse((msg.is_bad_headers()))
Example #9
0
    def test_is_bad_headers_if_bad_headers(self):

        from tgext.mailer.message import Message
        msg = Message(
            subject="testing\n\r",
            sender="from@\nexample.com",
            body="testing",
            recipients=["*****@*****.**"]
            )

        self.assertTrue(msg.is_bad_headers())
Example #10
0
 def test_to_message_multiple_to_recipients(self):
     from tgext.mailer.message import Message
     response = Message(
         subject="Subject",
         sender="From",
         recipients=["*****@*****.**", "*****@*****.**"],
         body="Body",
         html="Html",
         )
     message = response.to_message()
     self.assertEqual(text_type(message['To']),
                      '[email protected], [email protected]')
Example #11
0
    def test_cc_without_recipients_2(self):

        from tgext.mailer.message import Message

        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            body="testing",
            cc=["*****@*****.**"]
            )
        response = msg.to_message()
        self.assertTrue("Cc: [email protected]" in text_type(response))
Example #12
0
    def test_message_is_quoted_printable_with_text_body(self):
        from tgext.mailer.message import Message

        msg = Message(
            recipients=['*****@*****.**'],
            subject="testing",
            sender="*****@*****.**",
            body="THISSHOULDBEINMESSAGEBODY",
            )

        response = msg.to_message()
        self.assertTrue("THISSHOULDBEINMESSAGEBODY" in text_type(response))
Example #13
0
    def test_extra_headers(self):

        from tgext.mailer.message import Message

        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            recipients=["*****@*****.**"],
            body="testing",
            extra_headers=[('X-Foo', 'Joe')]
            )

        response = msg.to_message()
        self.assertTrue("X-Foo: Joe" in text_type(response))
Example #14
0
    def test_bcc_without_recipients(self):

        from tgext.mailer.message import Message
        from tgext.mailer.mailer import Mailer

        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            body="testing",
            bcc=["*****@*****.**"]
            )
        mailer = Mailer()
        msgid = mailer.send(msg)
        response = msg.to_message()

        self.assertFalse(
            "Bcc: [email protected]" in text_type(response))
        self.assertTrue(msgid)
Example #15
0
    def test_send_without_body(self):

        from tgext.mailer.message import Message
        from tgext.mailer.mailer import Mailer
        from tgext.mailer.exceptions import InvalidMessage

        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            recipients=["*****@*****.**"]
            )

        mailer = Mailer()

        self.assertRaises(InvalidMessage, mailer.send, msg)

        msg.html = "<b>test</b>"

        mailer.send(msg)
Example #16
0
def _makeMessage(subject="testing",
                sender="*****@*****.**",
                recipients=["*****@*****.**"],
                body="test",
                **kw):
    from tgext.mailer.message import Message
    return Message(subject=subject,
                   sender=sender,
                   recipients=recipients,
                   body=body,
                   **kw)
Example #17
0
    def test_recipients_properly_initialized(self):

        from tgext.mailer.message import Message

        msg = Message(subject="subject")

        self.assertEqual(msg.recipients, [])

        msg2 = Message(subject="subject")
        msg2.add_recipient("*****@*****.**")

        self.assertEqual(len(msg.recipients), 0)

        msg3 = Message(subject="subject")
        msg3.add_recipient("*****@*****.**")

        self.assertEqual(len(msg.recipients), 0)
Example #18
0
    def test_initialize(self):

        from tgext.mailer.message import Message

        msg = Message(
            subject="subject",
            sender="*****@*****.**",
            recipients=["*****@*****.**"]
            )

        self.assertEqual(msg.subject, "subject")
        self.assertEqual(msg.sender, "*****@*****.**")
        self.assertEqual(msg.recipients, ["*****@*****.**"])
Example #19
0
    def test_to_message_multipart(self):
        from tgext.mailer.message import Message, Attachment
        response = Message(
            recipients=['To'],
            sender='From',
            subject='Subject',
            body='Body',
            html='Html'
            )
        attachment_type = 'text/html'
        this = os.path.abspath(__file__)
        attachment = Attachment(
            filename=this,
            content_type=attachment_type,
            data='data'.encode('ascii'),
            disposition='disposition'
            )
        response.attach(attachment)
        message = response.to_message()
        self.assertEqual(message['Content-Type'], 'multipart/mixed')

        payload = message.get_payload()
        self.assertEqual(len(payload), 2)
        self.assertEqual(
            payload[0]['Content-Type'],
            'multipart/alternative'
            )
        self.assertTrue(
            payload[1]['Content-Type'].startswith(attachment_type)
            )

        alt_payload = payload[0].get_payload()
        self.assertTrue(
            alt_payload[0]['Content-Type'].startswith('text/plain')
            )
        self.assertTrue(
            alt_payload[1]['Content-Type'].startswith('text/html')
            )
Example #20
0
 def test_to_message_with_html_attachment(self):
     from tgext.mailer.message import Message, Attachment
     response = Message(
         recipients=['To'],
         sender='From',
         subject='Subject',
         body='Body',
         )
     attachment = Attachment(
         data=b'data',
         content_type='text/html'
         )
     response.attach(attachment)
     message = response.to_message()
     att_payload = message.get_payload()[1]
     self.assertEqual(
         att_payload['Content-Type'],
         'text/html; charset="us-ascii"'
         )
     self.assertEqual(
         att_payload.get_payload(),
         _bencode(b'data').decode('ascii')
         )
Example #21
0
 def test_to_message_with_binary_attachment(self):
     from tgext.mailer.message import Message, Attachment
     response = Message(
         recipients=['To'],
         sender='From',
         subject='Subject',
         body='Body',
         )
     data = os.urandom(100)
     attachment = Attachment(
         data=data,
         content_type='application/octet-stream',
         )
     response.attach(attachment)
     message = response.to_message()
     att_payload = message.get_payload()[1]
     self.assertEqual(
         att_payload['Content-Type'],
         'application/octet-stream'
         )
     self.assertEqual(
         att_payload.get_payload(),
         _bencode(data).decode('ascii')
         )
Example #22
0
    def test_send_without_recipients(self):

        from tgext.mailer.message import Message
        from tgext.mailer.mailer import Mailer
        from tgext.mailer.exceptions import InvalidMessage

        mailer = Mailer()

        msg = Message(
            subject="testing",
            recipients=[],
            body="testing"
            )

        self.assertRaises(InvalidMessage, mailer.send, msg)
Example #23
0
    def test_bad_header_sender(self):

        from tgext.mailer.message import Message
        from tgext.mailer.mailer import Mailer

        from tgext.mailer.exceptions import BadHeaders

        mailer = Mailer()

        msg = Message(
            subject="testing",
            sender="[email protected]\n\r",
            recipients=["*****@*****.**"],
            body="testing"
            )
        self.assertRaises(BadHeaders, mailer.send, msg)
Example #24
0
    def test_send_to(self):

        from tgext.mailer.message import Message

        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            recipients=[
                "*****@*****.**"],
            cc=['*****@*****.**',
                '*****@*****.**'],
            bcc=['*****@*****.**'],
            body="testing"
            )
        
        self.assertEqual(
            msg.send_to,
            set(["*****@*****.**",
                 "*****@*****.**",
                 "*****@*****.**"])
            )
Example #25
0
    def test_repoze_sendmail_send_to_queue_functional(self):
        # functest that emulates the interaction between tgext.mailer and
        # repoze.maildir.add and queuedelivery.send.
        
        import tempfile
        from email.generator import Generator
        from email.parser import Parser
        from tgext.mailer.message import Message
        from tgext.mailer.message import Attachment
        from repoze.sendmail.encoding import cleanup_message
        from repoze.sendmail.delivery import copy_message

        def checkit(msg):
            self.assertEqual(
                msg['Content-Type'],
                'text/plain; charset="iso-8859-1"'
                )
            self.assertEqual(
                msg['Content-Transfer-Encoding'], transfer_encoding)

            payload = msg.get_payload()
            self.assertEqual(payload, expected)

        charset = 'iso-8859-1'
        text_encoded = b'LaPe\xf1a'
        text = text_encoded.decode(charset)
        expected = _qencode(text_encoded).decode('ascii')
        transfer_encoding = 'quoted-printable'
        body = Attachment(
            data=text,
            transfer_encoding=transfer_encoding
            )
        msg = Message(
            subject="testing",
            sender="*****@*****.**",
            recipients=["*****@*****.**"],
            body=body
            )

        # done in tgext.mailer via mailer/send_to_queue
        msg = msg.to_message()
        msg.as_string()

        checkit(msg)

        # done in repoze.sendmail via delivery/AbstractMailDelivery/send
        cleanup_message(msg)

        checkit(msg)

        # done in repoze.sendmail via
        # delivery/AbstractMailDelivery/createDataManager
        msg_copy = copy_message(msg)

        checkit(msg_copy)

        try:
            # emulate what repoze.sendmail maildir.py/add does
            fn = tempfile.mktemp()
            fd = os.open(fn,
                         os.O_CREAT|os.O_EXCL|os.O_WRONLY,
                         0o600
                         )
            with os.fdopen(fd, 'w') as f:
                writer = Generator(f)
                writer.flatten(msg_copy)

            # emulate what repoze.sendmail.queue _parseMessage does
            with open(fn) as foo:
                parser = Parser()
                reconstituted = parser.parse(foo)
                checkit(reconstituted)
                
        finally: # pragma: no cover
            try:
                os.remove(fn)
            except:
                pass