In Issue 985, 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.
I'm a bit stumped as to how to use it and which parts of the code need changing (like "id" and "threadId"?).
Anyway, ideally I guess the usage would be like:
MailApp.createDraft(emailTo, subject, body, {'name':'Bob from Example Ltd'});
But whatever works is good with me. Thanks for any help here. Getting this to work will be fantastico.
Working Code - You just need to set up the Gmail API as per the selected answer
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
}
})
};