在Google App Engine Go中,可以使用CSS创建电子邮件模板吗?

huangapple go评论129阅读模式
英文:

Is it possible to create email templates with CSS in Google App Engine Go?

问题

我正在创建一个使用GAE Golang应用程序,用于通知用户系统中正在发生的事情。由于我希望邮件看起来漂亮,所以我已经在使用HTMLBody。然而,随着我创建越来越复杂的邮件,我想开始使用类似html/template的东西来创建带有CSS等漂亮外观的邮件。然而,我不确定如何使用Template.Execute将其转换为可以发送的HTMLBody字符串。

我该如何使用类似html/template的东西来创建HTML邮件,并与appengine/mail一起使用?

英文:

I am creating a GAE Golang application that will be notifying users of what is happening in the system. Since I want the emails to look nice, I am already using HTMLBody. However, as I'm creating more and more complex emails, I would like to start using something like html/template to crease nice looking emails with CSS and so forth. However, I'm not sure how I can do Template.Execute to turn it into HTMLBody string that can be sent.

How can I use something like html/template to create HTML emails to use with appengine/mail ?

答案1

得分: 1

你可以将模板渲染到一个临时的字节缓冲区中,像这样:

  1. var tmpl = template.Must(template.ParseFiles("templates/email.html"))
  2. buff := new(bytes.Buffer)
  3. if err = tmpl.Execute(buff, struct{ Name string }{"Juliet"}); err != nil {
  4. panic(err.Error())
  5. }
  6. msg := &mail.Message{
  7. Sender: "romeo@montague.com",
  8. To: []string{"Juliet <juliet@capulet.org>"},
  9. Subject: "See you tonight",
  10. Body: "...you put here the non-HTML part...",
  11. HTMLBody: buff.String(),
  12. }
  13. c := appengine.NewContext(r)
  14. if err := mail.Send(c, msg); err != nil {
  15. c.Errorf("Alas, my user, the email failed to sendeth: %v", err)
  16. }
英文:

You can render the template to a temporary byte buffer, like this:

  1. var tmpl = template.Must(template.ParseFiles(&quot;templates/email.html&quot;))
  2. buff := new(bytes.Buffer)
  3. if err = tmpl.Execute(buff, struct{ Name string }{&quot;Juliet&quot;}); err != nil {
  4. panic(err.Error())
  5. }
  6. msg := &amp;mail.Message{
  7. Sender: &quot;romeo@montague.com&quot;,
  8. To: []string{&quot;Juliet &lt;juliet@capulet.org&gt;&quot;},
  9. Subject: &quot;See you tonight&quot;,
  10. Body: &quot;...you put here the non-HTML part...&quot;,
  11. HTMLBody: buff.String(),
  12. }
  13. c := appengine.NewContext(r)
  14. if err := mail.Send(c, msg); err != nil {
  15. c.Errorf(&quot;Alas, my user, the email failed to sendeth: %v&quot;, err)
  16. }

huangapple
  • 本文由 发表于 2014年6月7日 10:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/24093200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定