Search code examples
google-apps-scriptgmail-api

Google Apps Script - createDraft() cannot update name and replyTo?


I found that the options.name and options.replyTo cannot be set to the new draft, while it works properly in direct sending.

is the result normal? OR any problems in the below code?

function pdfToClient(recipients, cliName, docblob, draftORsent){

  var subject = 'Quotation is attched';

  var string = 'Dear '+ cliName + ', <br><br> Please find the quotation as attached. <br><br>Regards,<br>ABC Company';

  var options = {};
      options.name = 'ABC Company';
      options.replyTo = 'k@abc.com';
      options.htmlBody = string; 
      options.attachments = [docblob];
  
  if (draftORsent=='sent') {
    MailApp.sendEmail(recipients, subject, string, options);
  } else if (draftORsent=='draft') {
    GmailApp.createDraft(recipients, subject, string, options);
  };  
}

Solution

  • As the name suggests, createDraft(recipient, subject, body, options) is used to create a new draft.

    If you want to update an existing draft email, then you need to use update(recipient, subject, body, options).

    For example, this

    var draft = GmailApp.getDrafts()[0];
    draft.update(recipient, subject, body, options) 
    

    will update the first draft message in the drafts folder.

    • If your goal is to create a new draft message, then your code works properly.
    • If you want to update an existing draft message, then you need to find the draft message and then use update to update it.