Files
go-common/examples/email_example.go

43 lines
838 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//go:build example
// +build example
package main
import (
"fmt"
"log"
"git.toowon.com/jimmy/go-common/config"
"git.toowon.com/jimmy/go-common/factory"
)
func main() {
cfg, err := config.LoadFromFile("./config/example.json")
if err != nil {
log.Fatal(err)
}
app := factory.New(cfg)
mail, err := app.Email()
if err != nil {
log.Fatal(err)
}
defer mail.Close()
// 同步发送(验证码等需等待结果)
err = mail.SendEmail(
[]string{"recipient@example.com"},
"测试邮件",
"这是一封测试邮件。",
)
if err != nil {
log.Printf("sync send failed: %v", err)
} else {
fmt.Println("sync email sent")
}
// 异步发送HTTP 通知类)
mail.SendEmailAsync(nil, []string{"recipient@example.com"}, "异步通知", "后台发送,不阻塞请求")
fmt.Println("async email enqueued")
}