2

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);
hhsb
  • 560
  • 3
  • 23
Albenss
  • 73
  • 9
  • how are you invoking the code you show here? https://developers.google.com/gmail/add-ons/how-tos/compose – tehhowch Jul 30 '18 at 21:29
  • This is the result of an action attached to a button when I click on "Send this file", this function is invoked. – Albenss Jul 30 '18 at 22:04
  • Is this useful for your situation? You can retrieve the created draft using ``GmailApp.getDrafts()``. And also you can retrieve each message of drafts and update it. You can see the detail information at [here](https://developers.google.com/apps-script/reference/gmail/gmail-draft). – Tanaike Jul 30 '18 at 22:47

2 Answers2

1

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.

hhsb
  • 560
  • 3
  • 23
  • Thanks, I'll check it asap :) – Albenss Jul 31 '18 at 15:42
  • It's working well, but only with an action like this : var button = CardService.newTextButton() .setText('Reply') .setComposeAction(action, CardService.ComposedEmailType.REPLY_AS_DRAFT); Thanks for your help ! – Albenss Aug 01 '18 at 14:56
  • REPLY_AS_DRAFT will reply to the same thread , though. Is that OK ? – hhsb Aug 02 '18 at 05:26
  • This is a very good point, I just changed to "STANDALONE_DRAFT" since I ask the user if he wants to create an empty draft or make an reply, thank you again. – Albenss Aug 02 '18 at 12:43
0
function create_draft_mail_and_get_url() {
    var draft = GmailApp.createDraft(e.parameters.senderEmail, "subject", "body");
    var draft_Id = draft.getMessage().getId();
    var email_Url = "https://mail.google.com/mail/u/0/#drafts?compose=" + draft_Id;
    return email_Url;
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Sun
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '23 at 23:48