0

I use google.script.run to use the MailApp API in google app script and I pass a mail object like this :

google.script.run.sendMail(mail);

The mail object is structured like this :

  var mail = {
    to:"",
    cc:"",
    subject:"",
    htmlBody:"",
    inlineImages: inlineImages
  }

inlineImages is a Javascript object that map key string to image data (BlobSource) from google ressources

But then I pass File object in inlineImages I get Failed due to illegal value.

EDIT :

I get the inlineImages like this :

var inlineImages

function createImages(event) {
    var file = event.target.files[0];
    var key = "image"+Object.keys(inlineImages).length;

    inlineImages[key] = file;
}

I also try to get Image object :

function createImages(event) {
    var file = event.target.files[0];
    var key = "image"+Object.keys(inlineImages).length;

    var reader = new FileReader();
    reader.onload = function(e) {
        var img = new Image();
        img.src = e.target.result;

        inlineImages[key] = img;
    }

    reader.readAsDataURL(file);
}
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
Poulpynator
  • 716
  • 5
  • 13
  • Could you show us how you get the `inlineImages` object please. And what's inside Thanks – JSmith Oct 01 '18 at 11:47
  • @JSmith Edit done. Anyway I am actually uploading the images on the drive and then use the ID to get image URL, it's way more easy. – Poulpynator Oct 01 '18 at 12:59
  • not sure to well undestand the whole process but is your final object like so `inlineImages: {variableName: imageObject}`?? – JSmith Oct 01 '18 at 13:10
  • Yep, I have the object mapping image with `inlineImages{key: FileObject}` every File have name, last update, ... properties. – Poulpynator Oct 01 '18 at 13:24
  • what is exactly the event object in creatImages how o you trigger this function? – JSmith Oct 01 '18 at 13:25
  • Trigger with `document.getElementById('importImage').addEventListener('change', createImage, false);` where *importImage* is the ID of an input – Poulpynator Oct 01 '18 at 13:27
  • The thing is I'm not quite sure even if I don't use `blob`that often that the `blob` requested by `inlineImages` is the same as the one you try to pass. – JSmith Oct 01 '18 at 13:30
  • I think so, but google don't explain that much ... I will stay on URL for my img. Possible answer : `reader.onloadend = function(e) { inlineImages['test'] = e.target.result;`} – Poulpynator Oct 01 '18 at 13:46
  • try reaing [this post](https://stackoverflow.com/questions/41994351/how-do-i-convert-jpg-or-png-to-blob-on-google-app-script) maybe it will help – JSmith Oct 01 '18 at 13:58

1 Answers1

0

FileObject is not a legal value.

Legal parameters and return values are JavaScript primitives like a
+ Number,
+ Boolean,
+ String, or
+ null, as well as
+ JavaScript objects and arrays that are composed of primitives, objects and arrays.
+ A form element within the page is also legal as a parameter, but it must be the function’s only parameter, and it is not legal as a return value

The only way you're going to get a file past through the above restrictions from client to server are if you convert the fileObject to one of the above types. I'll go with form:

If you call a server function with a form element as a parameter, the form becomes a single object with field names as keys and field values as values. The values are all converted to strings, except for the contents of file-input fields, which become Blob objects.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Yep, in fact `reader.onloadend = function(e) { inlineImages['test'] = e.target.result;}` where *e.target.result* is the file in string format (with file name, type and other info). Thanks for this I will check it for mail attachement – Poulpynator Oct 02 '18 at 07:54