Last active
October 7, 2018 07:25
-
-
Save oshliaer/8db2131bf7357247bc2b to your computer and use it in GitHub Desktop.
Create a Gmail draft via Google Apps Script #gas #gmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RAW_ = function(){ | |
//this['prOrder_'] = ['from', 'to', 'subject', 'MIME-Version', 'Content-Transfer-Encoding', 'Content-Type']; | |
this['prOrder_'] = ['from', 'to', 'subject', 'MIME-Version', 'Content-Type']; | |
this['from'] = ' '; | |
this['to'] = ' '; | |
this['subject'] = ' '; | |
this['MIME-Version'] = '1.0'; | |
this['Content-Transfer-Encoding'] = 'BASE64'; | |
this['Content-Type'] = 'text/html; charset=UTF-8'; | |
this['body'] = ' '; | |
} | |
RAW_.prototype.toRFC = function(){ | |
var line = ''; | |
for(var i = 0; i < this.prOrder_.length; i++){ | |
line += this.prOrder_[i] + ': ' + this[this.prOrder_[i]] + '\r\n'; | |
} | |
line += '\r\n' + this['body']; | |
return line; | |
} | |
RAW_.prototype.encode = function(){ | |
return Utilities.base64Encode(this.toRFC(), Utilities.Charset.UTF_8).replace(/\//g,'_').replace(/\+/g,'-'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createDraftHTMLEmail(){ | |
var forScope = GmailApp.getInboxUnreadCount(); | |
var r = new RAW_(); | |
r.from = 'Me <' + '[email protected]' + '>'; | |
r.to = 'You <'+ '[email protected]' +'>'; | |
r.subject = 'testing createDraftHTMLEmail'; | |
r.body = '<html><body>' + '<h1>World</h1>Проверка <a href="http://google.com" target="_blank"> LINK </a>' + '</body></html>'; | |
var draftBody = r.encode(); | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, I'm trying to modify your code to pull the contents of a google document, and make that the text of a draft gmail message. Things work OK, but all of the end of line feeds and blank line feeds have been stripped from the message. How could this be modified to work?
I appreciate your help!