自动转换电子邮件与Gmail标签为PDF,并将其发送到电子邮件地址



我试图自动保存收据(从亚马逊)我收到的GMail到Dropbox。所以我写了一个脚本:

  1. 自动选择带有特定标签的邮件
  2. 将邮件正文转换为html
  3. 将html转换为pdf
  4. 将正文和附件的pdf电子邮件发送到IFTTT(它会自动将附件保存到dropbox)
  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]);
}

thread[x]为null时,第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]);
  }
}

代码经过修饰器处理。

根据Google开发者页面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]);
  }
}

如果附件的格式可以转换为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)
    }
}

相关内容

最新更新