Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse_from_bytes() not working on BytesIO() object #98

Closed
degenkolb opened this issue Aug 9, 2021 · 1 comment
Closed

parse_from_bytes() not working on BytesIO() object #98

degenkolb opened this issue Aug 9, 2021 · 1 comment

Comments

@degenkolb
Copy link

Describe the bug
Hi, I would like to use mailparser on a BytesIO() object in python 3 but fail to load the message.
I have to use the object of class 'bytes' because I get the data handed this way.

I also tried with the other parse_from_ functions. All fail.

https://docs.python.org/3/glossary.html#term-bytes-like-object explizit mentions Objects of type bytes as a "bytes-like object".

Is there another way I'm not aware of?

To Reproduce
Steps to reproduce the behavior:
Minimal working example

from io import BytesIO
from os import read
import mailparser

fp = BytesIO()

with open('mail_file.eml', 'rb') as f:
    filecontent = f.read()
    print("type(filecontent)= ", type(filecontent))
    fp.write(filecontent)

mail = mailparser.parse_from_bytes(fp)

print(mail.body)

this results in

$ python testparser.py 
type(filecontent)=  <class 'bytes'>
Traceback (most recent call last):
  File "testparser.py", line 15, in <module>
    mail = mailparser.parse_from_bytes(fp)
  File "/home/xxx/.local/lib/python3.8/site-packages/mailparser/mailparser.py", line 118, in parse_from_bytes
    return MailParser.from_bytes(bt)
  File "/home/xxx/.local/lib/python3.8/site-packages/mailparser/mailparser.py", line 240, in from_bytes
    message = email.message_from_bytes(bt)
  File "/usr/lib/python3.8/email/__init__.py", line 46, in message_from_bytes
    return BytesParser(*args, **kws).parsebytes(s)
  File "/usr/lib/python3.8/email/parser.py", line 122, in parsebytes
    text = text.decode('ASCII', errors='surrogateescape')
AttributeError: '_io.BytesIO' object has no attribute 'decode'

Expected behavior
mailparser.parse_from_bytes(fp) to load the object and be able to parse the message

Raw mail
Do you need the raw message? This is a pre parser problem so the message should be irrelevant.

Environment:

  • OS: Linux Ubuntu 20.04
  • Docker: no
  • mail-parser version: 3.15.0
  • python: 3.8.10

Thansk for your response.

@degenkolb
Copy link
Author

Closing this since I found a solution.

mail = mailparser.parse_from_bytes(fp.getvalue())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant