refactor: make mail init a lifecycle hook so it's called after initCheckRequiredConfig
This commit is contained in:
parent
33af108d39
commit
f89be1fef8
|
@ -1,6 +1,8 @@
|
||||||
package mailer
|
package mailer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"git.lumeweb.com/LumeWeb/portal/config"
|
"git.lumeweb.com/LumeWeb/portal/config"
|
||||||
"github.com/wneessen/go-mail"
|
"github.com/wneessen/go-mail"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
|
@ -41,30 +43,37 @@ func (m *Mailer) TemplateSend(template string, subjectVars TemplateData, bodyVar
|
||||||
return m.client.DialAndSend(msg)
|
return m.client.DialAndSend(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMailer(config *config.Manager, templateRegistry *TemplateRegistry, logger *zap.Logger) (*Mailer, error) {
|
func NewMailer(lc fx.Lifecycle, config *config.Manager, logger *zap.Logger, templateRegistry *TemplateRegistry) (*Mailer, error) {
|
||||||
var options []mail.Option
|
m := &Mailer{config: config, logger: logger, templateRegistry: templateRegistry}
|
||||||
|
|
||||||
if config.Config().Core.Mail.Port != 0 {
|
lc.Append(fx.Hook{
|
||||||
options = append(options, mail.WithPort(config.Config().Core.Mail.Port))
|
OnStart: func(context.Context) error {
|
||||||
}
|
var options []mail.Option
|
||||||
|
|
||||||
if config.Config().Core.Mail.AuthType != "" {
|
if config.Config().Core.Mail.Port != 0 {
|
||||||
options = append(options, mail.WithSMTPAuth(mail.SMTPAuthType(config.Config().Core.Mail.AuthType)))
|
options = append(options, mail.WithPort(config.Config().Core.Mail.Port))
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Config().Core.Mail.SSL {
|
if config.Config().Core.Mail.AuthType != "" {
|
||||||
options = append(options, mail.WithSSLPort(true))
|
options = append(options, mail.WithSMTPAuth(mail.SMTPAuthType(config.Config().Core.Mail.AuthType)))
|
||||||
}
|
}
|
||||||
|
|
||||||
options = append(options, mail.WithUsername(config.Config().Core.Mail.Username))
|
if config.Config().Core.Mail.SSL {
|
||||||
options = append(options, mail.WithPassword(config.Config().Core.Mail.Password))
|
options = append(options, mail.WithSSLPort(true))
|
||||||
|
}
|
||||||
|
|
||||||
client, err := mail.NewClient(config.Config().Core.Mail.Host, options...)
|
options = append(options, mail.WithUsername(config.Config().Core.Mail.Username))
|
||||||
if err != nil {
|
options = append(options, mail.WithPassword(config.Config().Core.Mail.Password))
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
m := &Mailer{config: config, logger: logger, client: client, templateRegistry: templateRegistry}
|
client, err := mail.NewClient(config.Config().Core.Mail.Host, options...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.client = client
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue