Reputation: 1
I have a simple document/template I would like to use when sending email to user. It works fine for text version, but I am unable to figure out, how to send the document in HTML format. The document only includes text with some bold formatting.
function sendEmail(emailAddress, attachment){
var EMAIL_TEMPLATE_ID = "SOME_GOOGLE_DOC_ID";
var emailTemplate = DocumentApp.openById(EMAIL_TEMPLATE_ID);
MailApp.sendEmail(emailAddress, "Subject", emailTemplate.getText(), {
htmlBody: emailTemplate, <-- THIS does not return correct data });
}
Upvotes: 0
Views: 2761
Reputation: 31
The answer I found is this one
function getDocAsHtml(docId){
var url = 'https://docs.google.com/feeds/download/documents/Export?exportFormat=html&format=html&id=';
return UrlFetchApp.fetch(url+docId).getContentText();
}
works pretty well
Upvotes: 3
Reputation: 45750
This is fairly straight-forward if we take advantage of Roman Vialard's DriveApp library. You will need to follow the instructions here to add the library to your project, and then you can use the code below.
/**
* get a String containing the contents of the given document as HTML.
* Uses DriveApp library, key Mi-n8njGmTTPutNPEIaArGOVJ5jnXUK_T.
*
* @param {String} docID ID of a Google Document
*
* @returns {String} Content of document, rendered in HTML.
*
* @see https://sites.google.com/site/scriptsexamples/new-connectors-to-google-services/driveservice
*/
function getDocAsHTML(docID) {
var doc = DriveApp.getFileById(docID);
var html = doc.file.content.src;
var response = UrlFetchApp.fetch(html);
var template = response.getContentText();
return template;
}
function sendEmail(emailAddress, attachment){
var EMAIL_TEMPLATE_ID = 'SOME_GOOGLE_DOC_ID';
var emailTemplate = getDocAsHTML(EMAIL_TEMPLATE_ID);
MailApp.sendEmail(emailAddress, "Subject", emailTemplate.getText(), {
htmlBody: emailTemplate });
}
Upvotes: 0
Reputation: 7965
The htmlBody
parameter expects a string while emailTemplate
variable is of type Document.
You should use something like
MailApp.sendEmail(emailAddress, "Subject", emailTemplate.getText(), {
htmlBody: emailTemplate.getBody().getText() });
Upvotes: 0