【问题标题】:GmailMessage to PDFGmail邮件到PDF
【发布时间】:2012-06-17 06:50:40
【问题描述】:

Google Apps 脚本 - Gmail

会实现GmailMessage (GmailThread) .getAsPdf() 方法吗?预期的输出将与 Gmail 中可用的打印到 PDF 相同。这个功能在网站上有,为什么在Script中没有呢?

这是将选定的 Gmail 对话以 PDF 格式快速分发给其他人/外部人员所必需的。

另外,GmailMessage.getAttachments()虽然在网上文档中,但在现实中并不存在。会实施吗?

谢谢

【问题讨论】:

    标签: gmail google-apps-script


    【解决方案1】:

    我试过这个并且效果很好(不确定这是唯一的方法):

    function getattach(){
    var firstThread = GmailApp.getInboxThreads(0,1)[0];
    var message = firstThread.getMessages()[0];
    var attach = message.getAttachments();
    Logger.log(attach[0].getDataAsString() )
    if(attach.length>0){
    var file=DocsList.createFile(attach[0])
    var pdf=file.getAs('application/pdf').getBytes();
    // for test purpose I send the pdf as attachment
    var attach_to_send = {fileName: 'pdftest.pdf',content:pdf, mimeType:'application/pdf'};
       MailApp.sendEmail('emailadress@gmail.com', 'Your test as PDF ', 'see attachment', {attachments:[attach_to_send]});
    file.setTrashed(true);// delete after use ;-)
    }
    }
    

    编辑1:删除

    编辑2:这是一个带有pdf附件正文的新版本,也支持html(使用DocsList服务),临时文档被删除。一言以蔽之:非常满意;-)

    function getAttachAndBody(){
      var firstThread = GmailApp.getInboxThreads(0,1)[0];
      var message = firstThread.getMessages()[0];
      var attach = message.getAttachments();
      var body = message.getBody();//is a string
      var bodydochtml = DocsList.createFile('body.html', body, "text/html")
      var bodyId=bodydochtml.getId()
      var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();
      if(attach.length>0){
        var file=DocsList.createFile(attach[0])
        var pdf=file.getAs('application/pdf').getBytes();
        var attach_to_send = {fileName: 'pdftest.pdf',content:pdf, mimeType:'application/pdf'};
        var body_to_send = {fileName: 'body.pdf',content:bodydocpdf, mimeType:'application/pdf'};
        MailApp.sendEmail('emailadress@gmail.com', 'transfer email as pdf : body & attachment', 'see attachment', {attachments:[attach_to_send,body_to_send]});
        file.setTrashed(true);
        DocsList.getFileById(bodyId).setTrashed(true)
        }
    }
    

    【讨论】:

    • var attach = message.getAttachments();
    • 您应该确保收件箱中的最后一封邮件有附件(可转换为 pdf),此测试功能缺少一些检查例程......它只是对 getAs pdf 功能的测试;-)
    • getAttachments 现在可以使用了。不知道之前发生了什么。 PDF 格式的 gmail 消息怎么样?
    • 只是提到我的答案已更新为 EDIT2 效果很好;-)
    猜你喜欢
    • 2015-11-14
    • 2018-11-11
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2019-06-02
    • 2013-01-21
    • 2015-10-20
    相关资源
    最近更新 更多