2

I have an email template that contains a var - ${token} for a temporary password. In this case, token == @.iJ<ep2C]a-d$}4xK{. When rendered in Gmail token is truncated at the < character and rendered as @.iJ. How can I escape chars in token so they render correctly in Gmail?

Template

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>
neridaj
  • 2,143
  • 9
  • 31
  • 62
  • What language is that? It looks like JavaScript string interpolation in a template literal using `${value}`. – Sean May 09 '18 at 01:34

1 Answers1

2

You'll want to escape the reserved characters per the MDN docs:

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

Gmail thinks the < is the start of an HTML tag.

Assuming you're using JavaScript replace these in the token like 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;');
}

In your template:

...${escapeHtmlEntities(token)}...
Sean
  • 1,279
  • 9
  • 17