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

如何通过Google Script实现Gmail邮件延迟发送?现有脚本失效求助

解决Gmail插件延迟非工作时间邮件发送失效问题

问题概述

试图通过Google Script开发Gmail插件,实现非工作时间发送的邮件自动延迟至下一个工作日发送,但插件已安装并显示在“已安装的开发者插件”中,邮件仍会立即发送,插件逻辑未生效。

提供的初始配置和脚本存在占位符未实现的核心问题,导致逻辑无法触发。

失效核心原因

  1. 脚本中condition和nextWorkingDayDate为未定义的占位符,无实际判断逻辑
  2. GmailApp.sendEmail的schedule.sendDate需要传入符合要求的Date对象,且需匹配时区配置
  3. 未正确实现“非工作时间”的判断逻辑

修复后的完整代码

1. appsscript.json配置(保留原有效配置,无需修改)

{
  "timeZone": "Europe/Prague",
  "dependencies": {
    "enabledAdvancedServices": []
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://mail.google.com/",
    "https://www.googleapis.com/auth/userinfo.email"
  ],
  "runtimeVersion": "V8",
  "gmail": {
    "name": "Conditional sending",
    "logoUrl": "https://www.gstatic.com/images/branding/googleg/1x/googleg_standard_color_64dp.png",
    "contextualTriggers": [
      {
        "unconditional": {},
        "onTriggerFunction": "onGmailSend"
      }
    ],
    "primaryColor": "#146b2e",
    "openLinkUrlPrefixes": [
      "https://developers.google.com/gmail"
    ],
    "version": "TRUSTED_TESTER_V2"
  }
}

2. 核心脚本实现

替换原占位符脚本,实现完整的工作时间判断和延迟发送逻辑:

function onGmailSend(e) {
  // 配置工作时间参数(可根据需求调整)
  const WORK_START_HOUR = 9;
  const WORK_END_HOUR = 17;
  const TIME_ZONE = "Europe/Prague";

  // 获取当前布拉格时区的时间
  const now = new Date();
  const currentTimeInPrague = Utilities.formatDate(now, TIME_ZONE, "yyyy-MM-dd HH:mm:ss");
  const dateObj = new Date(currentTimeInPrague);
  const dayOfWeek = dateObj.getDay(); // 0=周日, 1=周一, ..., 6=周六
  const currentHour = dateObj.getHours();

  // 判断是否为非工作时间:周末 或 工作日非9-17点
  const isNonWorkingTime = dayOfWeek === 0 || dayOfWeek === 6 || currentHour < WORK_START_HOUR || currentHour >= WORK_END_HOUR;

  if (isNonWorkingTime) {
    // 计算下一个工作日的9点
    let nextWorkingDay = new Date(dateObj);
    if (dayOfWeek === 6) { // 周六,跳转到下周一
      nextWorkingDay.setDate(nextWorkingDay.getDate() + 2);
    } else if (dayOfWeek === 0) { // 周日,跳转到下周一
      nextWorkingDay.setDate(nextWorkingDay.getDate() + 1);
    } else { // 工作日非工作时间,跳转到次日9点
      nextWorkingDay.setDate(nextWorkingDay.getDate() + 1);
    }
    // 设置时间为工作开始时间
    nextWorkingDay.setHours(WORK_START_HOUR, 0, 0, 0);

    // 延迟发送邮件
    GmailApp.sendEmail(
      e.gmail.message.to,
      e.gmail.message.subject,
      e.gmail.message.body,
      {
        replyTo: e.gmail.message.from,
        bcc: e.gmail.bcc,
        cc: e.gmail.cc,
        name: e.gmail.name,
        htmlBody: e.gmail.htmlBody,
        attachments: e.gmail.attachments,
        schedule: { sendDate: nextWorkingDay }
      }
    );

    // 阻止默认发送行为
    return false;
  }

  // 工作时间内,允许立即发送
  return true;
}

验证步骤

  1. 重新部署插件:在Google Apps Script编辑器中,点击「部署」→「部署为插件」,确保版本更新并重新授权
  2. 测试非工作时间发送:在周末或工作日的9点前/17点后尝试发送邮件
  3. 检查Gmail的「已计划发送」文件夹,确认邮件已被安排到下一个工作日的9点发送

内容的提问来源于stack exchange,提问作者Michal Palus

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 03:40:56