1

I have a sign-in form built in Google Apps Script that sends data to a row in a Google Sheet. But if a user tries to sign in that's not in a preset list, it sends an email to the clerk. The sign in computer has a webcam, and the GAS form can activate the webcam and take a picture of the person; it returns a base64 encoded string of the image data (jpeg format).

What I'm wrestling with is getting GAS to create the proper Blob to add as an inlineImage into the email.

      var userImgBlob = Utilities.newBlob()
        .setContentType("image/jpeg")
        .setDataFromString(Utilities.base64Decode(imgTxt.slice(23)))
        .setName("userImgBlob");

imgTxt contains the data: URI, coming in like:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgM....

Slicing the first 24 (0 to 23) characters removes the header so that it's just the image data. Something about this causes the script to fail, however. (Commenting this section out lets the rest of the script run fine.)

What's malformed about my definition?

NOTE: I've already looked at the How to include inline images in email using MailApp. My question's a little different, because in that post, the user was Fetching a URL from the web. This is a data: URI that I'm working with.

Jeff White
  • 49
  • 5
  • This is a dupcliate of [this thread](https://stackoverflow.com/questions/36178369/how-to-include-inline-images-in-email-using-mailapp) – Dmitry Kostyuk Sep 10 '21 at 17:02
  • Does this answer your question? [How to include inline images in email using MailApp](https://stackoverflow.com/questions/36178369/how-to-include-inline-images-in-email-using-mailapp) – Dmitry Kostyuk Sep 10 '21 at 17:02
  • Actually, it's not quite a duplicate. I found that thread already - that user is fetching an actual URL, not using a data: URI. – Jeff White Sep 10 '21 at 17:42

1 Answers1

3

Well, $%&$#. Now I feel dumb. So a script I had seen elsewhere created a newBlob() and then set parameters after creation.

Turns out the Utilities.newBlob() function doesn't like creation with no parameters.

Put it in a single declaration:

var userImgBlob = Utilities.newBlob(Utilities.base64Decode(imgTxt.slice(23)),"image/jpeg","userImgBlob");

and boom - Bob's your uncle.

Reference: Class Utilities - Apps Script

Ron M
  • 5,791
  • 1
  • 4
  • 16
Jeff White
  • 49
  • 5