diff --git a/cmd/lavender/serve.go b/cmd/lavender/serve.go index 6a34a99..8714667 100644 --- a/cmd/lavender/serve.go +++ b/cmd/lavender/serve.go @@ -8,6 +8,7 @@ import ( "github.com/1f349/lavender/conf" "github.com/1f349/lavender/database" "github.com/1f349/lavender/logger" + "github.com/1f349/lavender/mail" "github.com/1f349/lavender/role" "github.com/1f349/lavender/server" "github.com/1f349/lavender/web" @@ -96,6 +97,11 @@ func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) } wd := filepath.Dir(configPathAbs) + mailSender, err := mail.New(&config.Mail, wd, "") + if err != nil { + logger.Logger.Fatal("Failed to create mail sender", "err", err) + } + // load the keystore private and public keys keyDir := filepath.Join(wd, "keystore") err = os.MkdirAll(keyDir, 0700) @@ -131,7 +137,7 @@ func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) } mux := httprouter.New() - server.SetupRouter(mux, config, db, signingKey) + server.SetupRouter(mux, config, mailSender, db, signingKey) srv := &http.Server{ Handler: mux, ReadTimeout: time.Minute, diff --git a/conf/conf.go b/conf/conf.go index bd8f734..841e7ee 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -2,7 +2,7 @@ package conf import ( "github.com/1f349/lavender/issuer" - "github.com/1f349/lavender/mail" + "github.com/1f349/simplemail" ) type Conf struct { @@ -13,6 +13,6 @@ type Conf struct { Kid string `yaml:"kid"` Namespace string `yaml:"namespace"` OtpIssuer string `yaml:"otpIssuer"` - Mail mail.Mail `yaml:"mail"` + Mail simplemail.Mail `yaml:"mail"` SsoServices []issuer.SsoConfig `yaml:"ssoServices"` } diff --git a/server/mail.go b/server/mail.go index 79f4b16..038567c 100644 --- a/server/mail.go +++ b/server/mail.go @@ -113,7 +113,7 @@ func (h *httpServer) MailDelete(rw http.ResponseWriter, req *http.Request, param return } - err = h.conf.Mail.SendEmailTemplate("mail-account-delete", "Account Deletion", userInfo.Name, address, nil) + err = h.mailSender.SendEmailTemplate("mail-account-delete", "Account Deletion", userInfo.Name, address, nil) if err != nil { http.Error(rw, "Failed to send confirmation email.", http.StatusInternalServerError) return diff --git a/server/server.go b/server/server.go index 613adb0..28bb9e4 100644 --- a/server/server.go +++ b/server/server.go @@ -9,6 +9,7 @@ import ( "github.com/1f349/lavender/database" "github.com/1f349/lavender/issuer" "github.com/1f349/lavender/logger" + "github.com/1f349/lavender/mail" "github.com/1f349/lavender/web" "github.com/1f349/mjwt" "github.com/go-oauth2/oauth2/v4/manage" @@ -28,6 +29,7 @@ type httpServer struct { db *database.Queries conf conf.Conf signingKey *mjwt.Issuer + mailSender *mail.Mail manager *issuer.Manager // mailLinkCache contains a mapping of verify uuids to user uuids @@ -53,7 +55,7 @@ type mailLinkKey struct { data string } -func SetupRouter(r *httprouter.Router, config conf.Conf, db *database.Queries, signingKey *mjwt.Issuer) { +func SetupRouter(r *httprouter.Router, config conf.Conf, mailSender *mail.Mail, db *database.Queries, signingKey *mjwt.Issuer) { // remove last slash from baseUrl config.BaseUrl = strings.TrimRight(config.BaseUrl, "/") @@ -67,6 +69,7 @@ func SetupRouter(r *httprouter.Router, config conf.Conf, db *database.Queries, s db: db, conf: config, signingKey: signingKey, + mailSender: mailSender, mailLinkCache: cache.New[mailLinkKey, string](),