MailApp.sendEmail中的Blob附件? [英] Blob attachments in MailApp.sendEmail?

查看:58
本文介绍了MailApp.sendEmail中的Blob附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF,并获取其内容并将其存储在Blob中.但是,我无法使用MailApp.sendEmail()将这个Blob作为附件附加到电子邮件中选项的附件参数说通过电子邮件发送的文件.每个项目都是具有以下属性的JavaScript对象:字符串fileName,字符串mimeType(可选)和字符串内容."

I have a PDF and fetch its contents and store it in a Blob. However, I"m not able to attach this blob as an attachment to an email using MailApp.sendEmail() The attachments paramter of options says "files to send in an email. Each item is a JavaScript objects with the following properties: String fileName, String mimeType (optional) and String content."

虽然,我可以将mimeType设置为'application/pdf',但它不起作用-可能是由于所涉及的编码.这是示例代码

Although, I can set the mimeType to 'application/pdf', it doesn't work - probably because of the encoding involved. Here is a sample code

     var resp = UrlFetchApp.fetch(link); 
      if (resp.getResponseCode() == 200){
        var blob = Utilities.newBlob(resp.getContent());
        Logger.log(blob.getDataAsString());
        //            var pdf = blob.getAs('application/pdf'); 
        var options = {'attachments' : 
                       {'fileName' : 'test',
                        'mimeType' : 'application/pdf',
                        'content' : blob.getDataAsString() //Doesn't work 
                       }
                      };
        MailApp.sendEmail(TO_EMAIL, 'Subject','', options);
      }

推荐答案

我们需要更新这些文档...实际上,您可以直接附加Blob,而无需具有文件名,mimetype等的对象.

We need to update those docs... you can actually attach Blobs directly without need for the object with filename, mimetype, etc.

 var resp = UrlFetchApp.fetch("www.google.com"); 
 if (resp.getResponseCode() == 200){

FetchResponse对象(又称我们称为"resp"的变量)具有"getBlob"方法,这意味着您可以在想要使用blob的任何地方使用它,而无需执行任何特殊操作:

The FetchResponse object (aka the variable we are calling 'resp') has the "getBlob" method on it, which means that you an use it wherever you'd want to use a blob without doing anything special:

   MailApp.sendEmail(TO_EMAIL, 'Subject', '', {attachments: resp});

或者,您可以从FetchResponse中获取一个明确的Blob,这只是用一些额外的方法将数据包装起来的好方法.唯一明显的原因是更改文件名或mime类型,尽管请注意,UrlFetchApp已将其设置为合理的默认值(因此,例如,如果您下载了名为MyFile.pdf的pdf,则名称和mime类型将已经为您设置为"MyFile.pdf"和"application/pdf".)

Alternatively, you can get an explicit Blob from the FetchResponse, which is just a nice wrapper around the data with some extra methods. The only obvious reason to do this would be to change the filename or mime type, although note that UrlFetchApp already sets these to reasonable defaults (so if for example you downloaded a pdf named MyFile.pdf, the name and mime type will already be set for you as 'MyFile.pdf' and 'application/pdf').

   var blob = resp.getBlob();
   blob.setName('test');
   MailApp.sendEmail(TO_EMAIL, 'Subject', '', {attachments: blob});
}

这篇关于MailApp.sendEmail中的Blob附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆