逃避电子邮件模板中的字符

时间:2018-05-08 21:58:58

标签: gmail escaping email-templates

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

模板

From: info@mycompany.com
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>

1 个答案:

答案 0 :(得分:0)

您希望按照MDN文档逃避the reserved characters

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

Gmail认为<是HTML代码的开头。

假设您正在使用JavaScript,请将其替换为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)}...