How to get permanent email URL in Gmail with Apps Script

Posted in: GmailGoogle Apps Script

All emails in your Gmail inbox have a permanent web address and you can bookmark this URL for quick access to this message in the future. You can save these message links in your task list or meeting notes as they provide important context for the conversation.

The URL for any email is Gmail follows a standard format:<>/#label/<

The UserId is the sequential ID of the currently logged in Gmail account (the default is 0). The Label is the name of the Gmail tag the message is in (or use all). The UniqueId is a unique identifier that Gmail assigns to each message.

The key here is the UniqueId which is assigned internally by Gmail.

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

Here is a simple procedure for sending a base64 encoded email.

const sendGmailMessage = (mimeText) => {
  const GMAIL_API =
  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, there are at least three ways to get the URL (permalink) of the email:

Option 1: Use the standard URL format

const getEmailMessageUrl = (messageId) => {
  return `${messageId}`;

Option 2: Use Apps Script to get thread url

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

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

This is my preferred approach because it is the most reliable. When you send an email, a unique message ID is assigned to the email by the sending service. This message ID is stored in the Message-Id header of the email and is used by your email client to group messages together in the same conversation.

Gmail provide special search operator rfc822msgid to search emails by message ID and we can use this search operator to get email message URL.

const getMessageUrl = (messageId) => {
  const message = GmailApp.getMessageById(messageId);
  const rfc822Id = message.getHeader("Message-Id");
  const searchQuery = `rfc822msgid:<${rfc822Id}>`;
  return `${searchQuery}`;

Related: Get a Second Email Address With Your @gmail Address

Comments are closed.