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

使用Google Apps Script发送邮件时出现异常换行问题

解决Google Apps Script邮件正文换行异常问题

嘿,我看到你在使用Google Apps Script发送邮件时遇到了正文换行位置异常的问题,咱们来快速搞定它~

问题根源

你当前用\n/\n\n来实现换行,但Google Apps Script的MailApp.sendEmail()默认发送的是HTML格式邮件。在HTML语法里,普通的换行符\n不会被解析成换行,只会被当作空格处理,这就是导致换行位置不对的核心原因。

两种解决方案

方案1:改用HTML换行标签(推荐)

如果希望邮件保留HTML格式(方便后续添加样式),把\n替换成HTML的换行标签<br>,空两行就用<br><br>。修改后的代码示例:

function sendEmail(){ 
  var name = "MyName";
  var body = name + " has issued a challenge. You already have a match currently scheduled so you have the option to decline. Reply to this email with the word 'ACCEPT' or 'DECLINE' in the subject." + "<br><br>NOTE: If you do not respond to this email you will automatically accept the challenge and be responsible for scheduling the match...";
  
  // 发送邮件时指定htmlBody参数
  MailApp.sendEmail({
    to: "recipient@example.com", // 替换成实际收件人邮箱
    subject: "Challenge Notification",
    htmlBody: body
  });
}

方案2:强制发送纯文本邮件

如果不需要HTML格式,直接设置邮件的MIME类型为纯文本,这样\n就能正常生效。代码示例:

function sendEmail(){ 
  var name = "MyName";
  var body = name + " has issued a challenge. You already have a match currently scheduled so you have the option to decline. Reply to this email with the word 'ACCEPT' or 'DECLINE' in the subject." + "\n\nNOTE: If you do not respond to this email you will automatically accept the challenge and be responsible for scheduling the match...";
  
  MailApp.sendEmail({
    to: "recipient@example.com", // 替换成实际收件人邮箱
    subject: "Challenge Notification",
    body: body,
    mimeType: MimeType.PLAIN_TEXT
  });
}

小提醒

  • 记得给变量声明加上分号(虽然GAS允许省略,但规范写法能避免潜在问题)
  • 你的代码里body内容好像没写完,记得补全完整的文本哦~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:50:51