2

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:john.doe@gmail.com|john.doe@gmail.com> 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": "john.doe@gmail.com",
    }
    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

PRATHEESH PC
  • 1,461
  • 1
  • 3
  • 15
  • @mkopriva It's here https://pkg.go.dev/html/template and also at https://stackoverflow.com/questions/36382624/how-to-get-rid-of-zgotmplz-from-html-template-in-golang – PRATHEESH PC Jun 22 '23 at 08:55
  • It's not a solution, but I noticed that removing the leading `<` character from the template string causes the data input to work properly. [Here's that code running in Playground.](https://go.dev/play/p/SuBOZna7vuQ). – phonaputer Jun 22 '23 at 22:26

0 Answers0