How to Get the Permanent URL of an Email Message in Gmail with Apps Script easily

Advertisements

Hello, Guys welcome back to learn How to Get the Permanent URL of an Email Message in Gmail with Apps Script easily. When you use Google Apps Script to send an email, the Gmail API returns a unique ID that you can use to get the URL of the email message in your sent items.

All email messages in your Gmail inbox have a permanent web address, which you may bookmark to instantly access that message in the future. These message links can be saved in your task list or meeting notes because they give vital context for the dialogue.

Advertisements
Gmail Email Link Bookmarks

The URL of any email message is Gmail follows a standard format:

https://mail.google.com/mail/u/<<UserId>>/#label/<<Label>>/<<UniqueId>>

The UserId is the sequential ID of the presently logged-in Gmail account (the value 0 is the default). The Label is the name of the Gmail label that contains the message (or use all). The UniqueId is a one-of-a-kind identifier that Gmail assigns to each message.

Advertisements

The key here is  UniqueId that is internally assigned by Gmail.

When you use Google Apps Script to send an email, the Gmail API returns a unique ID that you can use to get the URL of the email message in your sent items.

Advertisements

Here’s a simple procedure to send an email that is base64 encoded.

const sendGmailMessage = (mimeText) => {
  const GMAIL_API = 'https://gmail.googleapis.com/upload/gmail/v1/users/me/messages/send';
  const params = {
    method: 'POST',
    contentType: 'message/rfc822',
    headers: {
      Authorization: `Bearer ${ScriptApp.getOAuthToken()}`,
    },
    payload: mimeText,
  };
  const response = UrlFetchApp.fetch(GMAIL_API, params);
  const { id: messageId } = JSON.parse(response.getContentText());
  return messageId;
};

Now that you have the messageId of the outgoing email message, there are at least three ways to get the URL (permalink) of the email message:

Advertisements

Option 1: Use the standard URL format

const getEmailMessageUrl = (messageId) => {
  return `https://mail.google.com/mail/u/0/#all/${messageId}`;
};

Option 2: Use Apps Script to get the email thread URL

In this approach, we get the associated thread of the email message and then get the URL of the first message in the thread.

const getThreadUrl = (messageId) => {
  const message = GmailApp.getMessageById(messageId);
  return message.getThread().getPermalink();
};

Option 3: Use the Message-Id in Email Header

This is my preferred method because it is the most dependable. When you send an email message, the sending service assigns the email message a unique message ID. This message ID is saved in the email message’s Message-Id header and is used by your email client to group messages in the same conversation.

Advertisements

Gmail has a specific rfc822msgid search operator for searching emails by message ID, which we can use to extract the URL of the email message.

const getMessageUrl = (messageId) => {
  const message = GmailApp.getMessageById(messageId);
  const rfc822Id = message.getHeader('Message-Id');
  const searchQuery = `rfc822msgid:<${rfc822Id}>`;
  return `https://mail.google.com/mail/u/0/#search/${searchQuery}`;
};

Related:

Advertisements

Leave a Comment