Extract Gmail Thread ID from Email URL using a Script

238 views Asked by At

I'm trying to develop a script that can extract the Gmail thread ID from the URL of a specific email. The goal is to automate the process of retrieving the thread ID for further processing.

For example, given the URL of an email in Gmail:

https://mail.google.com/mail/u/0/#inbox/ABCDEF1234567890

I want to extract the thread ID programmatically.

I've been searching for a solution, but I couldn't find any clear guidance on how to achieve this using Google Apps Script or other scripting languages.

Is there a way to extract the Gmail thread ID from the email URL using a script? Any insights, sample code, or suggestions would be greatly appreciated. Thank you in advance!

1

There are 1 answers

2
Twilight On

Here is a suggested solution for extracting gmail thread ID as per your example above:

function myfunction() {
  var url = 'https://mail.google.com/mail/u/0/#inbox/ABCDEF1234567890';
  var regex = /#inbox\/([a-zA-Z0-9]+)(\?|$)/;
  var threadID = new RegExp(regex).exec(url)[1]
  Logger.log(threadID);
}

RESULT:
ABCDEF1234567890

the code above will extract the ID after the word #inbox/ using regex

Ref: Regular Expression in Google Apps Script