Google app script: Send mail, inlineImage from js File

纵饮孤独 提交于 2019-12-13 03:45:32

问题


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);
}

回答1:


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.

  • Bottleneck legal values
  • Forms


来源:https://stackoverflow.com/questions/52588200/google-app-script-send-mail-inlineimage-from-js-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!