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') )
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")
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') )
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') )
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') )