【问题标题】:Is it possible to create email templates with CSS in Google App Engine Go?是否可以在 Google App Engine Go 中使用 CSS 创建电子邮件模板?
【发布时间】:2014-07-28 09:28:48
【问题描述】:
我正在创建一个 GAE Golang 应用程序,它将通知用户系统中发生的事情。因为我希望电子邮件看起来不错,所以我已经在使用 HTMLBody。然而,随着我创建越来越复杂的电子邮件,我想开始使用 html/template 之类的东西来创建带有 CSS 等的漂亮电子邮件。但是,我不确定如何执行 Template.Execute 将其转换为可以发送的 HTMLBody 字符串。
如何使用 html/template 之类的东西来创建 HTML 电子邮件以与 appengine/mail 一起使用?
【问题讨论】:
标签:
html
css
google-app-engine
email
go
【解决方案1】:
您可以将模板渲染到临时字节缓冲区,如下所示:
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)
}