You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否用Google Apps Script过滤含动态重复内容的订单通知邮件?

需求可行性与起步代码示例

这个需求完全可以通过Google Apps Script实现,核心思路是提取邮件中的「BILLED TO」信息,结合持久化存储记录已出现过的用户,以此判断是否为重复邮件并执行标记操作。

起步代码模板

function filterSubscriptionEmails() {
  // 1. 定义邮件筛选条件(替换为实际的发件人/主题关键词)
  const searchQuery = "from:电商平台邮箱地址 subject:订阅订单通知"; 
  const threads = GmailApp.search(searchQuery);
  
  // 2. 获取持久化存储,记录已处理过的BILLED TO用户
  const userProperties = PropertiesService.getUserProperties();
  const processedUsers = userProperties.getProperty('processedBilledUsers') ? JSON.parse(userProperties.getProperty('processedBilledUsers')) : [];

  // 3. 遍历邮件线程
  threads.forEach(thread => {
    const messages = thread.getMessages();
    messages.forEach(message => {
      if (message.isUnread()) { // 仅处理未读邮件
        const body = message.getPlainBody(); // 提取纯文本内容避免HTML干扰
        // 4. 匹配「BILLED TO: First Last name」行
        const billedToMatch = body.match(/BILLED TO: (.+)/);
        if (billedToMatch) {
          const billedUser = billedToMatch[1].trim();
          // 5. 判断是否为重复用户
          if (processedUsers.includes(billedUser)) {
            // 重复邮件标记为已读
            message.markRead();
            console.log(`已标记重复邮件为已读:${billedUser}`);
          } else {
            // 首次出现的用户,添加到记录中
            processedUsers.push(billedUser);
            userProperties.setProperty('processedBilledUsers', JSON.stringify(processedUsers));
            console.log(`已记录新用户:${billedUser}`);
          }
        }
      }
    });
  });
}

代码说明

  • 邮件筛选:修改searchQuery里的发件人、主题关键词,精准定位订阅订单邮件,避免误处理其他邮件。
  • 持久化存储:用PropertiesService保存已处理用户信息,脚本重启后记录不会丢失。
  • 内容提取:通过正则表达式匹配目标行,若邮件格式有细微差异,可调整正则表达式适配实际内容。
  • 处理逻辑:只处理未读邮件,首次出现的用户存入记录,重复用户直接标记邮件为已读。

使用提示

  1. 打开Google邮箱,进入设置→高级→启用「IMAP」,确保脚本能正常访问邮件。
  2. 打开Google Apps Script新建项目,粘贴代码并修改searchQuery参数。
  3. 首次运行需授权权限,测试正常后可设置时间驱动触发器(比如每日运行)实现自动过滤。

内容的提问来源于stack exchange,提问作者Sics Arts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 11:47:40