Python email.message_from_bytes() Examples
The following are 30
code examples of email.message_from_bytes().
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
, or try the search function
.
Example #1
Source File: test_email.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_invalid_8bit_in_non_8bit_cte_uses_replace(self): # This is similar to the previous test, but proves that if the 8bit # byte is undecodeable in the specified charset, it gets replaced # by the unicode 'unknown' character. Again, this may or may not # be the ideal behavior. Note that if decode=False none of the # decoders will get involved, so this is the only test we need # for this behavior. m = self.bodytest_msg.format(charset='ascii', cte='quoted-printable', bodyline='p=C3=B6stál').encode('utf-8') msg = email.message_from_bytes(m) self.assertEqual(msg.get_payload(), 'p=C3=B6st\uFFFD\uFFFDl\n') self.assertEqual(msg.get_payload(decode=True), 'pöstál\n'.encode('utf-8')) # test_defect_handling:test_invalid_chars_in_base64_payload
Example #2
Source File: test_email.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_binary_body_with_encode_noop(self): # Issue 16564: This does not produce an RFC valid message, since to be # valid it should have a CTE of binary. But the below works in # Python2, and is documented as working this way. bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff' msg = MIMEApplication(bytesdata, _encoder=encoders.encode_noop) # 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) 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)
Example #3
Source File: gmail_client.py From indra with BSD 2-Clause "Simplified" License | 6 votes |
def fetch_email(M, msg_id): """Returns the given email message as a unicode string.""" res, data = M.fetch(msg_id, '(RFC822)') if res == 'OK': # Data here is a list with 1 element containing a tuple # whose 2nd element is a long string containing the email # The content is a bytes that must be decoded raw_msg_txt = data[0][1] # In Python3, we call message_from_bytes, but this function doesn't # exist in Python 2. try: msg = email.message_from_bytes(raw_msg_txt) except AttributeError: msg = email.message_from_string(raw_msg_txt) # At this point, we have a message containing bytes (not unicode) # fields that will still need to be decoded, ideally according to the # character set specified in the message. return msg else: return None
Example #4
Source File: mailbox.py From Imogen with MIT License | 6 votes |
def __init__(self, message=None): """Initialize a Message instance.""" if isinstance(message, email.message.Message): self._become_message(copy.deepcopy(message)) if isinstance(message, Message): message._explain_to(self) elif isinstance(message, bytes): self._become_message(email.message_from_bytes(message)) elif isinstance(message, str): self._become_message(email.message_from_string(message)) elif isinstance(message, io.TextIOWrapper): self._become_message(email.message_from_file(message)) elif hasattr(message, "read"): self._become_message(email.message_from_binary_file(message)) elif message is None: email.message.Message.__init__(self) else: raise TypeError('Invalid message type: %s' % type(message))
Example #5
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 #6
Source File: smime.py From commandment with MIT License | 6 votes |
def decrypt(smime: bytes, key: rsa.RSAPrivateKey, serial: Optional[int] = None): """Decrypt an S/MIME message using the RSA Private Key given. The recipient can be hinted using the serial parameter, otherwise we assume single recipient = the given key. """ string_content = smime.decode('utf8') msg: Message = email.message_from_string(string_content) assert msg.get_content_type() == 'application/pkcs7-mime' assert msg.get_filename() == 'smime.p7m' assert msg.get('Content-Description') == 'S/MIME Encrypted Message' b64payload = msg.get_payload() payload = b64decode(b64payload) decrypted_data = decrypt_smime_content(payload, key) decrypted_msg: Message = email.message_from_bytes(decrypted_data) return decrypted_msg.get_payload()
Example #7
Source File: mailbox.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def __init__(self, message=None): """Initialize a Message instance.""" if isinstance(message, email.message.Message): self._become_message(copy.deepcopy(message)) if isinstance(message, Message): message._explain_to(self) elif isinstance(message, bytes): self._become_message(email.message_from_bytes(message)) elif isinstance(message, str): self._become_message(email.message_from_string(message)) elif isinstance(message, io.TextIOWrapper): self._become_message(email.message_from_file(message)) elif hasattr(message, "read"): self._become_message(email.message_from_binary_file(message)) elif message is None: email.message.Message.__init__(self) else: raise TypeError('Invalid message type: %s' % type(message))
Example #8
Source File: test_email.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_8bit_in_quopri_body(self): # This is non-RFC compliant data...without 'decode' the library code # decodes the body using the charset from the headers, and because the # source byte really is utf-8 this works. This is likely to fail # against real dirty data (ie: produce mojibake), but the data is # invalid anyway so it is as good a guess as any. But this means that # this test just confirms the current behavior; that behavior is not # necessarily the best possible behavior. With 'decode' it is # returning the raw bytes, so that test should be of correct behavior, # or at least produce the same result that email4 did. m = self.bodytest_msg.format(charset='utf-8', cte='quoted-printable', bodyline='p=C3=B6stál').encode('utf-8') msg = email.message_from_bytes(m) self.assertEqual(msg.get_payload(), 'p=C3=B6stál\n') self.assertEqual(msg.get_payload(decode=True), 'pöstál\n'.encode('utf-8'))
Example #9
Source File: mailbox.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def __init__(self, message=None): """Initialize a Message instance.""" if isinstance(message, email.message.Message): self._become_message(copy.deepcopy(message)) if isinstance(message, Message): message._explain_to(self) elif isinstance(message, bytes): self._become_message(email.message_from_bytes(message)) elif isinstance(message, str): self._become_message(email.message_from_string(message)) elif isinstance(message, io.TextIOWrapper): self._become_message(email.message_from_file(message)) elif hasattr(message, "read"): self._become_message(email.message_from_binary_file(message)) elif message is None: email.message.Message.__init__(self) else: raise TypeError('Invalid message type: %s' % type(message))
Example #10
Source File: mailbox.py From ironpython3 with Apache License 2.0 | 6 votes |
def __init__(self, message=None): """Initialize a Message instance.""" if isinstance(message, email.message.Message): self._become_message(copy.deepcopy(message)) if isinstance(message, Message): message._explain_to(self) elif isinstance(message, bytes): self._become_message(email.message_from_bytes(message)) elif isinstance(message, str): self._become_message(email.message_from_string(message)) elif isinstance(message, io.TextIOWrapper): self._become_message(email.message_from_file(message)) elif hasattr(message, "read"): self._become_message(email.message_from_binary_file(message)) elif message is None: email.message.Message.__init__(self) else: raise TypeError('Invalid message type: %s' % type(message))
Example #11
Source File: parser.py From lego with MIT License | 6 votes |
def parse(self): try: if self.message_type == ParserMessageType.STRING: msg = email.message_from_string(self.raw_message, Message) elif self.message_type == ParserMessageType.BYTES: msg = email.message_from_bytes(self.raw_message, Message) elif self.message_type == ParserMessageType.BINARY_FILE: msg = email.message_from_binary_file(self.raw_message, Message) else: raise ValueError("Invalid message_type, could not parse message.") except Exception: raise ParseEmailException # Do basic post-processing of the message, checking it for defects or # other missing information. if msg.defects: raise DefectMessageException # Add headers used by LEGO msg.original_size = len(self.raw_message) msg["X-MailFrom"] = self.mail_from return msg
Example #12
Source File: queue_processors.py From zulip with Apache License 2.0 | 6 votes |
def consume(self, event: Mapping[str, Any]) -> None: rcpt_to = event['rcpt_to'] msg = email.message_from_bytes( base64.b64decode(event["msg_base64"]), policy=email.policy.default, ) assert isinstance(msg, EmailMessage) # https://github.com/python/typeshed/issues/2417 if not is_missed_message_address(rcpt_to): # Missed message addresses are one-time use, so we don't need # to worry about emails to them resulting in message spam. recipient_realm = decode_stream_email_address(rcpt_to)[0].realm try: rate_limit_mirror_by_realm(recipient_realm) except RateLimited: logger.warning("MirrorWorker: Rejecting an email from: %s " "to realm: %s - rate limited.", msg['From'], recipient_realm.name) return mirror_email(msg, rcpt_to=rcpt_to)
Example #13
Source File: imapserver.py From aioimaplib with GNU General Public License v3.0 | 6 votes |
def append_literal(self, data): tag, mailbox_name, size = self.append_literal_command if data == CRLF: if 'UIDPLUS' in self.capabilities: self.send_tagged_line(tag, 'OK [APPENDUID %s %s] APPEND completed.' % (self.uidvalidity, self.server_state.max_uid(self.user_login, mailbox_name))) else: self.send_tagged_line(tag, 'OK APPEND completed.') self.append_literal_command = None return literal_data, rest = data[:size], data[size:] if len(literal_data) < size: self.send_tagged_line(self.append_literal_command[0], 'BAD literal length : expected %s but was %s' % (size, len(literal_data))) self.append_literal_command = None elif rest and rest != CRLF: self.send_tagged_line(self.append_literal_command[0], 'BAD literal trailing data : expected CRLF but got %s' % (rest)) else: m = email.message_from_bytes(data) self.server_state.add_mail(self.user_login, Mail(m), mailbox_name) if rest: self.append_literal(rest)
Example #14
Source File: email_handler.py From app with MIT License | 6 votes |
def handle_sender_email(envelope: Envelope): filename = ( arrow.now().format("YYYY-MM-DD_HH-mm-ss") + "_" + random_string(10) + ".eml" ) filepath = os.path.join(SENDER_DIR, filename) with open(filepath, "wb") as f: f.write(envelope.original_content) LOG.d("Write email to sender at %s", filepath) msg = email.message_from_bytes(envelope.original_content) orig = get_orig_message_from_bounce(msg) if orig: LOG.warning( "Original message %s -> %s saved at %s", orig["From"], orig["To"], filepath ) return "250 email to sender accepted"
Example #15
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 #16
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def _msgobj(self, filename): with openfile(filename, 'rb') as fp: data = fp.read() data = self.normalize_linesep_regex.sub(self.blinesep, data) msg = email.message_from_bytes(data) return msg, data
Example #17
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 #18
Source File: __init__.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def _bytes_msg(self, bytestring, message=None, policy=None): if policy is None: policy = self.policy if message is None: message = self.message return email.message_from_bytes(bytestring, message, policy=policy)
Example #19
Source File: test_parser.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def parser(self, s, *args, **kw): return email.message_from_bytes(s.encode(), *args, **kw)
Example #20
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_get_content_type_with_8bit(self): msg = email.message_from_bytes(textwrap.dedent("""\ Content-Type: text/pl\xA7in; charset=utf-8 """).encode('latin-1')) self.assertEqual(msg.get_content_type(), "text/pl\uFFFDin") self.assertEqual(msg.get_content_maintype(), "text") self.assertEqual(msg.get_content_subtype(), "pl\uFFFDin") # test_headerregistry.TestContentTypeHeader.non_ascii_in_params
Example #21
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_decoded_generator_emits_unicode_body(self): m = email.message_from_bytes(self.latin_bin_msg) out = StringIO() email.generator.DecodedGenerator(out).flatten(m) #DecodedHeader output contains an extra blank line compared #to the input message. RDM: not sure if this is a bug or not, #but it is not specific to the 8bit->7bit conversion. self.assertEqual(out.getvalue(), self.latin_bin_msg.decode('latin-1')+'\n')
Example #22
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_string_generator_reencodes_to_quopri_when_appropriate(self): m = email.message_from_bytes(self.latin_bin_msg) self.assertEqual(str(m), self.latin_bin_msg_as7bit)
Example #23
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_bytes_generator_with_unix_from(self): # The unixfrom contains a current date, so we can't check it # literally. Just make sure the first word is 'From' and the # rest of the message matches the input. msg = email.message_from_bytes(self.non_latin_bin_msg) out = BytesIO() email.generator.BytesGenerator(out).flatten(msg, unixfrom=True) lines = out.getvalue().split(b'\n') self.assertEqual(lines[0].split()[0], b'From') self.assertEqual(b'\n'.join(lines[1:]), self.non_latin_bin_msg)
Example #24
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_str_generator_should_not_mutate_msg_when_handling_8bit(self): msg = email.message_from_bytes(self.non_latin_bin_msg) out = BytesIO() BytesGenerator(out).flatten(msg) orig_value = out.getvalue() Generator(StringIO()).flatten(msg) # Should not mutate msg! out = BytesIO() BytesGenerator(out).flatten(msg) self.assertEqual(out.getvalue(), orig_value)
Example #25
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_bytes_generator(self): msg = email.message_from_bytes(self.non_latin_bin_msg) out = BytesIO() email.generator.BytesGenerator(out).flatten(msg) self.assertEqual(out.getvalue(), self.non_latin_bin_msg)
Example #26
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_get_payload_with_8bit_cte_header(self): msg = email.message_from_bytes(textwrap.dedent("""\ Content-Transfer-Encoding: b\xa7se64 Content-Type: text/plain; charset=latin-1 payload """).encode('latin-1')) self.assertEqual(msg.get_payload(), 'payload\n') self.assertEqual(msg.get_payload(decode=True), b'payload\n')
Example #27
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_del_rfc2231_params_with_8bit(self): msg = email.message_from_bytes(textwrap.dedent("""\ Content-Type: text/plain; charset=us-ascii; title*=us-ascii'en'This%20is%20not%20f\xa7n""" ).encode('latin-1')) msg.del_param('title') self.assertEqual(msg.get_param('title'), None) self.assertEqual(msg.get_content_maintype(), 'text')
Example #28
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_set_rfc2231_params_with_8bit(self): msg = email.message_from_bytes(textwrap.dedent("""\ Content-Type: text/plain; charset=us-ascii; title*=us-ascii'en'This%20is%20not%20f\xa7n""" ).encode('latin-1')) msg.set_param('title', 'test') self.assertEqual(msg.get_param('title'), 'test')
Example #29
Source File: test_email.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_get_rfc2231_params_with_8bit(self): msg = email.message_from_bytes(textwrap.dedent("""\ Content-Type: text/plain; charset=us-ascii; title*=us-ascii'en'This%20is%20not%20f\xa7n""" ).encode('latin-1')) self.assertEqual(msg.get_param('title'), ('us-ascii', 'en', 'This is not f\uFFFDn'))
Example #30
Source File: mailagent.py From huobi-autotrading with MIT License | 5 votes |
def getSubject(self, i): conn = self.conn id_list = conn.search(None, '(UNSEEN)')[1][0].split() try: email_id = id_list[i] except IndexError: return None, -1 data = conn.fetch(email_id, 'BODY.PEEK[HEADER.FIELDS (SUBJECT)]')[1] msg = message_from_bytes(data[0][1]) s, encoding = decode_header(msg['Subject'])[0] subject = s if type(s) is str else s.decode(encoding or 'utf-8') return subject