Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

google apps script - Automatically convert emails with a Gmail label to PDF and send it to an email address

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]);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...