Your script only looks at the first level of parts, so attachments nested deeper get skipped. By recursively traversing all parts and checking both `attachmentId` and inline `data`, you should be able to download the images correctly.
To fix it, you need to recursively walk through all parts of the message payload and handle both cases (`attachmentId` vs inline `data`).
Try this:
def save_attachments(service, msg_id, payload, save_dir="attachments"):
attachments = []
def walk_parts(parts):
for part in parts:
filename = part.get("filename")
mime_type = part.get("mimeType")
body = part.get("body", {})
if filename and (body.get("attachmentId") or body.get("data")):
if body.get("attachmentId"):
attach_id = body["attachmentId"]
attachment = service.users().messages().attachments().get(
userId="me", messageId=msg_id, id=attach_id
).execute()
data = attachment.get("data")
else:
# Inline base64 data
data = body.get("data")
if data:
file_data = base64.urlsafe_b64decode(data)
filepath = os.path.join(save_dir, filename)
with open(filepath, "wb") as f:
f.write(file_data)
attachments.append(filename)
# Recurse into nested parts
if "parts" in part:
walk_parts(part["parts"])
walk_parts(payload.get("parts", []))
return attachments
(PS: I am new to stackoverflow, so all comments are appreciated)