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.

3
  • This is a dupcliate of this thread Commented Sep 10, 2021 at 17:02
  • Does this answer your question? How to include inline images in email using MailApp Commented Sep 10, 2021 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. Commented Sep 10, 2021 at 17:42

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.