0

I am using Node.js, Gmail and Gmails API.

I am trying to read messages using gmails API.

To do this I:

async function getMessages(auth){
  const gmail = google.gmail({version: 'v1', auth});

  let theMessages =  await gmail.users.messages.list({
    userId:"me",
    maxResults:10
  })

  let theIds = theMessages.data.messages
  let theId = theIds[0].id

  let response = await gmail.users.messages.get({
    userId:"me",
    id:theId
  })

  let theData = response.data
}

However the response i get is an obeject with this information:

historyId:"1"
id:"1"
internalDate:"1"
labelIds:Array(4) ["UNREAD", "IMPORTANT", "SENT", …]
payload:Object {partId: "", mimeType: "multipart/alternative", filename: "", …}
body:Object {size: 0}
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}
sizeEstimate:578
snippet:"Hey"
threadId:"1"
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}
filename:""
headers:Array(7) [Object, Object, Object, …]
mimeType:"multipart/alternative"
partId:""
parts:Array(2) [Object, Object]

How do i get the body of an email from this information. I've tried looking at the body attribute, but only find a size:0. Part of the message is there as a snippet, but obviously the full message isn't.

any help is appreciated.

throwawayryan
  • 73
  • 1
  • 8
  • Does this answer your question? [Gmail API - Parse message content (Base64 decoding?) with Javascript](https://stackoverflow.com/questions/24745006/gmail-api-parse-message-content-base64-decoding-with-javascript) – Mehdi Sep 03 '20 at 12:13

1 Answers1

1

The message body is available inside response.data.payload, as defined in the documentation:

payload: The parsed email structure in the message parts.

MessagePart section provides further details about the data structure, it is necessary to decode the base-64 encoded contents.

Mehdi
  • 7,204
  • 1
  • 32
  • 44