I'm trying to put some content in my automated reply messages from Google Apps Script. My project has a AutomatedResponseTemplate.html
the script hits for some response HTML string to send. That file is defined like this :
<!DOCTYPE html>
<html>
<head>
<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js></script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.3.1/mustache.js></script>
<script>
$(function() {
let data = <?= JSON.stringify(messageData) ?>
// make request to "server"
template = $('body').html()
rendering= Mustache.render(template, data)
$('body').html(template)
})
</script>
<style>
#closing {
white-space: pre-line;
}
.separate-lines {
white-space: pre-line;
}
.first {
width: 80px;
}
.left {
float: left;
}
</style>
</head>
<body>
<p>
{{name}}
</p>
<p>
Thank you for reaching out to us! This is an automated reply, and one of us shall reach out to you,
for real, shortly. Here is a copy of the message you sent us for reference:
</p>
<p>
<blockquote class=separate-lines>
<span><label class="first left">Sender:</label> {{sender}}</span>
<span><label class="first left">Subject:</label> {{subject}}</span>
<span><label class="first left">Message:</label> {{message}}</span>
</blockquote>
</p>
<p id=closing>
Mike Warren
Open Source Roads
</p>
</body>
</html>
To separate concerns, and keep things RESTful, I have the script send the relevant data to the HTML via that tag. There's a working pure-client-side version prior and I got the idea to do it that way from thinking about it and the implementation here.
There's one problem: it doesn't work. Even if I append some non-template code
$('#data').text('This was inserted by jQuery')
and a tag
<p id=data></p>
...nothing changes.
What in the world is going on?
UPDATE
I updated the <script>
tag to this:
<script>
$(function() {
let data = <?!= JSON.stringify(messageData.data) ?>
// make request to "server"
template = $('body').html()
rendering= Mustache.render(template, data)
$('body').html(rendering)
$('#data').text('This text was inserted by jQuery')
})
</script>
, enclosed the src
values on the client-side dependencies with quotes (idk why that matters to Google Apps Script, as it works fine elsewhere), and provided a doGet
for debugging purposes:
function doGet(e) {
var messageData = {
data: {
sender: 'mwarren04011990@gmail.com',
name: 'Test User',
recipient: Session.getActiveUser().getEmail(),
subject: 'Test email',
message: 'Hello world'
}
}
var template = HtmlService
.createTemplateFromFile('AutomatedResponseTemplate')
template.messageData = messageData
return template
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
}
Also, the function that is supposed to get this client side template rendering as a string has this code:
/**
* Generates an email message body in response to the sender's `messageData`
* @param {object} messageData - a JSON object with at least the following:
* - `sender`
* - `name`
* - `subject`
* - `message`
*/
function getAutomatedResponseTo(messageData) {
messageData = messageData || {};
if (!messageData.sender) return '';
if (!messageData.name) return '';
if (!messageData.subject) return '';
if (!messageData.message) return '';
var template = HtmlService
.createTemplateFromFile('AutomatedResponseTemplate')
template.messageData = messageData;
return template
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.getContent()
}
Is my trying to separate the concerns this way infeasible?