我有一个电子邮件模板,其中包含 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 文档:
-
&
&
-
<
<
-
>
>
-
"
"
Gmail 认为 <
是 HTML 标记的开始。
假设您使用 JavaScript,请将这些替换为 token 中的 Fastest method to escape HTML tags as HTML entities? :
function escapeHtmlEntities(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
在您的模板中:
...${escapeHtmlEntities(token)}...
关于gmail - 电子邮件模板中的转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50242833/