3
votes

I have specific incoming emails assigned different labels and I have threading (conversation view) disabled in settings. I can enter a search in the web app that returns the specific messages I want,

eg: "label: customer01 label:report"

However using the exact same filter with the API returns the thread and all messages there-in that Google has decided are part of the same conversation (even though in the real world they are not) which means my script processes messages it should not in addition to those it should.

eg: var threads = GmailApp.search(gSearchExp); // where gSearchExp is the aforementioned filter

Is there a way I can search for and return messages, NOT threads?

1

1 Answers

3
votes

How about using Gmail API? I think that by using Gmail API, you can retrieve the message with the specific labels. The sample script is as follows.

In order to use this, please enable Gmail API at Advanced Google Services and API console. You can see how to do it at here.

Sample script:

var userId = "me";
var query = "label:customer01 label:report";
var res = Gmail.Users.Messages.list(userId, {q: query});
var ids = res.messages.map(function(e){return e.id});
Logger.log(ids) // Message IDs with the specific labels.

Note:

  • In this sample script, the message IDs with the specific labels are retrieved. For example, if you want to retrieve the message bodies, please retrieve them using the retrieved message IDs.
  • Please modify var userId = "me" to your environment.

Reference:

If I misunderstand your question, please tell me. I would like to modify it.