master
Evan Chen 2021-12-17 12:54:03 +08:00
parent c574b8ae6e
commit 06889facfb
12 changed files with 129 additions and 32 deletions

View File

@ -7,3 +7,25 @@ combine all needed module into one lib
``` ```
``` ```
## Email
```go
m := gomail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
// Implements you email-sending function, for example by calling
// an API, or running postfix, etc.
fmt.Println("From:", from)
fmt.Println("To:", to)
return nil
})
if err := gomail.Send(s, m); err != nil {
panic(err)
}
```

View File

@ -1,7 +1,11 @@
package main package main
import ( import (
"time"
"github.com/rs/zerolog/log"
"kumoly.io/kumoly/app/auth" "kumoly.io/kumoly/app/auth"
"kumoly.io/kumoly/app/email"
"kumoly.io/kumoly/app/history" "kumoly.io/kumoly/app/history"
"kumoly.io/kumoly/app/server" "kumoly.io/kumoly/app/server"
"kumoly.io/kumoly/app/store" "kumoly.io/kumoly/app/store"
@ -18,7 +22,31 @@ func main() {
auth.SetDB(store.DB) auth.SetDB(store.DB)
sys.Inject(auth.Injector(server.API)) sys.Inject(auth.Injector(server.API))
sys.Append(server, auth.New(server), &task.Service{}, &history.Service{}) sys.Append(server, auth.New(server),
sys.Start() &task.Service{}, &history.Service{},
&email.Service{})
go sys.Start()
time.Sleep(time.Second)
err := email.Send("test@kumoly.io",
[]string{"evanchen333@gmail.com"}, "Test Mail",
&email.Body{
Greeting: "歡迎",
Signature: "Best Regards",
Content: `
# Testing
a|b|c
-|-|-
test||
|is|
| |up`,
},
)
if err != nil {
log.Error().Err(err).Msg("failed to send email")
}
<-sys.Done()
} }

View File

@ -1,4 +1,4 @@
package messenger package email
type App struct { type App struct {
Name string Name string
@ -7,12 +7,11 @@ type App struct {
Copyright string Copyright string
} }
type Base struct { type Body struct {
App App App App
Title string
Intros []string Intros []string
Outros []string Outros []string
Markdown string Content string
Greeting string Greeting string
Receiver string Receiver string

View File

@ -295,8 +295,8 @@
{{ end }} {{ end }}
{{/* Main Body */}} {{/* Main Body */}}
{{ if (ne .Markdown "") }} {{ if (ne .Content "") }}
{{ .Markdown | ToHTML }} {{ .Content | ToHTML }}
{{ else }} {{ else }}
<p>No Body</p> <p>No Body</p>
{{ end }} {{ end }}

36
email/email.go Normal file
View File

@ -0,0 +1,36 @@
package email
import (
"github.com/spf13/viper"
"gopkg.in/gomail.v2"
)
func init() {
// viper.SetDefault("email.type", "smtp")
viper.SetDefault("email.host", "mail.kumoly.io")
viper.SetDefault("email.port", 587)
viper.SetDefault("email.username", "test@kumoly.io")
viper.SetDefault("email.password", "test")
}
// host string, port int, username string, password string
var dial *gomail.Dialer
func Send(From string, To []string, Title string, b *Body) error {
m := gomail.NewMessage()
m.SetHeader("From", From)
m.SetHeader("To", To...)
m.SetHeader("Subject", Title)
// m.SetAddressHeader("Cc", "dan@example.com", "Dan")
// m.Attach("/home/Alex/lolcat.jpg")
body, err := Parse(b)
if err != nil {
return err
}
m.SetBody("text/html", body)
// Send the email
return dial.DialAndSend(m)
}

View File

@ -1,4 +1,4 @@
package messenger package email
import ( import (
"bytes" "bytes"
@ -16,22 +16,25 @@ var mailTmpl string
var engine *template.Template var engine *template.Template
var app App var app App
func init() { var mdParser = goldmark.New(
md := goldmark.New(
goldmark.WithExtensions(extension.GFM), goldmark.WithExtensions(extension.GFM),
) )
func init() {
engine = template.Must(template.New("email").Funcs(template.FuncMap{ engine = template.Must(template.New("email").Funcs(template.FuncMap{
"ToHTML": func(mdtext string) (string, error) { "ToHTML": ToHTML,
var buf bytes.Buffer
if err := md.Convert([]byte(mdtext), &buf); err != nil {
return "", err
}
return buf.String(), nil
},
}).Parse(mailTmpl)) }).Parse(mailTmpl))
} }
func Parse(b *Base) (string, error) { func ToHTML(mdtext string) (template.HTML, error) {
var buf bytes.Buffer
if err := mdParser.Convert([]byte(mdtext), &buf); err != nil {
return "", err
}
return template.HTML(buf.String()), nil
}
func Parse(b *Body) (string, error) {
b.App = app b.App = app
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := engine.Execute(buf, b) err := engine.Execute(buf, b)

View File

@ -1,23 +1,31 @@
package messenger package email
import ( import (
"fmt" "fmt"
"time" "time"
"github.com/spf13/viper" "github.com/spf13/viper"
"gopkg.in/gomail.v2"
"kumoly.io/kumoly/app/system" "kumoly.io/kumoly/app/system"
) )
type Messenger struct { type Service struct {
system.BaseService system.BaseService
} }
func (srv Messenger) GetName() string { return "messenger.Messenger" } func (srv Service) GetName() string { return "email.Service" }
func (srv *Messenger) Init() error { func (srv *Service) Init() error {
app.Name = viper.GetString("name") app.Name = viper.GetString("name")
app.Copyright = fmt.Sprintf("Copyright © %v %s. All rights reserved.", app.Copyright = fmt.Sprintf("Copyright © %v %s. All rights reserved.",
time.Now().Year(), viper.GetString("name")) time.Now().Year(), viper.GetString("name"))
app.Link = viper.GetString("server.url") app.Link = viper.GetString("server.url")
dial = gomail.NewDialer(
viper.GetString("email.host"),
viper.GetInt("email.port"),
viper.GetString("email.username"),
viper.GetString("email.password"))
return nil return nil
} }

2
go.mod
View File

@ -60,6 +60,8 @@ require (
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
golang.org/x/text v0.3.7 // indirect golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/ini.v1 v1.66.2 // indirect gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
) )

4
go.sum
View File

@ -289,10 +289,14 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

View File

@ -1,5 +0,0 @@
package messenger
func init() {
// viper.SetDefault()
}