SubscribeToList POSTs name+email to the configured Listmonk public list URL. Returns nil on 2xx, a wrapped error otherwise.
(ctx context.Context, name, email string)
| 34 | // SubscribeToList POSTs name+email to the configured Listmonk public list URL. |
| 35 | // Returns nil on 2xx, a wrapped error otherwise. |
| 36 | func SubscribeToList(ctx context.Context, name, email string) error { |
| 37 | form := url.Values{} |
| 38 | form.Set("NAME", name) |
| 39 | form.Set("EMAIL", email) |
| 40 | form.Set(honeypotField, "") |
| 41 | |
| 42 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, marketingSubscribeURL, strings.NewReader(form.Encode())) |
| 43 | if err != nil { |
| 44 | return fmt.Errorf("build request: %w", err) |
| 45 | } |
| 46 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 47 | |
| 48 | client := &http.Client{Timeout: 10 * time.Second} |
| 49 | resp, err := client.Do(req) |
| 50 | if err != nil { |
| 51 | return fmt.Errorf("post to marketing list: %w", err) |
| 52 | } |
| 53 | defer func() { |
| 54 | _, _ = io.Copy(io.Discard, resp.Body) |
| 55 | _ = resp.Body.Close() |
| 56 | }() |
| 57 | |
| 58 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 59 | return fmt.Errorf("marketing list returned status %d", resp.StatusCode) |
| 60 | } |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | // AutoSubscribeIfHosted enrolls a freshly-created user in the newsletter when |
| 65 | // the instance is running in admin_mode (managed/SaaS deployments). On |
no test coverage detected