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

使用Google Apps Script发送SMS时丢失字母‘s’的问题排查

问题:通过Gmail发送SMS时所有字母"s"丢失的原因及修复方案

问题背景

使用Google Apps Script通过Gmail向Google Sheets中A1单元格的手机号发送SMS,代码能成功发送短信,但所有字母"s"丢失,例如原消息"This is the message"变为"Thi i the meage"。原代码如下:

function sendTextMessagesWithGatewayTest() {
  // Get the active spreadsheet
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  
  // Get the range of phone numbers
  var range = sheet.getRange("A1"); // Adjust the range as per your data
  
  // Get the values from the range
  var phoneNumbers = range.getValues();
  
  // Loop through each phone number
  for (var i = 0; i < phoneNumbers.length; i++) {
    var phoneNumber = parsePhoneNumber(phoneNumbers[i][0]); // Parse phone number
    
    // Test each gateway domain before sending the text message
    var result = testGatewayDomains(phoneNumber);
    
    // If at least one gateway domain test was successful, send the text message
    if (result.isValid) {
      sendTextMessageGateway(phoneNumber, "This is the message", result.domain); // Pass the domain to the sendTextMessageGateway function
    } else {
      Logger.log('Unable to send text message to phone number: ' + phoneNumber + '. No valid gateway domains found.');
    }
  }
}

function testGatewayDomains(phoneNumber) {
  var gatewayDomains = {
    'Bell Mobility': 'txt.bell.ca',
    'Rogers Wireless': 'pcs.rogers.com',
    'Telus Mobility': 'msg.telus.com',
    'Fido': 'fido.ca',
    'Koodo Mobile': 'msg.koodomobile.com',
    'Virgin Mobile Canada': 'vmobile.ca'
    // Add more carriers and their respective gateway domains as needed
  };
  
  // Loop through each carrier and its corresponding gateway domain
  for (var carrier in gatewayDomains) {
    if (gatewayDomains.hasOwnProperty(carrier)) {
      var domain = gatewayDomains[carrier];
      
      // Construct URL to check if domain is reachable
      var url = 'http://' + domain;
      
      try {
        // Send HTTP GET request to the URL
        var response = UrlFetchApp.fetch(url);
        
        // Check response code
        if (response.getResponseCode() === 200) {
          // Log success if request was successful
          Logger.log('Domain is reachable: ' + domain);
          return { isValid: true, domain: domain }; // Exit function early if at least one domain is reachable
        } else {
          // Log error if request was not successful
          Logger.log('Domain is not reachable: ' + domain + ' (Response code: ' + response.getResponseCode() + ')');
        }
      } catch (error) {
        // Log error if request failed
        Logger.log('Error checking domain: ' + domain + ' - ' + error);
      }
    }
  }
  return { isValid: false, domain: null }; // Return false if no reachable domains found
}

function sendTextMessageGateway(phoneNumber, message, workingDomain) {
  var recipient = phoneNumber + "@" + workingDomain; 
  var subject = "Notification"; // Provide a subject for the email
  Logger.log('Recipient: ' + recipient); 
  
  // Send the email
  GmailApp.sendEmail(recipient, subject, message);
}

function parsePhoneNumber(phoneNumber) {
  // Remove non-numeric characters
  var parsedNumber = phoneNumber.replace(/\D/g, '');
  
  return parsedNumber;
}

原因分析

核心问题出在运营商SMS网关的邮件解析逻辑:

  1. 加拿大多数运营商的SMS网关对邮件主题的处理存在兼容性问题,当邮件包含非空主题(如代码中的"Notification")时,网关会将主题与正文合并解析,触发字符过滤规则,导致所有"s"被误删。
  2. GmailApp.sendEmail默认发送多格式邮件(HTML+纯文本),部分网关优先解析HTML版本,可能因格式兼容问题进一步引发字符丢失。

修复方案

只需修改sendTextMessageGateway函数,做两处调整:

  • 将邮件主题设为空字符串(SMS无需主题,网关会直接使用正文内容)
  • 明确指定发送纯文本格式邮件,避免网关解析HTML内容时出现异常

修改后的代码如下:

function sendTextMessageGateway(phoneNumber, message, workingDomain) {
  var recipient = phoneNumber + "@" + workingDomain; 
  var subject = ""; // 修改:设为空字符串,避免触发网关的字符过滤
  Logger.log('Recipient: ' + recipient); 
  
  // 修改:指定纯文本格式发送,确保网关正确解析内容
  GmailApp.sendEmail(recipient, subject, message, {
    textBody: message,
    htmlBody: null // 禁用HTML格式,强制发送纯文本
  });
}

其他函数无需修改,重新运行代码即可正常发送包含"s"字符的短信。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 23:47:35