Automatically convert emails with a Gmail label to PDF and send it to an email address

后端 未结 2 613
不知归路
不知归路 2021-02-11 06:36

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 c
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-11 07:07

    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.

提交回复
热议问题