hermes golang email 模版包
hermes 提供了灵活的基于html 的email发送模版支持,使用简单,而且也包含了灵活的主题支持
参考使用
- 项目准备
main.go
package main
import (
"io/ioutil"
"log"
"github.com/matcornic/hermes/v2"
)
func main() {
h := hermes.Hermes{
Product: hermes.Product{
Name: "Hermes",
Link: "https://example-hermes.com/",
Logo: "https://github.com/matcornic/hermes/blob/master/examples/gopher.png?raw=true",
},
}
email := hermes.Email{
Body: hermes.Body{
Name: "Jon Snow",
Intros: []string{
"Welcome to Hermes! We're very excited to have you on board.",
},
Actions: []hermes.Action{
{
Instructions: "To get started with Hermes, please click here:",
Button: hermes.Button{
Color: "#22BC66", // Optional action button color
Text: "账户确认",
Link: "https://hermes-example.com/confirm?token=d9729feb74992cc3482b350163a1a010",
},
},
},
Outros: []string{
"Need help, or have questions? Just reply to this email, we'd love to help.",
},
},
}
// Generate an HTML email with the provided contents (for modern clients)
emailBody, err := h.GenerateHTML(email)
if err != nil {
panic(err) // Tip: Handle error with something else than a panic ;)
}
// Generate the plaintext version of the e-mail (for clients that do not support xHTML)
emailText, err := h.GeneratePlainText(email)
if err != nil {
panic(err) // Tip: Handle error with something else than a panic ;)
}
log.Println(emailText)
// Optionally, preview the generated HTML e-mail by writing it to a local file
err = ioutil.WriteFile("preview.html", []byte(emailBody), 0644)
if err != nil {
panic(err) // Tip: Handle error with something else than a panic ;)
}
}
- 效果
说明
从效果来看,还是一个很不错的选择,至少简单,而且比较专业