解析模板以发送电子邮件时,Html未进行解析
我正在从事一个基于SAAS的项目,我需要就不同的事件向不同的客户发送电子邮件 我使用的电子邮件模板使用的令牌(格式为{.TOKENNAME}})在发送电子邮件时是动态的。现在,使用“html/template”包解析这些令牌 下面是我用来将这些令牌解析为电子邮件正文的自定义函数解析模板以发送电子邮件时,Html未进行解析,html,email,go,Html,Email,Go,我正在从事一个基于SAAS的项目,我需要就不同的事件向不同的客户发送电子邮件 我使用的电子邮件模板使用的令牌(格式为{.TOKENNAME}})在发送电子邮件时是动态的。现在,使用“html/template”包解析这些令牌 下面是我用来将这些令牌解析为电子邮件正文的自定义函数 type EmailTemplate struct{ BookingDetails string } type EmailRequest struct{ EmailTo
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
}
键入EmailTemplate结构{
BookingDetails字符串
}
键入EmailRequest结构{
EmailTo字符串
电子邮件正文字符串
}
//从数据库中获取带有标记的已保存html
notificationTemplate,errVal:=merchantDb.GetNotificationTemplate()
请求:=电子邮件请求{
"test@example.com",
notificationTemplate.Content,
}
templateData.BookingDetails+=“Industry”+Industry.IndustryName+”“
request.EmailSend(templateData)
func(request*EmailRequest)EmailSend(notificationTemplateData接口{})(bool,错误){
正文,errParse:=ParseTemplate(request.EmailBody,notificationTemplateData)
//电子邮件发送代码在这里
}
func ParseTemplate(templateHtml字符串,数据接口{})(字符串,错误){
变量体字符串
t、 err:=template.New(“我的模板”).Parse(templateHtml)
如果错误!=零{
返回体,错误
}
buf:=新(字节.缓冲区)
如果err=t.Execute(buf,data);err!=nil{
返回体,错误
}
body=buf.String()
返回体,无
}
其中templateHtml是包含令牌的电子邮件正文,data是包含这些令牌的动态值的接口。当我使用ParseTemplate函数将令牌解析为字符串值时,它工作得很好。但如果我必须在我的一个令牌中解析html,那么它会将html解析为字符串,并在电子邮件中将html显示为字符串
有人能告诉我在ParseTemplate函数中解析html应该怎么做吗?您可以将包含html的字符串转换为template.html类型:
str := "<br>" // gets converted and renders as "<br>"
strSafe := template.HTML(str) // renders as "<br>"
str:=“
”//转换并呈现为“br”
strSafe:=template.HTML(str)//呈现为“
”
这标志着你的绳子是安全的。执行此操作时,请确保包含的html是安全的,尤其是当它来自第三方时。您只需将
键入EmailTemplate struct
定义为
type EmailTemplate struct{
BookingDetails template.HTML
}
请解释“它将html解析为字符串”的含义。请举例说明您的输入和输出,以及您期望的输出。@Flimzy,这是我的输入:“bookingDetails+=”Industry“+Industry.IndustryName+””,输出是:“pspanIndustry/span家庭清洁/span/p”,这应该是您的问题。好的。但是你有什么解决办法吗?当我把它转换成template.HTML时,它给了我一个错误:无法将数据(类型接口{})转换为类型“HTML/template”。HTML:需要类型断言。我已将我的html片段作为函数中的类型接口。请向我们展示您的代码,您可以在其中组装
数据
。您可以调用body,errParse:=ParseTemplate(request.EmailBody,template.html(notificationTemplateData))
。在这种情况下,您必须绝对确定主体中没有恶意代码。至少应该可以编译。使用上面的语句仍然会得到相同的错误:无法将notificationTemplateData(类型接口{})转换为类型“html/template”。html:需要类型断言。您可以将EmailSend的签名更改为EmailSend(notificationTemplateData string)
。但这有点不对劲了。可以将结构作为数据传递给Execute func,并使用管道渲染字段。但那完全是另一回事。预订细节会被赋予什么样的价值??