Initial commit
This commit is contained in:
19
internal/mail/mailer.go
Normal file
19
internal/mail/mailer.go
Normal file
@ -0,0 +1,19 @@
|
||||
package mail
|
||||
|
||||
type Option struct {
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
Password string
|
||||
InsecureSkipVerify bool
|
||||
}
|
||||
|
||||
type OptionFunc func(*Option)
|
||||
|
||||
type Mailer struct {
|
||||
opt *Option
|
||||
}
|
||||
|
||||
func NewMailer(funcs ...OptionFunc) *Mailer {
|
||||
return &Mailer{}
|
||||
}
|
11
internal/mail/provider.go
Normal file
11
internal/mail/provider.go
Normal file
@ -0,0 +1,11 @@
|
||||
package mail
|
||||
|
||||
import "gitlab.com/wpetit/goweb/service"
|
||||
|
||||
func ServiceProvider(opts ...OptionFunc) service.Provider {
|
||||
mailer := NewMailer(opts...)
|
||||
|
||||
return func(ctn *service.Container) (interface{}, error) {
|
||||
return mailer, nil
|
||||
}
|
||||
}
|
138
internal/mail/send.go
Normal file
138
internal/mail/send.go
Normal file
@ -0,0 +1,138 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
gomail "gopkg.in/mail.v2"
|
||||
)
|
||||
|
||||
type SendFunc func(*SendOption)
|
||||
|
||||
type SendOption struct {
|
||||
Charset string
|
||||
AddressHeaders []AddressHeader
|
||||
Headers []Header
|
||||
Body Body
|
||||
AlternativeBodies []Body
|
||||
}
|
||||
|
||||
type AddressHeader struct {
|
||||
Field string
|
||||
Address string
|
||||
Name string
|
||||
}
|
||||
|
||||
type Header struct {
|
||||
Field string
|
||||
Values []string
|
||||
}
|
||||
|
||||
type Body struct {
|
||||
Type string
|
||||
Content string
|
||||
PartSetting gomail.PartSetting
|
||||
}
|
||||
|
||||
func WithCharset(charset string) func(*SendOption) {
|
||||
return func(opt *SendOption) {
|
||||
opt.Charset = charset
|
||||
}
|
||||
}
|
||||
|
||||
func WithFrom(address string, name string) func(*SendOption) {
|
||||
return WithAddressHeader("From", address, name)
|
||||
}
|
||||
|
||||
func WithAddressHeader(field, address, name string) func(*SendOption) {
|
||||
return func(opt *SendOption) {
|
||||
opt.AddressHeaders = append(opt.AddressHeaders, AddressHeader{field, address, name})
|
||||
}
|
||||
}
|
||||
|
||||
func WithHeader(field string, values ...string) func(*SendOption) {
|
||||
return func(opt *SendOption) {
|
||||
opt.Headers = append(opt.Headers, Header{field, values})
|
||||
}
|
||||
}
|
||||
|
||||
func WithBody(contentType string, content string, setting gomail.PartSetting) func(*SendOption) {
|
||||
return func(opt *SendOption) {
|
||||
opt.Body = Body{contentType, content, setting}
|
||||
}
|
||||
}
|
||||
|
||||
func WithAlternativeBody(contentType string, content string, setting gomail.PartSetting) func(*SendOption) {
|
||||
return func(opt *SendOption) {
|
||||
opt.AlternativeBodies = append(opt.AlternativeBodies, Body{contentType, content, setting})
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Mailer) Send(funcs ...SendFunc) error {
|
||||
opt := &SendOption{
|
||||
Charset: "UTF-8",
|
||||
Body: Body{
|
||||
Type: "text/plain",
|
||||
Content: "",
|
||||
PartSetting: gomail.SetPartEncoding(gomail.Unencoded),
|
||||
},
|
||||
AddressHeaders: make([]AddressHeader, 0),
|
||||
Headers: make([]Header, 0),
|
||||
AlternativeBodies: make([]Body, 0),
|
||||
}
|
||||
|
||||
for _, f := range funcs {
|
||||
f(opt)
|
||||
}
|
||||
|
||||
conn, err := m.openConnection()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not open connection")
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
message := gomail.NewMessage(gomail.SetCharset(opt.Charset))
|
||||
|
||||
for _, h := range opt.AddressHeaders {
|
||||
message.SetAddressHeader(h.Field, h.Address, h.Name)
|
||||
}
|
||||
|
||||
for _, h := range opt.Headers {
|
||||
message.SetHeader(h.Field, h.Values...)
|
||||
}
|
||||
|
||||
message.SetBody(opt.Body.Type, opt.Body.Content, opt.Body.PartSetting)
|
||||
|
||||
for _, b := range opt.AlternativeBodies {
|
||||
message.AddAlternative(b.Type, b.Content, b.PartSetting)
|
||||
}
|
||||
|
||||
if err := gomail.Send(conn, message); err != nil {
|
||||
return errors.Wrap(err, "could not send message")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mailer) openConnection() (gomail.SendCloser, error) {
|
||||
dialer := gomail.NewDialer(
|
||||
m.opt.Host,
|
||||
m.opt.Port,
|
||||
m.opt.User,
|
||||
m.opt.Password,
|
||||
)
|
||||
|
||||
if m.opt.InsecureSkipVerify {
|
||||
dialer.TLSConfig = &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
}
|
||||
|
||||
conn, err := dialer.Dial()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not dial smtp server")
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
33
internal/mail/service.go
Normal file
33
internal/mail/service.go
Normal file
@ -0,0 +1,33 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"gitlab.com/wpetit/goweb/service"
|
||||
)
|
||||
|
||||
const ServiceName service.Name = "mail"
|
||||
|
||||
// From retrieves the mail service in the given container
|
||||
func From(container *service.Container) (*Mailer, error) {
|
||||
service, err := container.Service(ServiceName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
srv, ok := service.(*Mailer)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
||||
}
|
||||
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
// Must retrieves the mail service in the given container or panic otherwise
|
||||
func Must(container *service.Container) *Mailer {
|
||||
srv, err := From(container)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
Reference in New Issue
Block a user