Get emails that contains attachment using Gmail's API JavaScript

Viewed 111

I'm trying to get a list of emails that contain attachments, I'm getting a list of messages, each message contains 2 properties - id and threadID - I assumed that since the messages do not hold an AttachmentID property, they do not have one either.

I didn't find a way to retrieve only messages with attachments.

This is the code:

function execute() {
    return gapi.client.gmail.users.messages.list({
        "userId": "myid@gmail.com",
        "includeSpamTrash": false,
        "maxResults": 100,
    }).then(function (response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
            },
            function (err) { console.error("Execute error", err); });
}
2 Answers

To retrieve only the messages that contains an attachment, you should use the method users.messages.list while including a q parameter to show only messages with attachments. That q parameter can be set up with the string has:attachment. On JavaScript that requests looks like the following:

  function execute() {
    return gapi.client.gmail.users.messages.list({
      "userId": "{ YOUR USERID HERE }",
      "q": "has:attachment"
    })
        .then(function(response) {
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }

Please remember to update the userId parameter to match your own scenario.

UPDATE

By reading your new comment I understand that your end goal is to retrieve the attachmentId field. You can do so very easily just by expanding the above request. You only have to keep in mind that the above function returns a message id that can be used to retrieve a Message object containing a MessagePart inside the payload field. That same MessagePart object contains a MessagePartBody object inside the body field. Finally, that MessagePartBody contains the attachmentId field.

To make it clearer I will describe the process step by step:

  1. You run the above users.messages.list function and get multiple id in return.
  2. For each id, you run a users.messages.get function to get a Message object. That function can be similar to this example:
  function execute() {
    return gapi.client.gmail.users.messages.get({
      "userId": "{ YOUR USERID HERE }",
      "id": "{ THE MESSAGE ID HERE }"
    })
        .then(function(response) {
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  1. The step above will return a Message object with multiple MessagePart inside. Since you are only interested in the attachments MessagePart then you need to interact every one of them checking that the filename field isn't empty (because only attachments MessagePart have a populated filename field). Please bear in mind that there is one MessagePart per every attachment in the email.
  2. Now you have identified which MessagePart contains info about the attachment. Finally, you only need to check the body property of that MessagePart, and read the attachmentId field inside.
function execute() {
  return gapi.client.gmail.users.messages.list({
    "userId": "myid@gmail.com",
    "q": "has:attachment", // This right here
    "includeSpamTrash": false,
    "maxResults": 100,
  })
    .then(
      function (response) {
        // Handle the results here (response.result has the parsed body).
        console.log("Response", response);
      },
      function (err) {
        console.error("Execute error", err)
      }
    )
}

The "q" parameter has a horde of options you can use to filter the output. This also works in the Gmail UI.

Related