add mailer

This commit is contained in:
Geno 2021-06-21 16:30:34 +02:00
parent c66f03d829
commit ea83f78593
6 changed files with 98 additions and 2 deletions

3
go.mod
View File

@ -11,6 +11,7 @@ require (
github.com/gin-gonic/autotls v0.0.3
github.com/gin-gonic/gin v1.7.2
github.com/go-gormigrate/gormigrate/v2 v2.0.0
github.com/go-mail/mail v2.3.1+incompatible
github.com/google/uuid v1.2.0
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
@ -19,6 +20,8 @@ require (
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/mail.v2 v2.3.1 // indirect
gorm.io/driver/postgres v1.1.0
gorm.io/gorm v1.21.10
gorm.io/plugin/prometheus v0.0.0-20210507023802-dc84a49b85d1

6
go.sum
View File

@ -96,6 +96,8 @@ github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgO
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-mail/mail v2.3.1+incompatible h1:UzNOn0k5lpfVtO31cK3hn6I4VEVGhe3lX8AJBAxXExM=
github.com/go-mail/mail v2.3.1+incompatible/go.mod h1:VPWjmmNyRsWXQZHVHT3g0YbIINUkSmuKOiLIDkWbL6M=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
@ -604,6 +606,8 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
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 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
@ -615,6 +619,8 @@ gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/mail.v2 v2.3.1 h1:WYFn/oANrAGP2C0dcV6/pbkPzv8yGzqTjPmTeO7qoXk=
gopkg.in/mail.v2 v2.3.1/go.mod h1:htwXN1Qh09vZJ1NVKxQqHPBaCBbzKhp5GzuJEA4VJWw=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=

33
mailer/main.go Normal file
View File

@ -0,0 +1,33 @@
package mailer
import (
"github.com/go-mail/mail"
)
// Service to send mail
type Service struct {
SMTPHost string `toml:"smtp_host"`
SMTPPort int `toml:"smtp_port"`
SMTPUsername string `toml:"smtp_username"`
SMTPPassword string `toml:"smtp_password"`
SMTPSSL bool `toml:"smtp_ssl"`
Dailer *mail.Dialer `toml:"-"`
From string `toml:"from"`
}
// Ping mailer
func (m *Service) Ping() error {
conn, err := m.Dailer.Dial()
if err != nil {
return err
}
// do not run in timeout or reconnect errors
return conn.Close()
}
// Setup dailer (and ping)
func (m *Service) Setup() error {
m.Dailer = mail.NewDialer(m.SMTPHost, m.SMTPPort, m.SMTPUsername, m.SMTPPassword)
m.Dailer.SSL = m.SMTPSSL
return m.Ping()
}

1
mailer/main_test.go Normal file
View File

@ -0,0 +1 @@
package mailer

50
mailer/template.go Normal file
View File

@ -0,0 +1,50 @@
package mailer
import (
"bytes"
hTemplate "html/template"
"text/template"
)
// Renderer for easy template of TXT or HTML
type Renderer struct {
tmpl *template.Template
hTmpl *hTemplate.Template
}
// TemplateTXT - create template render
func TemplateTXT(temp string) *Renderer {
tmpl, err := template.New("").Parse(temp)
if err != nil {
return nil
}
return &Renderer{
tmpl: tmpl,
}
}
// TemplateHTML - create template render for html
func TemplateHTML(temp string) *Renderer {
tmpl, err := hTemplate.New("").Parse(temp)
if err != nil {
return nil
}
return &Renderer{
hTmpl: tmpl,
}
}
// Render template
func (r *Renderer) Render(data interface{}) string {
var buf bytes.Buffer
if r.hTmpl != nil {
if err := r.hTmpl.Execute(&buf, data); err != nil {
return ""
}
} else {
if err := r.tmpl.Execute(&buf, data); err != nil {
return ""
}
}
return string(buf.Bytes())
}

View File

@ -3,10 +3,12 @@ package web
import (
"github.com/bdlm/log"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
// acme
"github.com/gin-gonic/autotls"
"golang.org/x/crypto/acme/autocert"
// internal
"dev.sum7.eu/genofire/golang-lib/mailer"
"gorm.io/gorm"
)
// Service to store Configuration and Webserver wide objects
@ -26,7 +28,8 @@ type Service struct {
Secret string `toml:"secret"`
} `toml:"session"`
// internal
DB *gorm.DB `toml:"-"`
DB *gorm.DB `toml:"-"`
Mailer *mailer.Service `toml:"-"`
}
// Run to startup all related web parts