High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Fix opt-in send errors silently ignored on signup form. Closes #2535.
Kailash Nadh4f6880d38e36
diff --git a/cmd/public.go b/cmd/public.goindex 15fad8e4..70a09ce1 100644--- a/cmd/public.go+++ b/cmd/public.go@@ -460,7 +460,7 @@ func (a *App) SubscriptionForm(c echo.Context) error {if err != nil {e, ok := err.(*echo.HTTPError)if !ok {- return e+ return err}return c.Render(e.Code, tplMessage, makeMsgTpl(a.i18n.T("public.errorTitle"), "", fmt.Sprintf("%s", e.Message)))@@ -648,12 +648,6 @@ func (a *App) processSubForm(c echo.Context) (bool, error) {return false, echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("public.noListsSelected"))}- // If there's no name, use the name bit from the e-mail.- req.Name = strings.TrimSpace(req.Name)- if req.Name == "" {- req.Name = strings.Split(req.Email, "@")[0]- }-// Validate fields.if len(req.Email) > 1000 {return false, echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("subscribers.invalidEmail"))@@ -666,7 +660,10 @@ func (a *App) processSubForm(c echo.Context) (bool, error) {req.Email = emreq.Name = strings.TrimSpace(req.Name)- if len(req.Name) == 0 || len(req.Name) > stdInputMaxLen {+ if len(req.Name) == 0 {+ // If there's no name, use the name bit from the e-mail.+ req.Name = strings.Split(req.Email, "@")[0]+ } else if len(req.Name) > stdInputMaxLen {return false, echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("subscribers.invalidName"))}@@ -690,26 +687,32 @@ func (a *App) processSubForm(c echo.Context) (bool, error) {Email: req.Email,Status: models.SubscriberStatusEnabled,}, nil, listUUIDs, false)- if err != nil {- // Subscriber already exists. Update subscriptions in the DB.- if e, ok := err.(*echo.HTTPError); ok && e.Code == http.StatusConflict {- // Get the subscriber from the DB by their email.- sub, err := a.core.GetSubscriber(0, "", req.Email)- if err != nil {- return false, err- }+ if err == nil {+ return hasOptin, nil+ }- // Update the subscriber's subscriptions in the DB.- _, hasOptin, err := a.core.UpdateSubscriberWithLists(sub.ID, sub, nil, listUUIDs, false, false)- if err != nil {- return false, err- }+ // Insert returned an error. Examine it.+ var lastErr = err- return hasOptin, nil+ // Subscriber already exists. Update subscriptions in the DB.+ if e, ok := err.(*echo.HTTPError); ok && e.Code == http.StatusConflict {+ // Get the subscriber from the DB by their email.+ sub, err := a.core.GetSubscriber(0, "", req.Email)+ if err != nil {+ return false, err}- return false, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("%s", err.(*echo.HTTPError).Message))+ // Update the subscriber's subscriptions in the DB.+ _, hasOptin, err := a.core.UpdateSubscriberWithLists(sub.ID, sub, nil, listUUIDs, false, false)+ if err == nil {+ return hasOptin, nil+ }+ lastErr = err}- return hasOptin, nil+ // Something else went wrong.+ if e, ok := lastErr.(*echo.HTTPError); ok {+ return false, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("%s", e.Message))+ }+ return false, echo.NewHTTPError(http.StatusInternalServerError, a.i18n.T("public.errorProcessingRequest"))}diff --git a/internal/core/subscribers.go b/internal/core/subscribers.goindex 0fac453b..d2285ec1 100644--- a/internal/core/subscribers.go+++ b/internal/core/subscribers.go@@ -324,7 +324,11 @@ func (c *Core) InsertSubscriber(sub models.Subscriber, listIDs []int, listUUIDshasOptin := falseif !preconfirm && c.consts.SendOptinConfirmation {// Send a confirmation e-mail (if there are any double opt-in lists).- num, _ := c.h.SendOptinConfirmation(out, listIDs)+ num, err := c.h.SendOptinConfirmation(out, listIDs)+ if err != nil {+ return out, hasOptin, err+ }+hasOptin = num > 0}@@ -409,7 +413,10 @@ func (c *Core) UpdateSubscriberWithLists(id int, sub models.Subscriber, listIDshasOptin := falseif !preconfirm && c.consts.SendOptinConfirmation {// Send a confirmation e-mail (if there are any double opt-in lists).- num, _ := c.h.SendOptinConfirmation(out, listIDs)+ num, err := c.h.SendOptinConfirmation(out, listIDs)+ if err != nil {+ return out, hasOptin, err+ }hasOptin = num > 0}