0

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!

Hamed
  • 23
  • 8
  • That seems pretty easy what is the specific problem you are having? – Cooper Jul 14 '23 at 16:35
  • Just pointing out that if you use Gmail with the preview pane enabled, the url will not show you the thread ID when you navigate to different messages. Apart from that, I would recommend checking out the Gmail API (https://developers.google.com/gmail/api/reference/rest). – Miguel Rivera Rios Jul 14 '23 at 16:44

1 Answers1

2

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

Twilight
  • 1,399
  • 2
  • 11
  • Thanks for your solution. But it only extracts the last part of the URL and logs it as the thread id, while the thread id of Gmail conversation differs from that. Suppose this URL as the unique address of an email `https://mail.google.com/mail/u/0/?zx=vh1f2utryt2z#inbox/FMfcgzGsnLPVnghLGZdspmSNWJqDqrhP` , it is a unique email address but in fact the **FMfcgzGsnLPVnghLGZdspmSNWJqDqrhP** in that address is not the thread id, the thread id is something like this `188f7bc4b3cea995`. this is just an example and my question is how to generate thread id from the unique url of the email? – Hamed Jul 28 '23 at 13:25
  • 1
    I understand, but the title you have provided seems confusing vs to the goal that you want to achieve. I apologize for misunderstanding your concern but here's a way to extract thread ID: [get current thread id in gmail using google app script](https://stackoverflow.com/questions/50267172/get-current-thread-id-in-gmail-using-google-app-script).however, generating thread ID might be complicated which apps script alone might not be enough. – Twilight Jul 31 '23 at 05:01