I'm a bit stuck on a project and could do with some help.
AIM: I'm trying to submit a google form, with various bits of data and an image. That would then trigger a HTML email using the form data and embed the image.
So far I can do everything except embed the image.
Here's what I have so far.
code.gs
function getData() {
var sh = SpreadsheetApp.getActive()
.getSheetByName('Form responses 1');
return sh.getRange(sh.getLastRow(), 1, 1, sh.getLastColumn())
.getValues()[0]
}
function testEmail() {
var htmlBody = HtmlService
.createTemplateFromFile('mail_template')
.evaluate()
.getContent();
var mailADdy = "myemail@email.co.uk";
MailApp.sendEmail({
to:mailADdy,
subject: 'Test Email markup - ' + new Date(),
htmlBody: htmlBody,
});
}
mail_template.html
<!DOCTYPE html>
<html>
<head>
<style>
@media only screen and (max-device-width: 480px) {
/* mobile-specific CSS styles go here */
}
</style>
</head>
<body>
<? var data = getData(); ?>
<? var first = data[2]; ?>
<? var second = data[3]; ?>
<? var third = data[4]; ?>
<? var fourth = data[5]; ?>
/* imageUpload var is the link to the image that's been uploaded, in "https://drive.google.com/open?id=ID" format"*/
<? var imageUpload = data[6]; ?>
/* Below are some things that I've tried and failed */
<? var imageID = imageUpload.replace("https://drive.google.com/open?id=","");?>
<? var imageBlob = UrlFetchApp.fetch(imageUpload).getBlob().setName("imageBlob");?>
<? var imageBlob = DriveApp.getFileById(imageID).getBlob();?>
<div class="main">
<p style="text-align: center;"><strong>This is a test HTML email.</strong></p>
<table style="margin-left: auto; margin-right: auto;">
<tbody>
<tr>
<td style="text-align: left;">First question to be put here</td>
<td style="text-align: right;"><strong><?= first ?></strong> </td>
</tr>
<tr>
<td style="text-align: left;">Second question here</td>
<td style="text-align: right;"><?= second ?></td>
</tr>
<tr>
<td style="text-align: left;">Third question here</td>
<td style="text-align: right;"><?= third ?></td>
</tr>
<tr>
<td style="text-align: left;">Image Upload test</td>
<td style="text-align: right;"><?= imageUpload ?></td>
</tr>
</tbody>
</table>
<p></p>
<p style="text-align: center;"><img src="https://imageurl.com" alt="Logo" /></p>
</div>
</body>
</html>
The getData function returns the last row of data in my sheet (Which will be the recent form submission) testEmail sends the email.
mail_template contains the HTML for the email. Layout etc.
By using scriplets I can get the form values into the email and get it to send.
However whatever I've tried with the image, I can't get it right.
When I try to use the following scriptlet in the HTML file,
<? var imageBlob = UrlFetchApp.fetch(imageUpload).getBlob().setName("imageBlob");?>
I get the following error.
Execution failed: You do not have permission to call fetch
I've also tried:
<? var imageBlob = DriveApp.getFileById(imageID).getBlob();?>
But still not working.
I did try following the info from this link, How to include inline images in email using MailApp
But cannot translate this to my example as they are not using a separate HTML file
Any help/pointers are appreciated.