Files
Docwell/Server/internal/mail/mail.go
T
tajniak81andClaude Opus 5.5 b9be8be156
CI / go (Server) (push) Failing after 22m50s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 8m47s
CI / desktop (ubuntu-latest) (push) Successful in 2h56m50s
CI / mobile (push) Failing after 5m33s
CI / docker (push) Successful in 1h43m8s
Docs / build (push) Successful in 16m2s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Run docwell-db as a stock PocketBase and let the server set it up
docwell-db no longer needs a Docwell image: the stack runs
tajniak81/pocketbase-docker-root as it is, and docwell-server does what
the migrations and pb_hooks used to do inside PocketBase.

On start, docwell-server now:
- waits for PocketBase and imports Docwell's collections from an
  embedded snapshot (internal/pbschema). Existing collections and
  fields are matched by name, so their data is kept; the admin-managed
  users options (OAuth2 providers, magic link) are left untouched.
- writes the DOCWELL_APP_NAME, DOCWELL_WEB_URL, DOCWELL_MAIL_FROM,
  DOCWELL_SMTP_* and DOCWELL_S3_* settings into PocketBase, as the
  boot hook did.

It also replaces the hook's two routes: token checks use PocketBase's
auth-refresh (the organization name is read as the superuser), and
Docwell's own emails go out over SMTP from the server. PocketBase never
returns the SMTP password, so the server keeps a copy in the
superuser-only instance_settings collection; a password saved before
this change has to be entered once more in the admin panel.

Database/ keeps pb_migrations as the source of the schema;
export-schema.sh regenerates the snapshot from them. Database/Dockerfile
and pb_hooks are removed, the release no longer builds docwell-db, the
AIO image runs PocketBase without migrations or hooks, and the compose
files default DB_IMAGE to the stock image and pass the settings to the
server instead of the database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 20:09:38 +01:00

52 lines
1.5 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
// Package mail sends Docwell's own emails (invites, notifications, test
// mails). They go out over SMTP with the same mail server configuration
// PocketBase uses for its auth emails (see SMTPSender).
package mail
import (
"bytes"
"context"
"embed"
"fmt"
"html/template"
"strings"
texttemplate "text/template"
)
// Message is one email. HTML and Text are alternative bodies.
type Message struct {
To string `json:"to"`
Subject string `json:"subject"`
HTML string `json:"html"`
Text string `json:"text"`
}
// Sender delivers a message.
type Sender interface {
Send(ctx context.Context, m Message) error
}
//go:embed templates
var files embed.FS
var (
htmlTemplates = template.Must(template.ParseFS(files, "templates/*.html"))
textTemplates = texttemplate.Must(texttemplate.ParseFS(files, "templates/*.txt"))
)
// Render builds a message from the templates named name.html and name.txt.
// The first line of the text template is the subject.
func Render(to, name string, data any) (Message, error) {
var text, html bytes.Buffer
if err := textTemplates.ExecuteTemplate(&text, name+".txt", data); err != nil {
return Message{}, fmt.Errorf("mail: %s.txt: %w", name, err)
}
if err := htmlTemplates.ExecuteTemplate(&html, name+".html", data); err != nil {
return Message{}, fmt.Errorf("mail: %s.html: %w", name, err)
}
subject, body, _ := strings.Cut(text.String(), "\n")
return Message{To: to, Subject: strings.TrimSpace(subject), HTML: html.String(), Text: strings.TrimSpace(body) + "\n"}, nil
}