gmail - 电子邮件模板中的转义字符

标签 gmail escaping email-templates

我有一个电子邮件模板,其中包含 var - ${token}作为临时密码。在这种情况下, token == @.iJ<ep2C]a-d$}4xK{ 。在 Gmail 中呈现时, token 在 < 字符处被截断并呈现为 @.iJ 。如何转义 token 中的字符以便它们在 Gmail 中正确呈现?

模板

From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="50393e363f103d29333f3d20313e297e333f3d" rel="noreferrer noopener nofollow">[email protected]</a>
Subject: New User Activation
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="alternative_boundary"

This is a message with multiple parts in MIME format.

--alternative_boundary
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

Dear ${firstName} ${lastName},

Please click on the below link and use Temporary password: ${token} to activate your new account.

${reset_url}


--alternative_boundary
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: 8bit


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>New account activation</title>

</head>
<body>
    <p>Dear ${firstName} ${lastName},</p>
    <p>Please click <a href="${reset_url}">here</a> to activate your new account and use verification code: ${token} in the page</p>

</body>
</html>

最佳答案

你会想逃跑the reserved characters根据 MDN 文档:

  • & &amp;
  • < &lt;
  • > &gt;
  • " &quot;

Gmail 认为 <是 HTML 标记的开始。

假设您使用 JavaScript,请将这些替换为 token 中的 Fastest method to escape HTML tags as HTML entities? :

function escapeHtmlEntities(str) {
  return str
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;');
}

在您的模板中:

...${escapeHtmlEntities(token)}...

关于gmail - 电子邮件模板中的转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50242833/

相关文章:

ruby-on-rails - (Rails Emails) 在 rails 中创建、查看、更新电子邮件模板

email-templates - 用于获取程序电子邮件的 Marketo REST API

javascript - 创建 Gmail 共享链接

django - 在 django 中发送邮件 - gmail smtp 问题

android - 像 gmail 新 (4.5) 应用程序一样拉动刷新

url - 如何对 url 的主机进行转义?

java - 如何防止 javax 变压器转义空白?

javascript - 转义字符串拆分和连接

javascript - 如何在node.js中使用多语言邮件模板?

node.js - 从 Gmail API 获取电子邮件的直接 URL(列出消息)