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

Google Apps Script邮件关键词自动回复异常排查求助

问题排查与修正方案

Got it, let's figure out why your auto-reply always sends the "This is my test" message no matter what content you send—this is a super common (and easy to fix!) mistake.

核心问题:赋值运算符代替了比较运算符

Look at this line in your code:

if (msg = "test") { 

You're using a single equals sign = here, which is an assignment operator—it sets the value of msg to "test" instead of checking if msg equals "test". Since "test" is a truthy value, this condition will always evaluate to true, so your code will never hit the else block.

You need to use the strict equality operator === (or loose equality == if you don't care about type matching) to compare values:

if (msg === "test") { 

其他优化建议

Beyond fixing the comparison, there are a couple small tweaks to make your code more robust:

  1. Declare variables properly: Your msg variable isn't declared with var, let, or const, which can lead to unexpected scoping issues. Add let before it:

    let msg = messages[ii].getPlainBody(); 
    
  2. Avoid index issues when fetching threads: Your current loop uses labelObj.getUnreadCount() and fetches threads via labelObj.getThreads()[gg] each time. If threads are moved to trash during the loop, the array returned by getThreads() will change, which can lead to skipping or reprocessing threads. Instead, fetch all unread threads first and loop over that array:

    function autoReplier() {
      var labelObj = GmailApp.getUserLabelByName('autoreply'); 
      if (!labelObj) { // Add a check in case the label doesn't exist
        console.log('Label "autoreply" not found!');
        return;
      }
      var gmailThreads = labelObj.getThreads(); // Fetch all threads first
      var sender;
      for (var gg = 0; gg < gmailThreads.length; gg++) { 
        var messages = gmailThreads[gg].getMessages(); 
        for (var ii = 0; ii < messages.length; ii++) {
          if (messages[ii].isUnread()){
            let msg = messages[ii].getPlainBody(); 
            if (msg === "test") { 
              sender = messages[ii].getFrom();
              MailApp.sendEmail(sender, "Auto Reply", "This is my test"); 
              messages[ii].markRead(); 
              messages[ii].moveToTrash(); 
            } else { 
              sender = messages[ii].getFrom();
              MailApp.sendEmail(sender, "Auto Reply", "Sorry, your keyword was not recognized"); 
              messages[ii].markRead(); 
              messages[ii].moveToTrash(); 
            } 
          } 
        } 
      } 
    }
    
  3. Prepare for multi-keyword expansion: Since you plan to add more keywords later, you can use an array to store keywords and check if the message body includes any of them. For example:

    const keywords = ['test', 'hello', 'help'];
    if (keywords.includes(msg.trim())) { // Trim to ignore extra whitespace
      // Send matching reply tailored to the keyword
    } else {
      // Send unrecognized reply
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:41:21