Get emails from gmail thread

surely someone has done this before?

Given a threadGmailId, or possibly an array of…
return ALL email addresses in that thread.

When getting a thread I can get all the messages and end up with an array of to: email addresses
will be duplicates and some with different formatting of the actual name and some with only the email address

bob <[email protected]>
bob jones <[email protected]>
[email protected]

etc

and then I want to filtre out the email addresses that belong to me

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

hello @bally

So what is the question? Do you need help to remove duplicates of emails?

Given a threadGmailId
I can get the threads
image

in each of the messages there will be a from, to, reply to

so in the picture shown that would be 6 emails that may or may not be duplicated

I want to collect them all and deduplicate and have it in a single list rather than three different fields…

As above there could be multiple formats, and of course some of them will be my email

I got this from ai,

// Initialize an array to store the email addresses
let emailAddresses = [];

// Regular expression to match the email format
let emailRegex = /<(.*)>/;

// Loop over input items and extract the email addresses
for (const item of $input.all()) {
  if (item.json.To) {
    let match = item.json.To.match(emailRegex);
    if (match) {
      emailAddresses.push(match[1]);
    }
  }
  if (item.json.From) {
    let match = item.json.From.match(emailRegex);
    if (match) {
      emailAddresses.push(match[1]);
    }
  }
  if (item.json['Reply-To']) {
    let match = item.json['Reply-To'].match(emailRegex);
    if (match) {
      emailAddresses.push(match[1]);
    }
  }
}

// Remove duplicates
emailAddresses = [...new Set(emailAddresses)];

// Sort the email addresses
emailAddresses.sort();

return { emailAddresses };

How is it? Seems possibly messy?

I think it will work partially, as there may be multiple ‘To’ persons in one email.

Can you provide a sample (as text) of what the gmail node is returning (just remove any sensitive info from it)?