Getting ZgotmplZ when sending an email link rendered with html/template to a Slack channel

70 Views Asked by At

I' trying to generate a mailto link with an email address as link text <mailto:{{.user_email }}|{{.user_email}}> has confirmed his email, which I want to send to a slack channel.

But when I parse the template with the user_email, I am getting the output as

<mailto:ZgotmplZ|ZgotmplZ> has confirmed his email

I know

"ZgotmplZ" is a special value that indicates that unsafe content reached a CSS or URL context at runtime. The output of the example will be If the data comes from a trusted source, use content types to exempt it from filtering: URL(javascript:...).

I would like to get an output as

 <mailto:[email protected]|[email protected]> has confirmed his email

because this is the template style for sending email links in slack.

I've tried using a custom template function to escape the values, but the problem persists.

Here is the simplified version of my code

package main

import (
    "bytes"
    "fmt"
    "html/template"
)

func main() {

    emailTemplate := `<mailto:{{.user_email }}|{{.user_email}}> has confirmed his email`

    tmpl, err := template.
        New("emailTemplate").
        Parse(emailTemplate)

    if err != nil {
        panic(err)
    }

    data := map[string]interface{}{
        "user_email": "[email protected]",
    }
    var buf bytes.Buffer

    err = tmpl.Execute(&buf, data)
    if err != nil {
        panic(err)
    }

    fmt.Println(buf.String())
}

I would greatly appreciate any insights or suggestions on how to properly render the email address in the template without it being treated as unsafe or displaying ZgotmplZ instead

0

There are 0 best solutions below