问题
I have a dynamic email template for sending a user a unique token link to reset their password. I assign the link to a field named 'link' in the dynamic_template_data section of my personalizations in my request object. When I receive the email, my anchor tag has no href attribute.
Here is my request object:
{
method: "POST",
url: "/v3/mail/send",
headers: {
"content-type": "application/json"
},
body: {
personalizations: [
{
to: [{ email: email, name: name }],
dynamic_template_data: {
link: link
}
}
],
// some other content
// ...
template_id: Constants.EMAIL.RESET_PASSWORD
},
json: true
}
And the html in my sendgrid dynamic template:
<div>Use the following link to reset your password: <a href="{{link}}">{{link}}</a></div>
When I receive the email, the text of the link is correctly filled in with my dynamic content but the element has no href attribute so it renders as plain text and isn't clickable:
What's particularly weird is that when I preview my dynamic template on SendGrid's design editor, the href field correctly populates with the test data I pass in.
回答1:
After some back-and-forth with SendGrid support, I managed to find the issue.
Because I've been developing this feature in my local environment, the link that I include in my request body points to my localhost. My link variable was being defined as follows:
link = "localhost:8080/#/resetPassword/" + token;
As a test, I changed the link to the following and it worked:
link = "https://google.com"
This made me suspicious that the lack of an http protocol was causing the issue. Changing it to the following fixed the issue:
link = "http://localhost:8080/#/resetPassword/" + token;
I'm assuming this is a quirk of the anchor tag element itself, though I don't quite understand the mechanism so if someone could elaborate on that for me I'd appreciate it.
来源:https://stackoverflow.com/questions/59633238/sendgrid-dynamic-template-stripping-href-from-anchor-tags