def test_attach_as_body_and_html_latin1(self): from pyramid_mailer.message import Message from pyramid_mailer.message import Attachment charset = 'iso-8859-1' text_encoded = b'LaPe\xf1a' 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="iso-8859-1"') 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="iso-8859-1"') self.assertEqual(html_part['Content-Transfer-Encoding'], transfer_encoding) payload = html_part.get_payload() self.assertEqual(payload, _qencode(html_encoded).decode('ascii'))
def test_attach_as_body_and_html_utf8(self): from pyramid_mailer.message import Message from pyramid_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_attach_as_body_and_html_latin1(self): from pyramid_mailer.message import Message from pyramid_mailer.message import Attachment charset = "iso-8859-1" text_encoded = b"LaPe\xf1a" 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="iso-8859-1"') 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="iso-8859-1"') self.assertEqual(html_part["Content-Transfer-Encoding"], transfer_encoding) payload = html_part.get_payload() self.assertEqual(payload, _qencode(html_encoded).decode("ascii"))
def test_attach_as_body_and_html_utf8(self): from pyramid_mailer.message import Message from pyramid_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_to_message_multipart_with_qpencoding(self): from pyramid_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_html(self): from pyramid_mailer.message import Message from pyramid_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(self): from pyramid_mailer.message import Message from pyramid_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_body_is_text_quopri(self): text_encoded = b'LaPe\xf1a' result = self._callFUT('quoted-printable', text_encoded) self.assertEqual( result, _qencode(text_encoded) )
def test_to_message_multipart_with_qpencoding(self): from pyramid_mailer.message import Message, Attachment response = Message( recipients=['To'], sender='From', subject='Subject', body='Body', html='Html' ) this = os.path.abspath(__file__) data = open(this, 'rb') 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_to_message_multipart_with_qpencoding(self): from pyramid_mailer.message import Message, Attachment response = Message(recipients=["To"], sender="From", subject="Subject", body="Body", html="Html") this = os.path.abspath(__file__) data = open(this, "rb") 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_repoze_sendmail_send_to_queue_functional(self): # functest that emulates the interaction between pyramid_mailer and # repoze.maildir.add and queuedelivery.send. import tempfile from email.generator import Generator from email.parser import Parser from pyramid_mailer.message import Message from pyramid_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 pyramid_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
def test_repoze_sendmail_send_to_queue_functional(self): # functest that emulates the interaction between pyramid_mailer and # repoze.maildir.add and queuedelivery.send. import tempfile from email.generator import Generator from email.parser import Parser from pyramid_mailer.message import Message from pyramid_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 pyramid_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