我正在尝试使用Golang发送超文本标记语言电子邮件,但我尝试使用Pongo2,而不是使用原生的Golang HTML /模板包。
在这个问题中:Is it possible to create email templates with CSS in Google App Engine Go?
用户提供了这个示例,它使用html/template
var tmpl = template.Must(template.ParseFiles("templates/email.html"))
buff := new(bytes.Buffer)
if err = tmpl.Execute(buff, struct{ Name string }{"Juliet"}); err != nil {
panic(err.Error())
}
msg := &mail.Message{
Sender: "romeo@montague.com",
To: []string{"Juliet <juliet@capulet.org>"},
Subject: "See you tonight",
Body: "...you put here the non-HTML part...",
HTMLBody: buff.String(),
}
c := appengine.NewContext(r)
if err := mail.Send(c, msg); err != nil {
c.Errorf("Alas, my user, the email failed to sendeth: %v", err)我想要做的是
var tmpl = pongo2.Must(pongo2.FromFile("template.html"))
buff := new(bytes.Buffer)
tmpl.Execute(buff, pongo2.Context{"data": "best-data"}, w)这里的问题是,pongo2.Execute()只允许输入上下文数据,而不允许输入缓冲区。
我的最终目标是能够使用Pongo2编写我的模板,并且能够以一种我也可以使用它来发送电子邮件的方式来呈现HTML。
我的问题是我做错了什么?我想要实现的目标有可能实现吗?如果我能找到一种将超文本标记语言呈现到缓冲区中方法,我可以稍后将其用作部分buff.String(),这将允许我将其输入到超文本标记语言主体中。
发布于 2017-01-20 07:32:04
使用ExecuteWriterUnbuffered而不是Execute
tmpl.ExecuteWriterUnbuffered(pongo2.Context{"data": "best-data"}, &buff)不确定w在您的示例中做了什么。如果你也想写另一个Writer,你可以使用io.MultiWriter。
// writes to w2 will go to both buff and w
w2 := io.MultiWriter(&buff, w)https://stackoverflow.com/questions/41751789
复制相似问题