Python email.generator.BytesGenerator() Examples
The following are 30
code examples of email.generator.BytesGenerator().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
email.generator
, or try the search function
.
Example #1
Source File: test_generator.py From android_universal with MIT License | 6 votes |
def test_smtputf8_policy(self): msg = EmailMessage() msg['From'] = "Páolo <főo@bar.com>" msg['To'] = 'Dinsdale' msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' msg.set_content("oh là là, know what I mean, know what I mean?") expected = textwrap.dedent("""\ From: Páolo <főo@bar.com> To: Dinsdale Subject: Nudge nudge, wink, wink \u1F609 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit MIME-Version: 1.0 oh là là, know what I mean, know what I mean? """).encode('utf-8').replace(b'\n', b'\r\n') s = io.BytesIO() g = BytesGenerator(s, policy=policy.SMTPUTF8) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #2
Source File: test_generator.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_smtputf8_policy(self): msg = EmailMessage() msg['From'] = "Páolo <főo@bar.com>" msg['To'] = 'Dinsdale' msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' msg.set_content("oh là là, know what I mean, know what I mean?") expected = textwrap.dedent("""\ From: Páolo <főo@bar.com> To: Dinsdale Subject: Nudge nudge, wink, wink \u1F609 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit MIME-Version: 1.0 oh là là, know what I mean, know what I mean? """).encode('utf-8').replace(b'\n', b'\r\n') s = io.BytesIO() g = BytesGenerator(s, policy=policy.SMTPUTF8) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #3
Source File: test_email.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_binary_body_with_encode_7or8bit(self): # Issue 17171. bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff' msg = MIMEApplication(bytesdata, _encoder=encoders.encode_7or8bit) # Treated as a string, this will be invalid code points. self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata)) self.assertEqual(msg.get_payload(decode=True), bytesdata) self.assertEqual(msg['Content-Transfer-Encoding'], '8bit') s = BytesIO() g = BytesGenerator(s) g.flatten(msg) wireform = s.getvalue() msg2 = email.message_from_bytes(wireform) self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata)) self.assertEqual(msg2.get_payload(decode=True), bytesdata) self.assertEqual(msg2['Content-Transfer-Encoding'], '8bit')
Example #4
Source File: test_email.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_mangled_from_with_bad_bytes(self): source = textwrap.dedent("""\ Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: aaa@bbb.org """).encode('utf-8') msg = email.message_from_bytes(source + b'From R\xc3\xb6lli\n') b = BytesIO() g = BytesGenerator(b, mangle_from_=True) g.flatten(msg) self.assertEqual(b.getvalue(), source + b'>From R\xc3\xb6lli\n') # Test the basic MIMEAudio class
Example #5
Source File: multipart.py From python-libmaas with GNU Affero General Public License v3.0 | 6 votes |
def encode_multipart_message(message): # The message must be multipart. assert message.is_multipart() # The body length cannot yet be known. assert "Content-Length" not in message # So line-endings can be fixed-up later on, component payloads must have # no Content-Length and their Content-Transfer-Encoding must be base64 # (and not quoted-printable, which Django doesn't appear to understand). for part in message.get_payload(): assert "Content-Length" not in part assert part["Content-Transfer-Encoding"] == "base64" # Flatten the message without headers. buf = BytesIO() generator = BytesGenerator(buf, False) # Don't mangle "^From". generator._write_headers = lambda self: None # Ignore. generator.flatten(message) # Ensure the body has CRLF-delimited lines. See # http://bugs.python.org/issue1349106. body = b"\r\n".join(buf.getvalue().splitlines()) # Only now is it safe to set the content length. message.add_header("Content-Length", "%d" % len(body)) return message.items(), body
Example #6
Source File: test_generator.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_smtputf8_policy(self): msg = EmailMessage() msg['From'] = "Páolo <főo@bar.com>" msg['To'] = 'Dinsdale' msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' msg.set_content("oh là là, know what I mean, know what I mean?") expected = textwrap.dedent("""\ From: Páolo <főo@bar.com> To: Dinsdale Subject: Nudge nudge, wink, wink \u1F609 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit MIME-Version: 1.0 oh là là, know what I mean, know what I mean? """).encode('utf-8').replace(b'\n', b'\r\n') s = io.BytesIO() g = BytesGenerator(s, policy=policy.SMTPUTF8) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #7
Source File: test_inversion.py From android_universal with MIT License | 5 votes |
def msg_as_input(self, msg): m = message_from_bytes(msg, policy=policy.SMTP) b = io.BytesIO() g = BytesGenerator(b) g.flatten(m) self.assertEqual(b.getvalue(), msg) # XXX: spaces are not preserved correctly here yet in the general case.
Example #8
Source File: message.py From android_universal with MIT License | 5 votes |
def as_bytes(self, unixfrom=False, policy=None): """Return the entire formatted message as a bytes object. Optional 'unixfrom', when true, means include the Unix From_ envelope header. 'policy' is passed to the BytesGenerator instance used to serialize the message; if not specified the policy associated with the message instance is used. """ from email.generator import BytesGenerator policy = self.policy if policy is None else policy fp = BytesIO() g = BytesGenerator(fp, mangle_from_=False, policy=policy) g.flatten(self, unixfrom=unixfrom) return fp.getvalue()
Example #9
Source File: message.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def as_bytes(self, unixfrom=False, policy=None): """Return the entire formatted message as a bytes object. Optional 'unixfrom', when true, means include the Unix From_ envelope header. 'policy' is passed to the BytesGenerator instance used to serialize the message; if not specified the policy associated with the message instance is used. """ from email.generator import BytesGenerator policy = self.policy if policy is None else policy fp = BytesIO() g = BytesGenerator(fp, mangle_from_=False, policy=policy) g.flatten(self, unixfrom=unixfrom) return fp.getvalue()
Example #10
Source File: test_generator.py From android_universal with MIT License | 5 votes |
def test_cte_type_7bit_handles_unknown_8bit(self): source = ("Subject: Maintenant je vous présente mon " "collègue\n\n").encode('utf-8') expected = ('Subject: Maintenant je vous =?unknown-8bit?q?' 'pr=C3=A9sente_mon_coll=C3=A8gue?=\n\n').encode('ascii') msg = message_from_bytes(source) s = io.BytesIO() g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit')) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #11
Source File: test_generator.py From android_universal with MIT License | 5 votes |
def test_cte_type_7bit_transforms_8bit_cte(self): source = textwrap.dedent("""\ From: foo@bar.com To: Dinsdale Subject: Nudge nudge, wink, wink Mime-Version: 1.0 Content-Type: text/plain; charset="latin-1" Content-Transfer-Encoding: 8bit oh là là, know what I mean, know what I mean? """).encode('latin1') msg = message_from_bytes(source) expected = textwrap.dedent("""\ From: foo@bar.com To: Dinsdale Subject: Nudge nudge, wink, wink Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable oh l=E0 l=E0, know what I mean, know what I mean? """).encode('ascii') s = io.BytesIO() g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit', linesep='\n')) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #12
Source File: message.py From odoo13-x64 with GNU General Public License v3.0 | 5 votes |
def as_bytes(self, unixfrom=False, policy=None): """Return the entire formatted message as a bytes object. Optional 'unixfrom', when true, means include the Unix From_ envelope header. 'policy' is passed to the BytesGenerator instance used to serialize the message; if not specified the policy associated with the message instance is used. """ from email.generator import BytesGenerator policy = self.policy if policy is None else policy fp = BytesIO() g = BytesGenerator(fp, mangle_from_=False, policy=policy) g.flatten(self, unixfrom=unixfrom) return fp.getvalue()
Example #13
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_crlf_flatten(self): with openfile('msg_26.txt', 'rb') as fp: text = fp.read() msg = email.message_from_bytes(text) s = BytesIO() g = email.generator.BytesGenerator(s) g.flatten(msg, linesep='\r\n') self.assertEqual(s.getvalue(), text)
Example #14
Source File: test_generator.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_cte_type_7bit_transforms_8bit_cte(self): source = textwrap.dedent("""\ From: foo@bar.com To: Dinsdale Subject: Nudge nudge, wink, wink Mime-Version: 1.0 Content-Type: text/plain; charset="latin-1" Content-Transfer-Encoding: 8bit oh là là, know what I mean, know what I mean? """).encode('latin1') msg = message_from_bytes(source) expected = textwrap.dedent("""\ From: foo@bar.com To: Dinsdale Subject: Nudge nudge, wink, wink Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable oh l=E0 l=E0, know what I mean, know what I mean? """).encode('ascii') s = io.BytesIO() g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit', linesep='\n')) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #15
Source File: test_generator.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_cte_type_7bit_handles_unknown_8bit(self): source = ("Subject: Maintenant je vous présente mon " "collègue\n\n").encode('utf-8') expected = ('Subject: Maintenant je vous =?unknown-8bit?q?' 'pr=C3=A9sente_mon_coll=C3=A8gue?=\n\n').encode('ascii') msg = message_from_bytes(source) s = io.BytesIO() g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit')) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #16
Source File: test_inversion.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def msg_as_input(self, msg): m = message_from_bytes(msg, policy=policy.SMTP) b = io.BytesIO() g = BytesGenerator(b) g.flatten(m) self.assertEqual(b.getvalue(), msg) # XXX: spaces are not preserved correctly here yet in the general case.
Example #17
Source File: message.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def as_bytes(self, unixfrom=False, policy=None): """Return the entire formatted message as a bytes object. Optional 'unixfrom', when true, means include the Unix From_ envelope header. 'policy' is passed to the BytesGenerator instance used to serialize the message; if not specified the policy associated with the message instance is used. """ from email.generator import BytesGenerator policy = self.policy if policy is None else policy fp = BytesIO() g = BytesGenerator(fp, mangle_from_=False, policy=policy) g.flatten(self, unixfrom=unixfrom) return fp.getvalue()
Example #18
Source File: test_generator.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_cte_type_7bit_transforms_8bit_cte(self): source = textwrap.dedent("""\ From: foo@bar.com To: Dinsdale Subject: Nudge nudge, wink, wink Mime-Version: 1.0 Content-Type: text/plain; charset="latin-1" Content-Transfer-Encoding: 8bit oh là là, know what I mean, know what I mean? """).encode('latin1') msg = message_from_bytes(source) expected = textwrap.dedent("""\ From: foo@bar.com To: Dinsdale Subject: Nudge nudge, wink, wink Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable oh l=E0 l=E0, know what I mean, know what I mean? """).encode('ascii') s = io.BytesIO() g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit', linesep='\n')) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #19
Source File: test_generator.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_cte_type_7bit_handles_unknown_8bit(self): source = ("Subject: Maintenant je vous présente mon " "collègue\n\n").encode('utf-8') expected = ('Subject: Maintenant je vous =?unknown-8bit?q?' 'pr=C3=A9sente_mon_coll=C3=A8gue?=\n\n').encode('ascii') msg = message_from_bytes(source) s = io.BytesIO() g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit')) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #20
Source File: test_inversion.py From ironpython3 with Apache License 2.0 | 5 votes |
def msg_as_input(self, msg): m = message_from_bytes(msg, policy=policy.SMTP) b = io.BytesIO() g = BytesGenerator(b) g.flatten(m) self.assertEqual(b.getvalue(), msg) # XXX: spaces are not preserved correctly here yet in the general case.
Example #21
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_binary_body_with_encode_base64(self): bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff' msg = MIMEApplication(bytesdata, _encoder=encoders.encode_base64) self.assertEqual(msg.get_payload(), '+vv8/f7/\n') self.assertEqual(msg.get_payload(decode=True), bytesdata) s = BytesIO() g = BytesGenerator(s) g.flatten(msg) wireform = s.getvalue() msg2 = email.message_from_bytes(wireform) self.assertEqual(msg.get_payload(), '+vv8/f7/\n') self.assertEqual(msg2.get_payload(decode=True), bytesdata) # Test the basic MIMEText class
Example #22
Source File: test_inversion.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def msg_as_input(self, msg): m = message_from_bytes(msg, policy=policy.SMTP) b = io.BytesIO() g = BytesGenerator(b) g.flatten(m) self.assertEqual(b.getvalue(), msg) # XXX: spaces are not preserved correctly here yet in the general case.
Example #23
Source File: test_generator.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_cte_type_7bit_handles_unknown_8bit(self): source = ("Subject: Maintenant je vous présente mon " "collègue\n\n").encode('utf-8') expected = ('Subject: Maintenant je vous =?unknown-8bit?q?' 'pr=C3=A9sente_mon_coll=C3=A8gue?=\n\n').encode('ascii') msg = message_from_bytes(source) s = io.BytesIO() g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit')) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #24
Source File: test_generator.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_cte_type_7bit_transforms_8bit_cte(self): source = textwrap.dedent("""\ From: foo@bar.com To: Dinsdale Subject: Nudge nudge, wink, wink Mime-Version: 1.0 Content-Type: text/plain; charset="latin-1" Content-Transfer-Encoding: 8bit oh là là, know what I mean, know what I mean? """).encode('latin1') msg = message_from_bytes(source) expected = textwrap.dedent("""\ From: foo@bar.com To: Dinsdale Subject: Nudge nudge, wink, wink Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable oh l=E0 l=E0, know what I mean, know what I mean? """).encode('ascii') s = io.BytesIO() g = BytesGenerator(s, policy=self.policy.clone(cte_type='7bit', linesep='\n')) g.flatten(msg) self.assertEqual(s.getvalue(), expected)
Example #25
Source File: message.py From Imogen with MIT License | 5 votes |
def as_bytes(self, unixfrom=False, policy=None): """Return the entire formatted message as a bytes object. Optional 'unixfrom', when true, means include the Unix From_ envelope header. 'policy' is passed to the BytesGenerator instance used to serialize the message; if not specified the policy associated with the message instance is used. """ from email.generator import BytesGenerator policy = self.policy if policy is None else policy fp = BytesIO() g = BytesGenerator(fp, mangle_from_=False, policy=policy) g.flatten(self, unixfrom=unixfrom) return fp.getvalue()
Example #26
Source File: message.py From ironpython3 with Apache License 2.0 | 5 votes |
def as_bytes(self, unixfrom=False, policy=None): """Return the entire formatted message as a bytes object. Optional 'unixfrom', when true, means include the Unix From_ envelope header. 'policy' is passed to the BytesGenerator instance used to serialize the message; if not specified the policy associated with the message instance is used. """ from email.generator import BytesGenerator policy = self.policy if policy is None else policy fp = BytesIO() g = BytesGenerator(fp, mangle_from_=False, policy=policy) g.flatten(self, unixfrom=unixfrom) return fp.getvalue()
Example #27
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_byte_message_rfc822_only(self): # Make sure new bytes header parser also passes this. with openfile('msg_46.txt') as fp: msgdata = fp.read().encode('ascii') parser = email.parser.BytesHeaderParser() msg = parser.parsebytes(msgdata) out = BytesIO() gen = email.generator.BytesGenerator(out) gen.flatten(msg) self.assertEqual(out.getvalue(), msgdata)
Example #28
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_as_bytes_policy(self): msg = self._msgobj('msg_01.txt') newpolicy = msg.policy.clone(linesep='\r\n') fullrepr = msg.as_bytes(policy=newpolicy) s = BytesIO() g = BytesGenerator(s,policy=newpolicy) g.flatten(msg) self.assertEqual(fullrepr, s.getvalue()) # test_headerregistry.TestContentTypeHeader.bad_params
Example #29
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_binary_body_with_encode_quopri(self): # Issue 14360. bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff ' msg = MIMEApplication(bytesdata, _encoder=encoders.encode_quopri) self.assertEqual(msg.get_payload(), '=FA=FB=FC=FD=FE=FF=20') self.assertEqual(msg.get_payload(decode=True), bytesdata) self.assertEqual(msg['Content-Transfer-Encoding'], 'quoted-printable') s = BytesIO() g = BytesGenerator(s) g.flatten(msg) wireform = s.getvalue() msg2 = email.message_from_bytes(wireform) self.assertEqual(msg.get_payload(), '=FA=FB=FC=FD=FE=FF=20') self.assertEqual(msg2.get_payload(decode=True), bytesdata) self.assertEqual(msg2['Content-Transfer-Encoding'], 'quoted-printable')
Example #30
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_bytes_generator_b_encoding_linesep(self): # Issue 14062: b encoding was tacking on an extra \n. m = Message() # This has enough non-ascii that it should always end up b encoded. m['Subject'] = Header('žluťoučký kůň') s = BytesIO() g = email.generator.BytesGenerator(s) g.flatten(m, linesep='\r\n') self.assertEqual( s.getvalue(), b'Subject: =?utf-8?b?xb5sdcWlb3XEjWvDvSBrxa/FiA==?=\r\n\r\n')