MCPcopy
hub / github.com/knadh/listmonk / TestSMTPSettings

Method TestSMTPSettings

cmd/settings.go:372–425  ·  view source on GitHub ↗

TestSMTPSettings returns the log entries stored in the log buffer.

(c echo.Context)

Source from the content-addressed store, hash-verified

370
371// TestSMTPSettings returns the log entries stored in the log buffer.
372func (a *App) TestSMTPSettings(c echo.Context) error {
373 // Copy the raw JSON post body.
374 reqBody, err := io.ReadAll(c.Request().Body)
375 if err != nil {
376 a.log.Printf("error reading SMTP test: %v", err)
377 return echo.NewHTTPError(http.StatusBadRequest, a.i18n.Ts("globals.messages.internalError"))
378 }
379
380 // Load the JSON into koanf to parse SMTP settings properly including timestrings.
381 ko := koanf.New(".")
382 if err := ko.Load(rawbytes.Provider(reqBody), koanfjson.Parser()); err != nil {
383 a.log.Printf("error unmarshalling SMTP test request: %v", err)
384 return echo.NewHTTPError(http.StatusBadRequest, a.i18n.Ts("globals.messages.internalError"))
385 }
386
387 req := email.Server{}
388 if err := ko.UnmarshalWithConf("", &req, koanf.UnmarshalConf{Tag: "json"}); err != nil {
389 a.log.Printf("error scanning SMTP test request: %v", err)
390 return echo.NewHTTPError(http.StatusBadRequest, a.i18n.Ts("globals.messages.internalError"))
391 }
392
393 to := ko.String("email")
394 if to == "" {
395 return echo.NewHTTPError(http.StatusBadRequest, a.i18n.Ts("globals.messages.missingFields", "name", "email"))
396 }
397
398 // Initialize a new SMTP pool.
399 req.MaxConns = 1
400 req.IdleTimeout = time.Second * 2
401 req.PoolWaitTimeout = time.Second * 2
402 msgr, err := email.New("", req)
403 if err != nil {
404 return echo.NewHTTPError(http.StatusBadRequest,
405 a.i18n.Ts("globals.messages.errorCreating", "name", "SMTP", "error", err.Error()))
406 }
407
408 // Render the test email template body.
409 var b bytes.Buffer
410 if err := notifs.Tpls.ExecuteTemplate(&b, "smtp-test", nil); err != nil {
411 a.log.Printf("error compiling notification template '%s': %v", "smtp-test", err)
412 return err
413 }
414
415 m := models.Message{}
416 m.From = a.cfg.FromEmail
417 m.To = []string{to}
418 m.Subject = a.i18n.T("settings.smtp.testConnection")
419 m.Body = b.Bytes()
420 if err := msgr.Push(m); err != nil {
421 return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
422 }
423
424 return c.JSON(http.StatusOK, okResp{a.bufLog.Lines()})
425}
426
427func (a *App) GetAboutInfo(c echo.Context) error {
428 var mem runtime.MemStats

Callers

nothing calls this directly

Calls 7

NewFunction · 0.92
TsMethod · 0.80
LoadMethod · 0.80
TMethod · 0.80
JSONMethod · 0.80
LinesMethod · 0.80
PushMethod · 0.65

Tested by

no test coverage detected