'Displays an existing message in a separate window by gmail API?

I'm developing javascript add-on where part of functionality is reminder. User entered some data and set what this email will be sent for example tomorrow.
My idea/task is to make possible what on pressing for example btn in my add-in it will open in gmail the needed email. What I have is id of the email that I need re-open in gmail.

scheme of calls

Is it possible, if you know needed email id, in the gmail to open it again by add-in in separate window? Assuming you clicked btn in your add-on? how should it looks like

Any ideas? btw I am using gmail api for my rest calls to app



Solution 1:[1]

Depending on what you want to accomplish, I'm thinking of two alternatives.

Open existing message in a new window:

Use setOpenLink or setOnClickOpenLinkAction to open a URL in a new tab when a button is clicked.

This tab can be full size or as a popup (via setOpenAs):

var message = GmailApp.getMessageById(messageId);
var url = message.getThread().getPermalink();
var openLinkButton = CardService.newTextButton()
    .setText('Open link')
    .setOpenLink(CardService.newOpenLink()
      .setUrl(url)
      .setOpenAs(CardService.OpenAs.OVERLAY));
var buttonSet = CardService.newButtonSet()
    .addButton(openLinkButton);
var section = CardService.newCardSection()
    .addWidget(buttonSet);
var card = CardService.newCardBuilder()
    .addSection(section);
return card.build();

Create and display a draft:

Use setComposeAction to create and display a draft as a popup when a button is clicked. This draft can be standalone or a response to whatever message id you provide.

For example, in the sample below, a draft is created and displayed when clicking the button Compose Reply:

function createCard(e) {
  var composeAction = CardService.newAction()
      .setFunctionName('createReplyDraft');
  var composeButton = CardService.newTextButton()
      .setText('Compose Reply')
      .setComposeAction(
          composeAction,
          CardService.ComposedEmailType.REPLY_AS_DRAFT);
  var buttonSet = CardService.newButtonSet()
      .addButton(composeButton);
  var section = CardService.newCardSection()
      .addWidget(buttonSet);
  var card = CardService.newCardBuilder()
      .addSection(section);
  return card.build();
}

function createReplyDraft(e) {
  // ...Get messageId ...
  var message = GmailApp.getMessageById(messageId);
  var draft = message.createDraftReply("I'm a draft!");
  return CardService.newComposeActionResponseBuilder()
      .setGmailDraft(draft).build();
}

Reference:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Iamblichus