Skip to content

Commit

Permalink
added support to send smtp mail with go-mail (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
irdaislakhuafa authored Jan 6, 2024
1 parent e729a4e commit 02e8a08
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ go 1.20
require github.com/rs/zerolog v1.31.0

require (
github.com/go-mail/gomail v2.3.1+incompatible // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/go-mail/gomail v2.3.1+incompatible h1:hPrvM8Ncd4AM/OUHssNc6vclHfPKVcV/JuIwbDkcSq8=
github.com/go-mail/gomail v2.3.1+incompatible/go.mod h1:3QIXh5Fu0sf8i95raYpBqdBzROEbW00No9d24bcbPp0=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
Expand All @@ -17,3 +19,5 @@ golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
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=
29 changes: 29 additions & 0 deletions smtp/gomail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package smtp

import (
mail "github.com/go-mail/gomail"
"github.com/irdaislakhuafa/go-sdk/codes"
"github.com/irdaislakhuafa/go-sdk/errors"
)

type GoMailInterface interface {
DialAndSend(messages ...*mail.Message) error
}

type gomailImpl struct {
dialer *mail.Dialer
}

func InitGoMail(cfg Config) GoMailInterface {
result := &gomailImpl{
dialer: mail.NewDialer(cfg.Host, int(cfg.Port), cfg.Username, cfg.Password),
}
return result
}

func (g *gomailImpl) DialAndSend(messages ...*mail.Message) error {
if err := g.dialer.DialAndSend(messages...); err != nil {
return errors.NewWithCode(codes.CodeSMTP, "failed to send messages, %v", err)
}
return nil
}
8 changes: 8 additions & 0 deletions smtp/smtp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package smtp

type Config struct {
Username string
Password string
Host string
Port int64
}

0 comments on commit 02e8a08

Please sign in to comment.