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_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') )
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') )
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') )
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))
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]')
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))
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))
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)
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') )
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