12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package mail
- import (
- "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
- )
- type Destination struct {
- CcAddresses []*string
- ToAddresses []*string
- }
- type Body struct {
- Html *string
- Text *string
- }
- type Mail struct {
- Template *string
- Sender *string
- Subject *string
- Body *Body
- TemplateData interface{}
- Destination *Destination
- }
- type SendMailOptions struct {
- Adapter string
- Sync bool
- }
- type MailAdapter interface {
- Send(mail *Mail, options *SendMailOptions) (err *errs.Error)
- SendTemplateEmail(mail *Mail, options *SendMailOptions) (err *errs.Error)
- }
- type MailAdapters = map[string]MailAdapter
- var (
- Adapters = MailAdapters{}
- )
- func init(){
- Adapters["default"] = &AwsSESAdapter{}
- }
- func Send(mail *Mail, options *SendMailOptions) (err *errs.Error) {
- var (
- adapterID = "default"
- adapter MailAdapter
- found bool
- sendSync = true
- sendHandler func(mail *Mail, options *SendMailOptions) (err *errs.Error)
- )
-
- if options != nil {
- if options.Adapter != "" { adapterID = options.Adapter}
- if options.Sync { sendSync = options.Sync}
- }
-
- if adapter, found = Adapters[adapterID]; found {
- if mail.Template != nil {
- sendHandler = adapter.SendTemplateEmail
- } else {
- sendHandler = adapter.Send
- }
- if sendSync {
- err = sendHandler(mail, options)
- }else {
- go sendHandler(mail, options)
- }
- }
-
- return
- }
|