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

后端 未结 2 605
不知归路
不知归路 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

    this code I wrote a while ago does the job if the attachement is in a format that can be converted to pdf. It gets the message in the last thread and sends both body and attachment only if an attachment is present. This is a test script.

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

提交回复
热议问题