Send mailing list emails

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2026-03-05 18:05:41 +01:00
parent aa01c40184
commit 85ec106cd6
67 changed files with 7615 additions and 160 deletions

View File

@@ -0,0 +1,124 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package mailactions
import (
"errors"
"html/template"
"net/http"
"net/url"
"go.probo.inc/probo/pkg/mailman"
)
func confirmGetHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
if token == "" {
renderPage(
w,
http.StatusBadRequest,
page{
Title: "Invalid link",
Heading: "Invalid link",
Body: "This confirmation link is missing required information. Please use the link from your email.",
},
)
return
}
renderPage(
w,
http.StatusOK,
page{
Title: "Confirm subscription",
Heading: "Confirm your subscription",
Body: "Click the button below to confirm that you want to receive updates.",
Form: &form{
ActionURL: template.URL("?token=" + url.QueryEscape(token)),
Button: "Confirm subscription",
Danger: false,
},
},
)
}
}
func confirmPostHandler(mailmanSvc *mailman.Service, tokenSecret string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
if token == "" {
renderPage(
w,
http.StatusBadRequest,
page{
Title: "Invalid link",
Heading: "Invalid link",
Body: "This confirmation link is missing required information. Please use the link from your email.",
},
)
return
}
data, err := mailman.ValidateConfirmToken(tokenSecret, token)
if err != nil {
renderPage(
w,
http.StatusUnauthorized,
page{
Title: "Invalid link",
Heading: "Invalid or expired link",
Body: "This confirmation link is invalid or has expired. Confirmation links are valid for 30 days — please re-subscribe to get a new one.",
},
)
return
}
if err := mailmanSvc.ConfirmSubscriberByEmail(r.Context(), data.MailingListID, data.Email); err != nil {
if errors.Is(err, mailman.ErrSubscriberNotFound) {
renderPage(
w,
http.StatusNotFound, page{
Title: "Not found",
Heading: "Subscription not found",
Body: "We could not find your subscription. It may have already been cancelled or this link was already used.",
},
)
return
}
renderPage(
w,
http.StatusInternalServerError,
page{
Title: "Something went wrong",
Heading: "Something went wrong",
Body: "We could not confirm your subscription. Please try again later.",
},
)
return
}
renderPage(
w,
http.StatusOK,
page{
Title: "Subscription confirmed",
Heading: "Subscription confirmed",
Body: "You're now subscribed and will receive updates.",
},
)
}
}

View File

@@ -0,0 +1,40 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// Package mailactions provides HTTP handlers for mailing list subscription
// management. All routes are mounted at /mail-actions/ with no API version
// prefix so they can be linked directly from emails and bookmarked by users.
//
// GET /mail-actions/unsubscribe shows an unsubscribe confirmation page
// POST /mail-actions/unsubscribe RFC 8058 one-click unsubscribe
// GET /mail-actions/confirm shows a subscription confirmation page
// POST /mail-actions/confirm confirms a pending subscription
package mailactions
import (
"github.com/go-chi/chi/v5"
"go.probo.inc/probo/pkg/mailman"
)
func NewMux(mailmanSvc *mailman.Service, tokenSecret string) *chi.Mux {
r := chi.NewMux()
r.Get("/unsubscribe", unsubscribeGetHandler())
r.Post("/unsubscribe", unsubscribePostHandler(mailmanSvc, tokenSecret))
r.Get("/confirm", confirmGetHandler())
r.Post("/confirm", confirmPostHandler(mailmanSvc, tokenSecret))
return r
}

View File

@@ -0,0 +1,45 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package mailactions
import (
_ "embed"
"html/template"
"net/http"
)
//go:embed templates/page.html.tmpl
var pageTmplHTML string
type form struct {
ActionURL template.URL
Button string
Danger bool
}
type page struct {
Title string
Heading string
Body string
Form *form
}
var tmpl = template.Must(template.New("page.html.tmpl").Parse(pageTmplHTML))
func renderPage(w http.ResponseWriter, status int, p page) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(status)
_ = tmpl.Execute(w, p)
}

View File

@@ -0,0 +1,99 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{.Title}}</title>
<style>
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background: #f3f4f6;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 1.5rem;
color: #111827;
}
.card {
background: #fff;
border-radius: 0.75rem;
border: 1px solid #e5e7eb;
padding: 2.5rem 2rem;
max-width: 26rem;
width: 100%;
text-align: center;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
h1 {
font-size: 1.25rem;
font-weight: 600;
line-height: 1.4;
margin-bottom: 0.625rem;
}
p {
font-size: 0.9375rem;
color: #6b7280;
line-height: 1.6;
margin-top: 0.5rem;
}
form {
margin-top: 1.75rem;
}
button {
font-family: inherit;
font-size: 0.9375rem;
font-weight: 500;
padding: 0.625rem 1.5rem;
border-radius: 0.5rem;
border: none;
cursor: pointer;
line-height: 1.5;
}
.primary {
background: #111827;
color: #fff;
}
.primary:hover {
background: #1f2937;
}
.danger {
background: #dc2626;
color: #fff;
}
.danger:hover {
background: #b91c1c;
}
</style>
</head>
<body>
<div class="card">
<h1>{{.Heading}}</h1>
<p>{{.Body}}</p>
{{- if .Form}}
<form method="POST" action="{{.Form.ActionURL}}">
<button type="submit" class="{{if .Form.Danger}}danger{{else}}primary{{end}}">
{{.Form.Button}}
</button>
</form>
{{- end}}
</div>
</body>
</html>

View File

@@ -0,0 +1,116 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package mailactions
import (
"errors"
"html/template"
"net/http"
"net/url"
"go.probo.inc/probo/pkg/mailman"
)
func unsubscribeGetHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
if token == "" {
renderPage(
w,
http.StatusBadRequest,
page{
Title: "Invalid link",
Heading: "Invalid link",
Body: "This unsubscribe link is missing required information. Please use the link from your email.",
},
)
return
}
renderPage(
w,
http.StatusOK,
page{
Title: "Unsubscribe",
Heading: "Unsubscribe from mailing list",
Body: "Click the button below to confirm that you no longer want to receive updates.",
Form: &form{
ActionURL: template.URL("?token=" + url.QueryEscape(token)),
Button: "Confirm unsubscribe",
Danger: true,
},
},
)
}
}
func unsubscribePostHandler(mailmanSvc *mailman.Service, tokenSecret string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
if token == "" {
renderPage(
w,
http.StatusBadRequest,
page{
Title: "Invalid link",
Heading: "Invalid link",
Body: "This unsubscribe link is missing required information. Please use the link from your email.",
},
)
return
}
data, err := mailman.ValidateUnsubscribeToken(tokenSecret, token)
if err != nil {
renderPage(
w,
http.StatusUnauthorized,
page{
Title: "Invalid link",
Heading: "Invalid or expired link",
Body: "This unsubscribe link is invalid or has expired.",
},
)
return
}
if err := mailmanSvc.UnsubscribeByEmail(r.Context(), data.MailingListID, data.Email); err != nil {
if !errors.Is(err, mailman.ErrSubscriberNotFound) {
renderPage(
w,
http.StatusInternalServerError,
page{
Title: "Something went wrong",
Heading: "Something went wrong",
Body: "We could not process your request. Please try again later.",
},
)
return
}
}
// Also success when already unsubscribed — unsubscribe is idempotent
// per RFC 8058.
renderPage(
w,
http.StatusOK,
page{
Title: "Unsubscribed",
Heading: "You've been unsubscribed",
Body: "You will no longer receive updates.",
},
)
}
}