2

I am using mailgun Templates with Go to send emails. However when the text in my template fields contain \n no newline is added. My basic code looks like the following:

message := m.mg.NewMessage(sender, subject, body, recipient)
message.SetTemplate("my-template")
_ = message.AddTemplateVariable("myText", event.Text)
// ... Code for sending

I saw this answer about sending mail with newlines, however when I implement it like the following:

_ = message.AddTemplateVariable("myText", strings.ReplaceAll(event.Text, "\\n", "<br />"))

The email contains <br /> text in the body but no newlines.

I also tried adding the following field in the css of my template: white-space: pre-wrap; but still there was no break. Thanks for any help with adding a newline to a Mailgun Template with Golang.

Gabe
  • 5,643
  • 3
  • 26
  • 54

1 Answers1

3

Try to use triple-stash in your template instead of double-stash

{{var1}} -> {{{var1}}}
Phan Dinh
  • 245
  • 2
  • 11
  • This is a security concern if the content to display is something the user typed via an HTML form, isn't it ? – Flobesst Jan 05 '22 at 14:15