Answer
The structure of the response body changes depending on the message itself. You can do some test to check how they look like in the documentation of the method: users.messages.get
How to manage it
Get the message with the id
and define the parts
.
msg = service.users().messages().get(userId='me', id=message_id['id']).execute()
payload = msg['payload']
parts = payload.get('parts')
You can find the raw version of the body message in the snippet
, as the documentation says, it contains the short part of the message text
. It's a simple solution that returns you the message without formatting or line breaks. Furthermore, you don't have to decode the result. If it does not fit your requirements, check the next solutions.
raw_message = msg['snippet']
Add a conditional statement to check if any part
of the message has a mimeType
equal to multipart/alternative
. If it is the case, the message has an attachment and the body is inside that part. You have to get the list of subparts
inside that part
. I attach you the code:
for part in parts:
body = part.get("body")
data = body.get("data")
mimeType = part.get("mimeType")
# with attachment
if mimeType == 'multipart/alternative':
subparts = part.get('parts')
for p in subparts:
body = p.get("body")
data = body.get("data")
mimeType = p.get("mimeType")
if mimeType == 'text/plain':
body_message = base64.urlsafe_b64decode(data)
elif mimeType == 'text/html':
body_html = base64.urlsafe_b64decode(data)
# without attachment
elif mimeType == 'text/plain':
body_message = base64.urlsafe_b64decode(data)
elif mimeType == 'text/html':
body_html = base64.urlsafe_b64decode(data)
final_result = str(body_message, 'utf-8')
Use a recursive function to process the parts:
def processParts(parts):
for part in parts:
body = part.get("body")
data = body.get("data")
mimeType = part.get("mimeType")
if mimeType == 'multipart/alternative':
subparts = part.get('parts')
[body_message, body_html] = processParts(subparts)
elif mimeType == 'text/plain':
body_message = base64.urlsafe_b64decode(data)
elif mimeType == 'text/html':
body_html = base64.urlsafe_b64decode(data)
return [body_message, body_html]
[body_message, body_html] = processParts(parts)
final_result = str(body_message, 'utf-8')
Extra comments
- If you need to get more data from your message I recommend you to use the documentation to see how the response body looks like.
- You can also check the method in the API library of Python to see a detailed description of each element.
- Do not use images in this way as DalmTo has said
edit
I tried the code with Python 2, it was my mistake. With Python 3, as you said, you have to use base64.urlsafe_b64decode(data)
instead of base64.b64decode(data)
. I've already updated the code.
I added a simple solution that maybe fits your needs. It takes the message from the snippet
key. It is a simplified version of the body message that does not need decoding.
I also don't know how you have obtained the text/html
part with my code that does not handle that. If you want to get it, you have to add a second if statement, I updated the code so you can see it.
Finally, what you obtained using base64.urlsafe_b64decode
is a bytes
variable, to obtain the string you have to convert it using str(body_message, 'utf-8')
. It is now in the code
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…