gpt4 book ai didi

google-apps-script - 自动将带有 Gmail 标签的电子邮件转换为 PDF 并将其发送到电子邮件地址

转载 作者:行者123 更新时间:2023-12-04 04:54:30 25 4
gpt4 key购买 nike

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

  • 自动选择带有特定标签的电子邮件
  • 将电子邮件正文转换为 html
  • 将 html 转换为 pdf
  • 将正文和附件的 pdf 通过电子邮件发送到 IFTTT(它会自动将附件保存到 Dropbox)
  • 删除临时文件
  • 移除标签

  • 该脚本工作并生成 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] 出现第 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]);
    }
    }

    代码已通过 prettifier .

    关于google-apps-script - 自动将带有 Gmail 标签的电子邮件转换为 PDF 并将其发送到电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16946168/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com