自动将带有Gmail标签的电子邮件转换为PDF并将其发送到电子邮件地址 [英] Automatically convert emails with a Gmail label to PDF and send it to an email address

查看:99
本文介绍了自动将带有Gmail标签的电子邮件转换为PDF并将其发送到电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将在GMail中收到的收据(来自亚马逊)自动保存到Dropbox.因此,我编写了一个脚本,该脚本为:

  1. 自动选择带有特定标签的电子邮件
  2. 将电子邮件正文转换为html
  3. 将html转换为pdf
  4. 将正文和附件的pdf电子邮件发送到IFTTT(这会自动将附件保存到保管箱)
  5. 删除临时文件
  6. 删除标签

该脚本可以工作并生成bodydochtml,但是PDF转换和电子邮件不起作用.我盯着这个剧本好几个小时.我的脚本中的错误在哪里?

谢谢!

 函数send_Gmail_as_PDF(){var gLabel ="#Receipt";var thread = GmailApp.search("label:" + gLabel);for(var x = 0; x< thread.length; x ++){var messages = thread [x] .getMessages();对于(var y = 0; y  

解决方案

thread [x] 为null时,第46行出现错误.由于您已经在处理线程[x]的循环之外获得了该语句,因此您始终为空.将语句移到循环中,可以避免此问题.

在您的消息循环中,您检查消息是否具有任何附件, if(attach.length> 0){,并且仅在确实存在的情况下继续处理该消息.您是否要继续发送仅包含pdf正文的电子邮件?如果是这样,我们需要对 {attachments:[attach_to_send,body_to_send]} 中的固定数组进行处理.最好是从 body_to_send 开始构建一系列附件.添加:

  var attachmentList = [];attachmentList.push(body_to_send); 

更好的是,如果邮件中包含多个附件怎么办-您希望它们全部都包含.为此,请将附件处理放入循环中,而不要使用 if ,并确保将临时文件与之一起整理.(无论如何,它应该在 if 内部,因为如果没有附件,则 setTrashed()调用将崩溃.)

 //处理所有附件for(var att = 0; att< attach.length; att ++){...attachmentList.push(attach_to_send);//删除临时文件file.setTrashed(true);} 

这是您的代码,进行了以下更改-效果很好:

 函数send_Gmail_as_PDF(){var gLabel ="#Receipt";var thread = GmailApp.search("label:" + gLabel);for(var x = 0; x< thread.length; x ++){var messages = thread [x] .getMessages();for(var y = 0; y< messages.length; y ++){var attach = messages [y] .getAttachments();var body = messages [y] .getBody();//从消息正文创建HTML文件var bodydochtml = DocsList.createFile('body.html',body,"text/html")var bodyId = bodydochtml.getId()//将HTML转换为PDFvar bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();var body_to_send = {fileName:'body.pdf',内容:bodydocpdf,mimeType:应用程序/pdf"};var attachmentList = [];attachmentList.push(body_to_send);//删除临时文件bodydochtml.setTrashed(true);//处理所有附件for(var att = 0; att< attach.length; att ++){var file = DocsList.createFile(attach [att]);var pdf = file.getAs('application/pdf').getBytes();var attach_to_send = {fileName:'pdftest.pdf',内容:pdf,mimeType:应用程序/pdf"};attachmentList.push(attach_to_send);//删除临时文件file.setTrashed(true);}}//将PDF发送到任何电子邮件地址MailApp.sendEmail('myemail@gmail.com','将电子邮件以pdf格式传输:正文&附件','见附件', {附件:attachmentList});//消息已处理;移除Google云端硬盘标签GmailApp.getUserLabelByName(gLabel).removeFromThread(thread [x]);}} 

代码是通过修饰词输入的.

I am trying to automatically save the receipts (from Amazon) I receive in GMail to Dropbox. So I have written a script that:

  1. automatically select emails with a certain label
  2. converts the body of the email to html
  3. converts the html to pdf
  4. email the pdf of the body and attachment to IFTTT (which automatically saves the attachments to dropbox)
  5. deletes the temporary files
  6. removes the label

The script works and generates the bodydochtml, but the PDF conversion and email don't work. I am staring at this script for hours. Where is the error in my script?

Thank you!

Function send_Gmail_as_PDF(){

  var gLabel  = "#Receipt";
  var thread = GmailApp.search("label:" + gLabel);
  for (var x=0; x<thread.length; x++) {    
     var messages = thread[x].getMessages();  

     for (var y=0; y<messages.length; y++) {  
       var attach  = messages[y].getAttachments();
       var body    = messages[y].getBody(); 

       // Create an HTML File from the Message Body
       var bodydochtml = DocsList.createFile('body.html', body, "text/html")
       var bodyId=bodydochtml.getId()

       // Convert the HTML to PDF
       var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();

       // Does the Gmail Message have any attachments?
       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'};

       // Send the PDF to any email address
       MailApp.sendEmail('myemail@gmail.com', 
                         'transfer email as pdf : body & attachment', 
                         'see attachment', {attachments:[attach_to_send,body_to_send]});

     // Trash the temporary PDF and HTML files
     file.setTrashed(true);
     DocsList.getFileById(bodyId).setTrashed(true)
     }
   }
}
   // Message Processed; Remove the Google Drive Label
 GmailApp.getUserLabelByName(gLabel)
         .removeFromThread(thread[x]);
}

解决方案

The error on line 46 is coming up when thread[x] is null. Since you've got that statement outside of the loop that deals with thread[x], you ALWAYS have null. Move the statement into the loop, and this problem is avoided.

Inside your message loop, you check whether the message has any attachments, if(attach.length>0){ and only continue with the message if it DOES. Wouldn't you want to continue to send an email with only the pdf body? If so, we need to do something about the fixed array in {attachments:[attach_to_send,body_to_send]}. Better would be to build an array of attachments as we go, starting with the body_to_send. Add:

      var attachmentList = [];
      attachmentList.push(body_to_send);

Better yet, what if the message has multiple attachments - you'd want them all. To do that, put the attachment handling inside a loop, instead of an if, and make sure to move the temp file tidy-up along with it. (That should have been inside the if anyway, because if there was no attachment, the setTrashed() call would crash.)

      // Process all attachments
      for (var att = 0; att < attach.length; att++) {    
        ...
        attachmentList.push(attach_to_send);

        // Trash the temporary file
        file.setTrashed(true);
      }

Here's your code, with these changes - it works nicely:

function send_Gmail_as_PDF() {

  var gLabel = "#Receipt";
  var thread = GmailApp.search("label:" + gLabel);
  for (var x = 0; x < thread.length; x++) {
    var messages = thread[x].getMessages();

    for (var y = 0; y < messages.length; y++) {
      var attach = messages[y].getAttachments();
      var body = messages[y].getBody();

      // Create an HTML File from the Message Body
      var bodydochtml = DocsList.createFile('body.html', body, "text/html")
      var bodyId = bodydochtml.getId()

      // Convert the HTML to PDF
      var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();

      var body_to_send = {
        fileName: 'body.pdf',
        content: bodydocpdf,
        mimeType: 'application/pdf'
      };

      var attachmentList = [];
      attachmentList.push(body_to_send);

      // Trash the temporary file
      bodydochtml.setTrashed(true);

      // Process all attachments
      for (var att = 0; att < attach.length; att++) {

        var file = DocsList.createFile(attach[att]);
        var pdf = file.getAs('application/pdf').getBytes();

        var attach_to_send = {
          fileName: 'pdftest.pdf',
          content: pdf,
          mimeType: 'application/pdf'
        };
        attachmentList.push(attach_to_send);

        // Trash the temporary file
        file.setTrashed(true);
      }
    }

    // Send the PDF to any email address
    MailApp.sendEmail('myemail@gmail.com',
      'transfer email as pdf : body & attachment',
      'see attachment', {
        attachments: attachmentList
      });

    // Message Processed; Remove the Google Drive Label
    GmailApp.getUserLabelByName(gLabel)
      .removeFromThread(thread[x]);
  }
}

Code was put through a prettifier.

这篇关于自动将带有Gmail标签的电子邮件转换为PDF并将其发送到电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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