将外发邮件移至回收站

Moving an outgoing message to trash

我有一个脚本可以接收来自特定发件人的邮件 并将它们发送到另一封电子邮件 代码是:

function send() {
  let addressForSearch = 'aaaaaa@mail.com' // the senders email
  let emailForTransfer = 'bbbbbb@mail.com' // the final recipients email
  let messages = GmailApp.search(`from:${addressForSearch}`)
  if (messages.length > 0) {
    messages.forEach(function (m) {
      let message = m.getMessages()[0]
      let subject = message.getSubject()
      let body = message.getBody()
      let attachments = message.getAttachments()
      MailApp.sendEmail(emailForTransfer, subject, '', {
        htmlBody: body,
        attachments: attachments
      })
    })
  }
}

问题是,我如何删除(移至垃圾箱)我通过脚本发送并现在位于 'is sent' 中的邮件?

发送电子邮件后,您可以为发送给最终收件人的电子邮件创建另一个搜索并使用 GmailApp:moveThreadsToTrash(threads)

将此添加到您的代码中:

 let threads = GmailApp.search(`to:${emailForTransfer}`)
  if (thread.length > 0) {
    GmailApp.moveThreadsToTrash(threads);
  }

此外,请注意 MailApp 的功能有限(仅限 getRemainingDailyQuota() 和 sendEmail())。参见 documentation

好的,我所做的是,我在邮件中插入了一个隐藏的 Uuid,这样我就有了一种搜索特定电子邮件的方法。这是代码

function redirect() {
  let emailForTransfer = '****123@gmail.com' // the final recipients email
  let addressForSearch = GmailApp.search(`from:***123@gmail.com is:unread`) //the senders email
  if (addressForSearch.length > 0) {
    for (let formessage of addressForSearch) {
      let message = formessage.getMessages()[0]
      let subject = message.getSubject()
      let body = message.getBody()
      let attachments = message.getAttachments()
      var searchTerm = Utilities.getUuid();
      var uidText = '<span style="color:transparent; display:none !important; height:0; opacity:0; visibility:hidden; width:0">' + searchTerm +  '</span>';
      GmailApp.sendEmail (emailForTransfer, subject, '', {
        htmlBody: body + uidText,
        attachments: attachments
        });
        Utilities.sleep(2000);
        GmailApp.search(searchTerm)[0].moveToTrash()
    }
  }
}