title: How to use the Google Apps Script code for creating a Draft email (from 985)?
body: <p>In <a href="https://code.google.com/p/google-apps-script-issues/issues/detail?can=2&amp;start=0&amp;num=100&amp;q=&amp;colspec=Stars%20Opened%20ID%20Type%20Status%20Summary%20Component%20Owner&amp;groupby=&amp;sort=&amp;id=985" rel="nofollow">Issue 985</a>, a developer named "yurec" (perhaps) in comment #29 provides some code to create a draft (text) email in Google Apps Script. Some others thank him for the good work and say that it's tested and working.</p>
  
  <p>I'm a bit stumped as to how to use it and which parts of the code need changing (like "id" and "threadId"?). </p>
  
  <p>Anyway, ideally I guess the usage would be like:</p>
  
  <p>MailApp.createDraft(emailTo, subject, body, {'name':'Bob from Example Ltd'});</p>
  
  <p>But whatever works is good with me. Thanks for any help here. Getting this to work will be fantastico.</p>
  
  <h2>Working Code - You just need to set up the Gmail API as per the selected answer</h2>
  
  <pre><code>function createDraft() {
  
    var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope
  
    var raw = 
        'Subject: testing Draft\n' + 
        'To: my.test.account@gmail.com\n' +
        'Content-Type: multipart/alternative; boundary=1234567890123456789012345678\n' +
        'testing Draft msg\n' + 
        '--1234567890123456789012345678--\n';
  
    var draftBody = Utilities.base64Encode(raw);
  
    var params = {method:"post",
                  contentType: "application/json",
                  headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
                  muteHttpExceptions:true,
                  payload:JSON.stringify({
                  "message": {
                  "raw": draftBody
                }
              })
             };
  </code></pre>