Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google apps script - gmail - return message (not thread) via filter

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?

like image 282
9cents Avatar asked Sep 14 '25 01:09

9cents


1 Answers

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:

  • Users.messages: list

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

like image 152
Tanaike Avatar answered Sep 17 '25 12:09

Tanaike