Reputation: 6175
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.
function createDraft() {
var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope
var raw =
'Subject: testing Draft\n' +
'To: [email protected]\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
}
})
};
Upvotes: 1
Views: 3934
Reputation: 21
is it any solutions, to paste on this e-mail part of sheet? f.e.:
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Podsumowanie - raport'), true); var range = sheet.getRange('A1:N46');
Upvotes: 0
Reputation: 847
You only need to setup the Gmail API in the console
In Script Editor:
In Developers Console:
Close the Developers Console tab
Click Ok in the Advanced Google Services modal
Authorize the app, and you're all set.
Here is the code I tested with
function createDraft() {
try{
var forScope = GmailApp.getInboxUnreadCount(); // needed for auth scope
var raw =
'Subject: testing Draft from Apps Script\n' +
//'To: [email protected]\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
}
})
};
var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts", params);
Logger.log(resp.getContentText());
}catch(err){
Logger.log(err.lineNumber + ' - ' + err);
}
}
Screenshot 1
Screenshot 2
Screenshot 3
Upvotes: 3