SparkPost / gosparkpost

SparkPost client library for the Go Programming Language
https://www.sparkpost.com/
Other
62 stars 40 forks source link

Cannot send HTML in SubstitutionData #133

Closed camuthig closed 6 years ago

camuthig commented 6 years ago

Hey, I'm trying to use the Send function on the client with a template and send HTML as part of one of my substitution values. My HTML template on SparkPost is just html, so whatever I send in that substitution value will be the entirety of my email message, and my Transmission is built something like

tx := &sp.Transmission{
    Recipients: []string{"test@example.com"},
    Content: map[string]string{
        "template_id": "my-template-id",
    },
    SubstitutionData: map[string]string{
        "html": "<strong>HTML</strong>",
    },
}

When this request is sent over the API, json.Marshal is escaping the HTML special characters, and they are not being unescaped on the SparkPost service. This means my email is literally showing me <strong>HTML</strong>, and the raw value of the MIME content is &lt;strong&gt;HTML&lt;&#x2F;strong&gt;.

I'm just learning go, so I'm not sure if there is a way for me to avoid this behavior in json.Marshal, but so far I have no been able to figure anything out.

yargevad commented 6 years ago

Are you using double {{ or triple {{{ curlies for the variable? Doubles escape html (it's a feature) while triples pass any html through.

Since you're passing in the entire body for each message anyway, I'd question the value of using a stored template. Have you considered using an inline template?

camuthig commented 6 years ago

Ah, you are correct in the number of curlies. I had two instead of three. I was porting code over from PHP to Go, and I believed I saw different behavior when sending the JSON with PHP, which doesn't alter the JSON when sending HTML like Go. What seems more likely is I just altered my template at some point without realizing it.

Sorry to waste your time on this one.

Your suggestion of using the inline template is also helpful. What I'm working on right now is really HTML partials instead of the entire email, but I attempted to simplify the testcase for brevity.