Search code examples
google-apps-scriptgmail-addons

Open a draft email after creating it


I would like to know if there is the possibility to open a draft directly after it's creation. After getting a file in a distant server, I would create an empty draft and I would like to open the draft message, so that I can fill it.

Here is my current code, the draft is well created, but I haven't found a way to open it. I need to navigate to "Drafts" section to open it.

var responseDownload = UrlFetchApp.fetch(urlDownload, params);    
    
var theFile = Utilities.newBlob(responseDownload)
  .setName(json.entry.name)
  .setContentTypeFromExtension();

var recipient = "";
var subject = "";
var body = "\n\nThis draft was automatically generated.";
var options = 
    {
      attachments: [theFile]
    };
  
  var draft = GmailApp.createDraft(recipient, subject, body, options);


Solution

  • That's a good question. Unfortunately , CardService is the only way to create addons and it does not control the GmailUI except for opening a compose popup.

    You may refer to this SO post as well.

    Update: To open a compose popup :

    function renderComposeMail(e) {
    // Activate temporary Gmail add-on scopes, in this case to allow
    // a reply to be drafted.
    var accessToken = e.messageMetadata.accessToken;
    GmailApp.setCurrentMessageAccessToken(accessToken);
    
    var draft = GmailApp.createDraft(e.parameters.senderEmail, "subject", "body");
    var card = CardService.newComposeActionResponseBuilder().setGmailDraft(draft);
    return card.build();}
    

    Call this method on button click ,or any desired event.