2

我正在尝试将我在 GMail 中收到的收据(来自亚马逊)自动保存到 Dropbox。所以我写了一个脚本:

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

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

谢谢!

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]);
}
4

3 回答 3

3

thread[x]为空时,第 46 行的错误将出现。由于您在处理线程 [x] 的循环之外有该语句,因此您总是有 null。将语句移到循环中,就避免了这个问题。

在您的消息循环中,您检查消息是否有任何附件,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()调用就会崩溃。)

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

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

这是您的代码,经过这些更改 - 它运行良好:

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]);
  }
}

代码通过了美化器

于 2013-06-05T21:12:18.450 回答
0

根据谷歌开发者页面 DocsList已被弃用。
上面答案中的代码应该改用DriveApp

 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 = DriveApp.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 = DriveApp.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]);
  }
}
于 2022-02-21T11:03:21.597 回答
-1

如果附件的格式可以转换为 pdf,我不久前写的这段代码就可以完成这项工作。它在最后一个线程中获取消息,并且仅在存在附件时才发送正文和附件。这是一个测试脚本。

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('xxxxxxx@gmail.com', 'transfer email as pdf : body ', 'see attachment', {attachments:[body_to_send]});
    MailApp.sendEmail('xxxxxxx@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)
    }
}
于 2013-06-05T20:39:54.663 回答