Search code examples
node.jsgoogle-apigmailgmail-api

Get direct URL to email from Gmail API (list messages)


I am retrieving messages from the Google Gmail API in Node.JS, but i'm having trouble generating or retrieving the direct link to the emails.

Currently, I am using the following code:

var link = 'https://mail.google.com/mail/u/0/#inbox/' + messageContent.threadId;

But as u can see, it always navigates to the user's primary inbox (/u/0/). The emails I am trying to link to are in my second and third inbox.

I tried getting the inbox number of an email using the get message call, but unfortunately it doesn't return that data.

I also tried the following piece of code, but it didn't work either. It just loads my primary inbox.

var link = 'https://mail.google.com/mail/#inbox/' + messageContent.threadId;

Does anybody know how to generate the correct and direct link to an email?

Thanks in advance.


Test case

First, login to two or more accounts in Google. As you can see on the image below, I am logged in to 3 accounts. The first one (on top), is my primary account. The other ones are secondary accounts.

Google Accounts

The direct link to my primary Gmail is: https://mail.google.com/mail/u/0/#inbox/

The direct link to my second account is: https://mail.google.com/mail/u/1/#inbox/

And the link of my third and last account is: https://mail.google.com/mail/u/2/#inbox/

These numbers depend on the order you logged into the accounts. The first account you log in to will always be your primary account.

So lets take 2527bfcqed7d54c1 as example message ID. The only thing thats left is generating the direct link to the message, but the problem is, we only know the e-mailadres it's linked to, and not the number of the inbox (e.g. /u/2).

So if the message exists in inbox /u/2 we can't get the direct link. Because the API does not return the inbox number, and https://mail.google.com/mail/#inbox/2527bfcqed7d54c1 will always redirect to the primary inbox /u/0.


Solution

  • You can supply a query parameter authuser with the value of the email address you would like to use:

    https://mail.google.com/[email protected]#all/1567f47dd61869df
    

    This works for the regular Gmail client, but it does not work for Google Inbox since the id of the thread is not a part of the url. You can however search for the message's unique Message-ID header to get a similar effect:

    https://inbox.google.com/search/rfc822msgid:<[email protected]>[email protected]