I am working on a SAAS based project for which I need to send emails to different clients on different events.
I am using email templates which use tokens (in format {{.TOKENNAME}}) that are made dynamic while sending emails. Now these token are parsed by using "html/template" package.
following is the custom function that I have made to parse these tokens into email body.
type EmailTemplate struct{
BookingDetails string
}
type EmailRequest struct{
EmailTo string
EmailBody string
}
// get saved html with tokens from database
notificationTemplate, errVal := merchantDb.GetNotificationTemplate()
request := EmailRequest{
"test@example.com",
notificationTemplate.Content,
}
templateData.BookingDetails += "<p><span>Industry</span><span>"+industry.IndustryName+"</span></p>"
request.EmailSend(templateData)
func (request *EmailRequest) EmailSend(notificationTemplateData interface{}) (bool, error) {
body, errParse := ParseTemplate(request.EmailBody, notificationTemplateData)
//email sending code here
}
func ParseTemplate(templateHtml string, data interface{}) (string, error) {
var body string
t, err := template.New("my_template").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)
if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}
Where templateHtml is the email body with tokens and data is the interface holding dynamic values for these tokens. When I use ParseTemplate function to parse tokens as string values then it works fine. But if I have to parse html in one of my tokens then it parses html as string and in email displays html as string.
Can anybody tell me what should I do to parse html in ParseTemplate function?