Skip to content

extracting message size from BODYSTRUCTURE - extra layer of brackets in result #475

Closed
@mungewell

Description

@mungewell

I am trying to extract the size of a message from the BODYSTRUCTURE report. I notice that it contains a multipart parameter, and this lets me evaluate the report.

However it seems that I get a third type of result, which contains an extra 'layer'.

---
([(b'text', b'plain', (b'charset', b'US-ASCII', b'format', b'flowed'), None, None, b'7bit', 39, 0, None, None, None, None), (b'message', b'rfc822', (b'name', b'Dell Order Has Been Acknowledged.eml'), None, None, b'8bit', 37758, (b'7 May 2020 19:23:25 -0500', b'=?utf-8?B?RGVsbCBPcmRlciBIYXMgQmVlbiBBY2tub3dsZWRnZWQ=?=', ((b'Dell Canada Online Sales', None, b'dell_automated_email', b'dell.com'),), ((b'Dell Automated Email', None, b'automated_email', b'dell.com'),), ((b'Dell Canada Online Sales', None, b'dell_automated_email', b'dell.com'),), ((None, None, b'simon', b'mungewell.org'),), None, None, None, b'<[email protected]>'), (b'text', b'html', (b'charset', b'utf-8'), None, None, b'7bit', 33956, 500, None, None, None, None), 574, None, (b'attachment', (b'filename', b'Dell Order Has Been Acknowledged.eml', b'size', b'37758')), None, None)], b'mixed', (b'boundary', b'=_5b7507bbac3255404153ce14dd599572'), None, None, None)
multipart: 37797
---
(b'text', b'plain', (b'charset', b'UTF-8', b'format', b'flowed'), None, None, b'8bit', 4368, 219, None, None, None, None)
singlepart: 4368
---
(b'text', b'plain', (b'charset', b'UTF-8', b'format', b'flowed'), None, None, b'8bit', 3387, 90, None, None, None, None)
singlepart: 3387
---
([([(b'text', b'plain', (b'charset', b'utf-8'), None, None, b'quoted-printable', 41, 2, None, None, None, None), (b'text', b'html', (b'charset', b'utf-8'), None, None, b'quoted-printable', 275, 5, None, None, None, None)], b'alternative', (b'boundary', b'----5C9PD0MHM0TEX79M75OIIFNDOBHL76'), None, None, None), (b'image', b'jpeg', (b'name', b'20200514_112103.jpg'), None, None, b'base64', 2657010, None, (b'attachment', (b'filename', b'20200514_112103.jpg', b'size', b'1941660')), None, None)], b'mixed', (b'boundary', b'----RLFQJ3Q7Y8M4ND8TW7OSOP76VYXBTO'), None, None, None)
Traceback (most recent call last):
  File "move_too_big_imap.py", line 36, in <module>
    size += part[6]
IndexError: tuple index out of range

Why does that last message have ([([(b'text, rather than ([(b'text'??

Extract of my code

    for msgid, data in mail.fetch(smaller, ['BODYSTRUCTURE']).items():
        bodystructure = data[b'BODYSTRUCTURE']
        print("---")
        print(bodystructure)

        if bodystructure:
            if bodystructure.is_multipart:
                size = 0
                for part in bodystructure[0]:
                    size += part[6]
                print("multipart: %d" % size)
            else:
                size = bodystructure[6]
                print("singlepart: %d" % size)

            if size > maxsize:
                print("Size: %d" % size)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions