python / cpython

The Python programming language
https://www.python.org
Other
62.47k stars 29.99k forks source link

email.encoders.encode_base64 create a single long line #55365

Closed c7d07a43-d799-45c8-bc8c-bbcc9898e89c closed 13 years ago

c7d07a43-d799-45c8-bc8c-bbcc9898e89c commented 13 years ago
BPO 11156
Nosy @bitdancer
Superseder
  • bpo-9298: binary email attachment issue with base64 encoding
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = created_at = labels = ['type-bug', 'library'] title = 'email.encoders.encode_base64 create a single long line' updated_at = user = 'https://bugs.python.org/yveszioupcom' ``` bugs.python.org fields: ```python activity = actor = 'r.david.murray' assignee = 'none' closed = True closed_date = closer = 'r.david.murray' components = ['Library (Lib)'] creation = creator = 'yves@zioup.com' dependencies = [] files = [] hgrepos = [] issue_num = 11156 keywords = [] message_count = 3.0 messages = ['128202', '128213', '128219'] nosy_count = 2.0 nosy_names = ['r.david.murray', 'yves@zioup.com'] pr_nums = [] priority = 'normal' resolution = 'duplicate' stage = 'resolved' status = 'closed' superseder = '9298' type = 'behavior' url = 'https://bugs.python.org/issue11156' versions = ['Python 3.1', 'Python 3.2', 'Python 3.3'] ```

    c7d07a43-d799-45c8-bc8c-bbcc9898e89c commented 13 years ago

    email.encoders.encode_base64 returns a str of a single long line instead of breaking it up into 76 chars line as per RFC 2045, and as implemented by email.base64mime.

    Solution: In /usr/lib/python3.1/email/encoders.py, use encodebytes instead of b64encode:

    --- encoders.py 2011-02-08 09:37:21.025030051 -0700
    +++ encoders.py.yves    2011-02-08 09:38:04.945608365 -0700
    @@ -12,7 +12,7 @@
         ]
    
    -from base64 import b64encode as _bencode
    +from base64 import encodebytes as _bencode
     from quopri import encodestring as _encode
    c7d07a43-d799-45c8-bc8c-bbcc9898e89c commented 13 years ago

    In case this does not get fixed for a long time, here is a work around (re-implement the encoder yourself):

    . . . def myencoder(msg): from base64 import encodebytes as _bencode

      orig = msg.get_payload()
      encdata = str(_bencode(orig), 'ascii')
      msg.set_payload(encdata)
      msg['Content-Transfer-Encoding'] = 'base64'
    .
    .
    .
    # here is an example of how to use it
    fp = open('/usr/share/openclipart/png/animals/mammals/happy_monkey_benji_park_01.png', 'rb')
    
    bindata = fp.read()
    
    x = email.mime.image.MIMEImage(bindata, _subtype='png', _encoder=myencoder)
    
    y = x.get_payload()
    print (y)
    .
    .
    .
    bitdancer commented 13 years ago

    This appears to be a duplicate of bpo-9298. Yves, if you disagree let me know what's different.