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

如何让Google表单提交时,Gmail邮件以独立线程而非会话发送?

解决Gmail合并邮件会话的方法

Gmail会自动合并主题相同且存在线程关联的邮件为会话。要让每封表单提交触发的邮件以独立线程呈现,最直接有效的方式是确保每封邮件的主题独一无二,同时可配合邮件头设置强化效果。以下是具体修改方案:

方案1:给主题添加唯一标识

通过在主题中加入全局唯一值(如表单响应ID、毫秒级时间戳),让每封邮件的主题不重复,从根源避免会话合并。

针对表单提交场景(推荐)

如果脚本是由Google表单提交触发,可直接使用表单响应的唯一ID作为标识:

// 表单提交触发的函数
function onFormSubmit(e) {
  // 此处根据表单字段生成tInfo,示例省略具体逻辑
  var tInfo = "提交人姓名:张三\n联系方式:13xxxxxxxxx\n请求内容:xxx";
  // 传入表单响应的唯一ID
  sendEmailToTech(tInfo, e.response.getId());
}

function sendEmailToTech(tInfo, uniqueId) {
  var tEmail = "abc1234@gmail.com";
  // 主题加入唯一响应ID
  var subject = `Building Request From Approved - ${uniqueId}`;
  var body = "<b>Requester Information:</b>\n\n" + tInfo.replace(/\n/g, '<br>');
  var recipientList = tEmail;

  MailApp.sendEmail({
    to: recipientList,
    subject: subject,
    htmlBody: body,
    // 可选:清空线程关联头,进一步避免合并
    headers: {
      'In-Reply-To': '',
      'References': ''
    }
  });
}

无表单ID的通用场景

如果无法获取表单响应ID,可使用毫秒级时间戳作为唯一标识:

function sendEmailToTech(tInfo) {
  var tEmail = "abc1234@gmail.com";
  // 生成毫秒级时间戳(全局唯一)
  var uniqueId = new Date().getTime();
  var subject = `Building Request From Approved - ${uniqueId}`;
  var body = "<b>Requester Information:</b>\n\n" + tInfo.replace(/\n/g, '<br>');
  var recipientList = tEmail;

  MailApp.sendEmail({
    to: recipientList,
    subject: subject,
    htmlBody: body
  });
}

解决重复发送问题

你之前遇到的重复发送,大概率是脚本被重复触发(如表单提交重试、脚本并发执行)。可通过以下方式避免:

function sendEmailToTech(tInfo, uniqueId) {
  // 加锁防止并发执行
  var lock = LockService.getScriptLock();
  try {
    // 等待30秒获取锁,超时则取消发送
    if (!lock.tryLock(30000)) {
      Logger.log("获取锁失败,取消邮件发送");
      return;
    }
    // 检查该请求是否已发送过邮件
    var properties = PropertiesService.getScriptProperties();
    if (properties.getProperty(`sent_${uniqueId}`)) {
      Logger.log("该请求的邮件已发送,无需重复操作");
      return;
    }

    // 邮件发送逻辑
    var tEmail = "abc1234@gmail.com";
    var subject = `Building Request From Approved - ${uniqueId}`;
    var body = "<b>Requester Information:</b>\n\n" + tInfo.replace(/\n/g, '<br>');
    var recipientList = tEmail;

    MailApp.sendEmail({
      to: recipientList,
      subject: subject,
      htmlBody: body
    });

    // 标记该请求已发送
    properties.setProperty(`sent_${uniqueId}`, "true");
  } finally {
    // 释放锁
    lock.releaseLock();
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 07:12:43