diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..08695a6 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,26 @@ +package config + +import ( + "sendmsg/global" + "sendmsg/model" + "testing" +) + +func TestConfig(t *testing.T) { + global.GLO_CONF_CH = make(chan model.Config) + config, err := InitConfig() + if err != nil { + t.Fatalf("Failed to initialize config: %v", err) + } + t.Log(config) + + var count int + for { + if count > 5 { + return + } + config = <-global.GLO_CONF_CH + count++ + t.Log(config) + } +} diff --git a/go.mod b/go.mod index 34f7088..a096e79 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,13 @@ module sendmsg go 1.19 -require github.com/spf13/viper v1.15.0 +require ( + github.com/fsnotify/fsnotify v1.6.0 + github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible + github.com/spf13/viper v1.15.0 +) require ( - github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect diff --git a/go.sum b/go.sum index edf286c..261cb2a 100644 --- a/go.sum +++ b/go.sum @@ -122,6 +122,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA= +github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= diff --git a/message/mail.go b/message/mail.go index b8fb489..518d152 100644 --- a/message/mail.go +++ b/message/mail.go @@ -1,6 +1,11 @@ package message -import "log" +import ( + "fmt" + "github.com/jordan-wright/email" + "log" + "net/smtp" +) type Mail struct { Host string @@ -14,5 +19,15 @@ type Mail struct { func (m *Mail) Send(body Body) { log.Printf("[mail] sending message...") - log.Printf(body.Title + " " + body.Content) + e := email.NewEmail() + e.From = m.FromName + e.To = m.To + e.Subject = body.Title + e.Text = []byte(body.Content) + addr := fmt.Sprintf("%v:%v", m.Host, m.Port) + err := e.Send(addr, smtp.PlainAuth("", m.Username, m.Password, m.Host)) + if err != nil { + log.Fatalf("[mail] send failed: %v\n", err) + } + log.Printf("[mail] send successful") } diff --git a/message/message_test.go b/message/message_test.go index 717fff7..b85fc8e 100644 --- a/message/message_test.go +++ b/message/message_test.go @@ -15,8 +15,8 @@ func TestSend(t *testing.T) { global.GLO_CONF = conf m := GetType() - if !Enabled() { - return + if !Enabled() || m == nil { + t.Fatalf("[test] Failed to get type...") } m.Send(Body{ Title: "Hello",