Files
knowledge-governance/internal/qawemail/store.go
T
hermes 1dda055e4c
build / build (push) Successful in 1m10s
fix(qa): 授权码加密不再绑定设置版本
AAD 只含租户,避免任何设置变更使已存密码失效;旧行在解密时按历史版本回退,无需重填授权码。
2026-10-26 16:11:17 +08:00

809 lines
29 KiB
Go

package qawemail
import (
"context"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"net"
"net/mail"
"net/smtp"
"os"
"strings"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
)
var (
ErrUnavailable = errors.New("weekly mail store unavailable")
ErrInvalid = errors.New("weekly mail invalid input")
ErrConflict = errors.New("weekly mail settings changed")
ErrNoPassword = errors.New("weekly mail password missing")
)
// Settings never carries the SMTP password; HasPassword only says whether one is stored.
type Settings struct {
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username"`
From string `json:"from"`
Recipients []string `json:"recipients"`
Cc []string `json:"cc_recipients"`
Review string `json:"review_address"`
Weekday int `json:"weekday"`
Hour int `json:"hour"`
Enabled bool `json:"enabled"`
HasPassword bool `json:"has_password"`
Revision int64 `json:"revision"`
UpdatedBy string `json:"updated_by"`
UpdatedAt time.Time `json:"updated_at"`
}
type SettingsInput struct {
BaseRevision int64 `json:"base_revision"`
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
ClearPassword bool `json:"clear_password"`
From string `json:"from"`
Recipients []string `json:"recipients"`
Cc []string `json:"cc_recipients"`
Review string `json:"review_address"`
Weekday int `json:"weekday"`
Hour int `json:"hour"`
Enabled bool `json:"enabled"`
}
type Confirmation struct {
Week string `json:"week"`
HasImage bool `json:"has_image"`
DraftRevision int64 `json:"draft_revision"`
BodySHA256 string `json:"body_sha256"`
ConfirmedBy string `json:"confirmed_by"`
ConfirmedAt time.Time `json:"confirmed_at"`
Current bool `json:"current"`
}
type Send struct {
Week string `json:"week"`
Revision int64 `json:"draft_revision"`
Status string `json:"status"`
Attempts int `json:"attempts"`
LastError string `json:"last_error"`
UpdatedAt time.Time `json:"updated_at"`
}
// InlineImage is embedded with a Content-ID so clients show it inside the body
// instead of as a detached attachment.
type InlineImage struct {
Name string
ContentType string
CID string
Bytes []byte
}
type Message struct {
To []string
Cc []string
From string
Subject string
HTML string
Text string
Images []InlineImage
}
// Mailer is the only outbound dependency; tests substitute it to prove the exact wire payload.
type Mailer interface {
Send(ctx context.Context, cfg SMTPConfig, msg Message) (string, error)
}
type SMTPConfig struct {
Host string
Port int
Username string
Password string
}
type Store struct {
pool *pgxpool.Pool
tenant string
keyFile string
mailer Mailer
now func() time.Time
}
func NewStore(pool *pgxpool.Pool, tenant, keyFile string, mailer Mailer) *Store {
if mailer == nil {
mailer = SMTPMailer{}
}
return &Store{pool: pool, tenant: tenant, keyFile: keyFile, mailer: mailer, now: time.Now}
}
func (s *Store) valid() bool {
return s != nil && s.pool != nil && strings.TrimSpace(s.tenant) != ""
}
func (s *Store) masterKey() ([]byte, error) {
info, err := os.Lstat(s.keyFile)
if err != nil || !info.Mode().IsRegular() || info.Mode().Perm() != 0o600 {
return nil, ErrUnavailable
}
raw, err := os.ReadFile(s.keyFile)
if err != nil || len(raw) != 32 {
return nil, ErrUnavailable
}
return raw, nil
}
func (s *Store) seal(secret string, revision int64) ([]byte, error) {
key, err := s.masterKey()
if err != nil {
return nil, err
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, ErrUnavailable
}
aead, err := cipher.NewGCM(block)
if err != nil {
return nil, ErrUnavailable
}
nonce := make([]byte, aead.NonceSize())
if _, err = rand.Read(nonce); err != nil {
return nil, ErrUnavailable
}
// The AAD must not follow the settings revision: rotating recipients would otherwise
// make a previously saved password undecryptable.
return aead.Seal(nonce, nonce, []byte(secret), []byte("weekly-mail:"+s.tenant)), nil
}
func (s *Store) unseal(sealed []byte, revision int64) (string, error) {
if len(sealed) == 0 {
return "", ErrNoPassword
}
key, err := s.masterKey()
if err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", ErrUnavailable
}
aead, err := cipher.NewGCM(block)
if err != nil || len(sealed) < aead.NonceSize() {
return "", ErrUnavailable
}
nonce, body := sealed[:aead.NonceSize()], sealed[aead.NonceSize():]
raw, err := aead.Open(nil, nonce, body, []byte("weekly-mail:"+s.tenant))
if err == nil {
return string(raw), nil
}
// Legacy rows were sealed with the revision in the AAD; recover them without forcing
// the operator to paste the authorization code again.
for candidate := int64(1); candidate <= 8; candidate++ {
if plain, openErr := aead.Open(nil, nonce, body, []byte(fmt.Sprintf("weekly-mail:%s:%d", s.tenant, candidate))); openErr == nil {
return string(plain), nil
}
}
return "", ErrUnavailable
}
func validAddress(value string) bool {
parsed, err := mail.ParseAddress(strings.TrimSpace(value))
return err == nil && parsed.Address == strings.TrimSpace(value) && strings.Contains(parsed.Address, "@")
}
func normalize(input SettingsInput) (SettingsInput, error) {
input.Host = strings.TrimSpace(input.Host)
input.Username = strings.TrimSpace(input.Username)
input.From = strings.TrimSpace(input.From)
input.Review = strings.TrimSpace(input.Review)
seen := map[string]bool{}
recipients := make([]string, 0, len(input.Recipients))
for _, raw := range input.Recipients {
address := strings.TrimSpace(raw)
if address == "" || !validAddress(address) || seen[strings.ToLower(address)] {
return input, ErrInvalid
}
seen[strings.ToLower(address)] = true
recipients = append(recipients, address)
}
input.Recipients = recipients
cc := make([]string, 0, len(input.Cc))
for _, raw := range input.Cc {
address := strings.TrimSpace(raw)
if address == "" {
continue
}
// A copy that is also a primary recipient would receive the mail twice.
if !validAddress(address) || seen[strings.ToLower(address)] {
return input, ErrInvalid
}
seen[strings.ToLower(address)] = true
cc = append(cc, address)
}
input.Cc = cc
if input.Host != "" && !strings.Contains(input.Host, " ") && !strings.Contains(input.Host, "/") {
if _, err := net.LookupHost(input.Host); err != nil {
// A host that does not resolve yet stays a validation error, not a silent no-op.
return input, ErrInvalid
}
}
if input.Port == 0 {
input.Port = 465
}
if input.Weekday == 0 {
input.Weekday = 1
}
if input.Port < 1 || input.Port > 65535 || input.Weekday < 1 || input.Weekday > 7 || input.Hour < 0 || input.Hour > 23 {
return input, ErrInvalid
}
if input.Review != "" && !validAddress(input.Review) {
return input, ErrInvalid
}
if input.Enabled {
if input.Host == "" || input.From == "" || !validAddress(input.From) || len(input.Recipients) == 0 {
return input, ErrInvalid
}
}
return input, nil
}
func (s *Store) ReadSettings(ctx context.Context) (Settings, error) {
out := Settings{Port: 465, Weekday: 1, Hour: 9, Recipients: []string{}, Cc: []string{}}
if !s.valid() {
return out, ErrUnavailable
}
var sealed []byte
err := s.pool.QueryRow(ctx, `SELECT smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,review_address,send_weekday,send_hour,enabled,revision,updated_by,updated_at FROM qa_weekly_email_settings WHERE tenant_key=$1`, s.tenant).
Scan(&out.Host, &out.Port, &out.Username, &sealed, &out.From, &out.Recipients, &out.Cc, &out.Review, &out.Weekday, &out.Hour, &out.Enabled, &out.Revision, &out.UpdatedBy, &out.UpdatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return out, nil
}
if err != nil {
return Settings{}, ErrUnavailable
}
if out.Recipients == nil {
out.Recipients = []string{}
}
if out.Cc == nil {
out.Cc = []string{}
}
out.HasPassword = len(sealed) > 0
return out, nil
}
func (s *Store) SaveSettings(ctx context.Context, actor string, input SettingsInput) (Settings, error) {
if !s.valid() || strings.TrimSpace(actor) == "" {
return Settings{}, ErrUnavailable
}
normalized, err := normalize(input)
if err != nil {
return Settings{}, err
}
ctx, cancel := context.WithTimeout(ctx, 8*time.Second)
defer cancel()
tx, err := s.pool.Begin(ctx)
if err != nil {
return Settings{}, ErrUnavailable
}
defer tx.Rollback(context.Background())
if _, err = tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1,257))`, "weekly-mail:"+s.tenant); err != nil {
return Settings{}, ErrUnavailable
}
var current int64
var sealed []byte
var passwordRevision int64
err = tx.QueryRow(ctx, `SELECT revision,smtp_password_sealed,password_revision FROM qa_weekly_email_settings WHERE tenant_key=$1 FOR UPDATE`, s.tenant).Scan(&current, &sealed, &passwordRevision)
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return Settings{}, ErrUnavailable
}
if current != input.BaseRevision {
return Settings{}, ErrConflict
}
next := current + 1
switch {
case normalized.ClearPassword:
sealed, passwordRevision = nil, 0
case strings.TrimSpace(normalized.Password) != "":
if sealed, err = s.seal(normalized.Password, next); err != nil {
return Settings{}, err
}
passwordRevision = next
}
if _, err = tx.Exec(ctx, `INSERT INTO qa_weekly_email_settings(tenant_key,smtp_host,smtp_port,smtp_username,smtp_password_sealed,from_address,recipients,cc_recipients,review_address,send_weekday,send_hour,enabled,revision,updated_by,updated_at,password_revision)
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,now(),$15)
ON CONFLICT (tenant_key) DO UPDATE SET smtp_host=excluded.smtp_host,smtp_port=excluded.smtp_port,smtp_username=excluded.smtp_username,smtp_password_sealed=excluded.smtp_password_sealed,from_address=excluded.from_address,recipients=excluded.recipients,cc_recipients=excluded.cc_recipients,review_address=excluded.review_address,send_weekday=excluded.send_weekday,send_hour=excluded.send_hour,enabled=excluded.enabled,revision=excluded.revision,updated_by=excluded.updated_by,updated_at=now(),password_revision=excluded.password_revision`,
s.tenant, normalized.Host, normalized.Port, normalized.Username, sealed, normalized.From, normalized.Recipients, normalized.Cc, normalized.Review, normalized.Weekday, normalized.Hour, normalized.Enabled, next, actor, passwordRevision); err != nil {
return Settings{}, ErrUnavailable
}
if err = tx.Commit(ctx); err != nil {
return Settings{}, ErrUnavailable
}
return s.ReadSettings(ctx)
}
func bodyHash(markdown string) string {
sum := sha256.Sum256([]byte(markdown))
return hex.EncodeToString(sum[:])
}
// Confirm binds the confirmation to the exact revision and body the operator just reviewed,
// so a later edit invalidates it instead of silently mailing changed text.
func (s *Store) Confirm(ctx context.Context, week, actor string) (Confirmation, error) {
if !s.valid() || strings.TrimSpace(actor) == "" || strings.TrimSpace(week) == "" {
return Confirmation{}, ErrUnavailable
}
ctx, cancel := context.WithTimeout(ctx, 8*time.Second)
defer cancel()
var revision int64
var markdown string
if err := s.pool.QueryRow(ctx, `SELECT revision,markdown FROM qa_ops_weekly_drafts WHERE tenant_key=$1 AND week=$2`, s.tenant, week).Scan(&revision, &markdown); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return Confirmation{}, ErrInvalid
}
return Confirmation{}, ErrUnavailable
}
if revision <= 0 || strings.TrimSpace(markdown) == "" {
return Confirmation{}, ErrInvalid
}
var out Confirmation
err := s.pool.QueryRow(ctx, `INSERT INTO qa_weekly_confirmations(tenant_key,week,draft_revision,body_sha256,confirmed_by,confirmed_at)
VALUES($1,$2,$3,$4,$5,now())
ON CONFLICT (tenant_key,week) DO UPDATE SET draft_revision=excluded.draft_revision,body_sha256=excluded.body_sha256,confirmed_by=excluded.confirmed_by,confirmed_at=now()
RETURNING week,draft_revision,body_sha256,confirmed_by,confirmed_at`, s.tenant, week, revision, bodyHash(markdown), actor).
Scan(&out.Week, &out.DraftRevision, &out.BodySHA256, &out.ConfirmedBy, &out.ConfirmedAt)
if err != nil {
return Confirmation{}, ErrUnavailable
}
out.Current = true
return out, nil
}
func (s *Store) Unconfirm(ctx context.Context, week string) error {
if !s.valid() {
return ErrUnavailable
}
if _, err := s.pool.Exec(ctx, `DELETE FROM qa_weekly_confirmations WHERE tenant_key=$1 AND week=$2`, s.tenant, week); err != nil {
return ErrUnavailable
}
return nil
}
func (s *Store) ReadConfirmation(ctx context.Context, week string) (*Confirmation, error) {
if !s.valid() {
return nil, ErrUnavailable
}
var out Confirmation
var currentRevision int64
var currentMarkdown string
err := s.pool.QueryRow(ctx, `SELECT c.week,c.draft_revision,c.body_sha256,c.confirmed_by,c.confirmed_at,d.revision,d.markdown,c.dashboard_png IS NOT NULL
FROM qa_weekly_confirmations c LEFT JOIN qa_ops_weekly_drafts d ON d.tenant_key=c.tenant_key AND d.week=c.week
WHERE c.tenant_key=$1 AND c.week=$2`, s.tenant, week).
Scan(&out.Week, &out.DraftRevision, &out.BodySHA256, &out.ConfirmedBy, &out.ConfirmedAt, &currentRevision, &currentMarkdown, &out.HasImage)
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, ErrUnavailable
}
out.Current = currentRevision == out.DraftRevision && currentMarkdown != "" && bodyHash(currentMarkdown) == out.BodySHA256
return &out, nil
}
func (s *Store) Sends(ctx context.Context, week string) ([]Send, error) {
if !s.valid() {
return nil, ErrUnavailable
}
rows, err := s.pool.Query(ctx, `SELECT week,draft_revision,status,attempts,last_error,updated_at FROM qa_weekly_email_sends WHERE tenant_key=$1 AND ($2='' OR week=$2) ORDER BY updated_at DESC LIMIT 50`, s.tenant, week)
if err != nil {
return nil, ErrUnavailable
}
defer rows.Close()
out := []Send{}
for rows.Next() {
var item Send
if err := rows.Scan(&item.Week, &item.Revision, &item.Status, &item.Attempts, &item.LastError, &item.UpdatedAt); err != nil {
return nil, ErrUnavailable
}
out = append(out, item)
}
return out, nil
}
type DueResult struct {
State string `json:"state"`
Week string `json:"week,omitempty"`
SentAt time.Time `json:"sent_at,omitempty"`
Reason string `json:"reason,omitempty"`
}
// weekStart resolves the Beijing Monday of an ISO week label; reports are due after that week closes.
func weekStart(week string) (time.Time, error) {
var year, number int
if _, err := fmt.Sscanf(week, "%4d-W%2d", &year, &number); err != nil || year < 2000 || year > 2200 || number < 1 || number > 53 {
return time.Time{}, ErrInvalid
}
location := time.FixedZone("CST", 8*3600)
jan4 := time.Date(year, 1, 4, 0, 0, 0, 0, location)
offset := (int(jan4.Weekday()) + 6) % 7
start := jan4.AddDate(0, 0, -offset+7*(number-1))
if _, got := start.ISOWeek(); got != number {
return time.Time{}, ErrInvalid
}
return start, nil
}
// slotMatches is the single definition of "the configured weekly slot is now".
func slotMatches(settings Settings, now time.Time) bool {
local := now.In(time.FixedZone("CST", 8*3600))
weekday := int(local.Weekday())
if weekday == 0 {
weekday = 7
}
return weekday == settings.Weekday && local.Hour() == settings.Hour
}
// reportClosed reports whether the ISO week is over and its report may go out.
func reportClosed(week string, now time.Time) bool {
start, err := weekStart(week)
if err != nil {
return false
}
return !now.Before(start.AddDate(0, 0, 7))
}
// SendDue mails only a confirmed, still-current, already-closed week at the configured slot.
// Every skip path returns a reason instead of pretending a message went out.
func (s *Store) SendDue(ctx context.Context, now time.Time) (DueResult, error) {
if !s.valid() {
return DueResult{}, ErrUnavailable
}
settings, err := s.ReadSettings(ctx)
if err != nil {
return DueResult{}, err
}
if !settings.Enabled {
return DueResult{State: "disabled", Reason: "未启用自动发送"}, nil
}
if !slotMatches(settings, now) {
return DueResult{State: "not_due", Reason: "未到设定发送时段"}, nil
}
rows, err := s.pool.Query(ctx, `SELECT c.week,c.draft_revision,c.body_sha256,d.revision,d.markdown
FROM qa_weekly_confirmations c JOIN qa_ops_weekly_drafts d ON d.tenant_key=c.tenant_key AND d.week=c.week
WHERE c.tenant_key=$1 ORDER BY c.week DESC`, s.tenant)
if err != nil {
return DueResult{}, ErrUnavailable
}
type candidate struct {
week string
revision int64
hash string
markdown string
}
candidates := []candidate{}
for rows.Next() {
var c candidate
var draftRevision int64
if err := rows.Scan(&c.week, &c.revision, &c.hash, &draftRevision, &c.markdown); err != nil {
rows.Close()
return DueResult{}, ErrUnavailable
}
if draftRevision != c.revision || bodyHash(c.markdown) != c.hash {
continue
}
candidates = append(candidates, c)
}
rows.Close()
for _, c := range candidates {
if !reportClosed(c.week, now) {
continue
}
var sent bool
if err := s.pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM qa_weekly_email_sends WHERE tenant_key=$1 AND week=$2 AND draft_revision=$3 AND status='sent')`, s.tenant, c.week, c.revision).Scan(&sent); err != nil {
return DueResult{}, ErrUnavailable
}
if sent {
continue
}
return s.send(ctx, settings, c.week, c.revision, c.markdown, now)
}
return DueResult{State: "nothing_confirmed", Reason: "没有已确认且已结束的周报"}, nil
}
// SaveDashboardImage stores the dashboard PNG that belongs to the currently confirmed revision,
// so a later body edit (which invalidates the confirmation) never mails a stale chart.
func (s *Store) SaveDashboardImage(ctx context.Context, week string, png []byte) error {
if !s.valid() || strings.TrimSpace(week) == "" || len(png) < 8 || len(png) > 8<<20 {
return ErrInvalid
}
if string(png[:8]) != "\x89PNG\r\n\x1a\n" {
return ErrInvalid
}
sum := sha256.Sum256(png)
tag, err := s.pool.Exec(ctx, `UPDATE qa_weekly_confirmations SET dashboard_png=$2,dashboard_sha256=$3,dashboard_saved_at=now()
WHERE tenant_key=$1 AND week=$4`, s.tenant, png, hex.EncodeToString(sum[:]), week)
if err != nil {
return ErrUnavailable
}
if tag.RowsAffected() == 0 {
return ErrInvalid
}
return nil
}
func (s *Store) dashboardImage(ctx context.Context, week string) ([]byte, error) {
var png []byte
err := s.pool.QueryRow(ctx, `SELECT dashboard_png FROM qa_weekly_confirmations WHERE tenant_key=$1 AND week=$2`, s.tenant, week).Scan(&png)
if errors.Is(err, pgx.ErrNoRows) || len(png) == 0 {
return nil, nil
}
if err != nil {
return nil, ErrUnavailable
}
return png, nil
}
// TestSend mails a saved weekly body right now so an operator can verify the mailbox
// before any scheduled run. It never writes a send receipt and never marks a week as sent.
func (s *Store) TestSend(ctx context.Context, week string) (string, error) {
if !s.valid() || strings.TrimSpace(week) == "" {
return "", ErrInvalid
}
settings, err := s.ReadSettings(ctx)
if err != nil {
return "", err
}
if settings.Host == "" || settings.From == "" || len(settings.Recipients) == 0 {
return "", ErrInvalid
}
password, err := s.password(ctx, settings.Revision)
if err != nil {
return "", err
}
var markdown string
if err := s.pool.QueryRow(ctx, `SELECT markdown FROM qa_ops_weekly_drafts WHERE tenant_key=$1 AND week=$2`, s.tenant, week).Scan(&markdown); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return "", ErrInvalid
}
return "", ErrUnavailable
}
if strings.TrimSpace(markdown) == "" {
return "", ErrInvalid
}
html, text := renderReport(markdown)
images, err := s.inlineImages(ctx, week, &html)
if err != nil {
return "", err
}
// A review copy goes only to the operator's own review address so testing never
// reaches colleagues who are on the real distribution list.
target := settings.Recipients
if settings.Review != "" {
target = []string{settings.Review}
}
return s.mailer.Send(ctx, SMTPConfig{Host: settings.Host, Port: settings.Port, Username: settings.Username, Password: password},
Message{To: target, Cc: nil, From: settings.From, Subject: fmt.Sprintf("知识库周报 %s(测试发送)", week), HTML: html, Text: text, Images: images})
}
func (s *Store) send(ctx context.Context, settings Settings, week string, revision int64, markdown string, now time.Time) (DueResult, error) {
password, err := s.password(ctx, settings.Revision)
if err != nil {
return DueResult{State: "blocked", Week: week, Reason: "SMTP 密码未配置或无法解密"}, nil
}
html, text := renderReport(markdown)
images, err := s.inlineImages(ctx, week, &html)
if err != nil {
return DueResult{}, err
}
subject := fmt.Sprintf("知识库周报 %s", week)
var sendID string
err = s.pool.QueryRow(ctx, `INSERT INTO qa_weekly_email_sends(tenant_key,week,draft_revision,body_sha256,status,attempts,created_at,updated_at)
VALUES($1,$2,$3,$4,'sending',1,now(),now())
ON CONFLICT (tenant_key,week,draft_revision) DO UPDATE SET status='sending',attempts=qa_weekly_email_sends.attempts+1,updated_at=now()
WHERE qa_weekly_email_sends.status<>'sent'
RETURNING id::text`, s.tenant, week, revision, bodyHash(markdown)).Scan(&sendID)
if err != nil {
return DueResult{}, ErrUnavailable
}
messageID, sendErr := s.mailer.Send(ctx, SMTPConfig{Host: settings.Host, Port: settings.Port, Username: settings.Username, Password: password},
Message{To: settings.Recipients, Cc: settings.Cc, From: settings.From, Subject: subject, HTML: html, Text: text, Images: images})
if sendErr != nil {
reason := sendErr.Error()
if len(reason) > 400 {
reason = reason[:400]
}
_, _ = s.pool.Exec(ctx, `UPDATE qa_weekly_email_sends SET status='failed',last_error=$2,updated_at=now() WHERE id=$1`, sendID, reason)
return DueResult{State: "failed", Week: week, Reason: "发送失败,稍后重试"}, nil
}
if _, err = s.pool.Exec(ctx, `UPDATE qa_weekly_email_sends SET status='sent',message_id=$2,last_error='',updated_at=now() WHERE id=$1`, sendID, messageID); err != nil {
return DueResult{}, ErrUnavailable
}
return DueResult{State: "sent", Week: week, SentAt: now}, nil
}
const dashboardImageCID = "weekly-dashboard@knowledge-governance"
// inlineImages appends the confirmed dashboard chart to the end of the HTML body.
func (s *Store) inlineImages(ctx context.Context, week string, html *string) ([]InlineImage, error) {
png, err := s.dashboardImage(ctx, week)
if err != nil || len(png) == 0 {
return nil, err
}
footer := `<h2 style="font-size:17px;margin-top:24px;color:#17324d">统计看板</h2>` +
`<p><img src="cid:` + dashboardImageCID + `" alt="问题统计看板" style="max-width:100%;height:auto;border:1px solid #e5e7eb;border-radius:8px"></p>`
*html = strings.Replace(*html, "</body>", footer+"</body>", 1)
return []InlineImage{{Name: "dashboard.png", ContentType: "image/png", CID: dashboardImageCID, Bytes: png}}, nil
}
func (s *Store) password(ctx context.Context, revision int64) (string, error) {
var sealed []byte
var sealedAt int64
if err := s.pool.QueryRow(ctx, `SELECT smtp_password_sealed,password_revision FROM qa_weekly_email_settings WHERE tenant_key=$1`, s.tenant).Scan(&sealed, &sealedAt); err != nil {
return "", ErrUnavailable
}
if sealedAt <= 0 {
sealedAt = revision
}
return s.unseal(sealed, sealedAt)
}
func renderReport(markdown string) (string, string) {
converter := goldmark.New(goldmark.WithExtensions(extension.GFM))
var html strings.Builder
if err := converter.Convert([]byte(markdown), &html); err != nil {
html.Reset()
}
return `<!doctype html><html><head><meta charset="utf-8"><style>
body{font-family:-apple-system,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;color:#1f2937;line-height:1.7;margin:0;padding:24px;background:#fff}
h1{font-size:22px;border-bottom:2px solid #e5e7eb;padding-bottom:8px;margin-top:32px}
h2{font-size:17px;margin-top:24px;color:#17324d}
table{border-collapse:collapse}h1:first-child{margin-top:0}
</style></head><body>` + html.String() + `</body></html>`, markdown
}
// SMTPMailer speaks SMTP directly: implicit TLS on 465, STARTTLS elsewhere. Plaintext is refused.
type SMTPMailer struct{}
func (SMTPMailer) Send(ctx context.Context, cfg SMTPConfig, msg Message) (string, error) {
if cfg.Host == "" || cfg.Port <= 0 || len(msg.To) == 0 || !validAddress(msg.From) {
return "", ErrInvalid
}
address := net.JoinHostPort(cfg.Host, fmt.Sprint(cfg.Port))
dialer := &net.Dialer{Timeout: 15 * time.Second}
var conn net.Conn
var err error
if cfg.Port == 465 {
conn, err = tls.DialWithDialer(dialer, "tcp", address, &tls.Config{ServerName: cfg.Host, MinVersion: tls.VersionTLS12})
} else {
conn, err = dialer.DialContext(ctx, "tcp", address)
}
if err != nil {
return "", fmt.Errorf("smtp 连接失败: %w", err)
}
client, err := smtp.NewClient(conn, cfg.Host)
if err != nil {
conn.Close()
return "", fmt.Errorf("smtp 握手失败: %w", err)
}
defer client.Close()
if cfg.Port != 465 {
if ok, _ := client.Extension("STARTTLS"); !ok {
return "", errors.New("smtp 服务器不支持 STARTTLS,已拒绝明文发送")
}
if err := client.StartTLS(&tls.Config{ServerName: cfg.Host, MinVersion: tls.VersionTLS12}); err != nil {
return "", fmt.Errorf("smtp STARTTLS 失败: %w", err)
}
}
if cfg.Username != "" {
if err := client.Auth(smtp.PlainAuth("", cfg.Username, cfg.Password, cfg.Host)); err != nil {
return "", fmt.Errorf("smtp 登录失败: %w", err)
}
}
if err := client.Mail(msg.From); err != nil {
return "", fmt.Errorf("smtp MAIL FROM 被拒: %w", err)
}
for _, to := range append(append([]string{}, msg.To...), msg.Cc...) {
if err := client.Rcpt(to); err != nil {
return "", fmt.Errorf("smtp RCPT 被拒: %w", err)
}
}
writer, err := client.Data()
if err != nil {
return "", fmt.Errorf("smtp DATA 被拒: %w", err)
}
messageID := fmt.Sprintf("<%s@%s>", hex.EncodeToString(randomBytes(12)), domainOf(msg.From))
boundary := hex.EncodeToString(randomBytes(10))
if _, err = writer.Write([]byte(buildMIME(msg, messageID, boundary))); err != nil {
return "", fmt.Errorf("smtp 写入失败: %w", err)
}
if err = writer.Close(); err != nil {
return "", fmt.Errorf("smtp 提交失败: %w", err)
}
_ = client.Quit()
return messageID, nil
}
func randomBytes(n int) []byte {
out := make([]byte, n)
_, _ = rand.Read(out)
return out
}
func domainOf(address string) string {
if at := strings.LastIndex(address, "@"); at >= 0 {
return address[at+1:]
}
return "localhost"
}
func buildMIME(msg Message, messageID, boundary string) string {
var body strings.Builder
body.WriteString("From: " + msg.From + "\r\n")
body.WriteString("To: " + strings.Join(msg.To, ", ") + "\r\n")
if len(msg.Cc) > 0 {
body.WriteString("Cc: " + strings.Join(msg.Cc, ", ") + "\r\n")
}
body.WriteString("Subject: " + mimeSubject(msg.Subject) + "\r\n")
body.WriteString("Date: " + time.Now().Format(time.RFC1123Z) + "\r\n")
body.WriteString("Message-ID: " + messageID + "\r\n")
body.WriteString("MIME-Version: 1.0\r\n")
alternative := func(w *strings.Builder) {
w.WriteString("--" + boundary + "\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: base64\r\n\r\n")
w.WriteString(base64Lines(msg.Text))
w.WriteString("\r\n--" + boundary + "\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Transfer-Encoding: base64\r\n\r\n")
w.WriteString(base64Lines(msg.HTML))
w.WriteString("\r\n--" + boundary + "--\r\n")
}
if len(msg.Images) == 0 {
body.WriteString("Content-Type: multipart/alternative; boundary=\"" + boundary + "\"\r\n\r\n")
alternative(&body)
return body.String()
}
// Images live in a multipart/related wrapper so "cid:" references render inline,
// after the text and HTML alternatives.
related := boundary + "-related"
body.WriteString("Content-Type: multipart/related; boundary=\"" + related + "\"; type=\"multipart/alternative\"\r\n\r\n")
body.WriteString("--" + related + "\r\nContent-Type: multipart/alternative; boundary=\"" + boundary + "\"\r\n\r\n")
alternative(&body)
for _, image := range msg.Images {
body.WriteString("--" + related + "\r\nContent-Type: " + image.ContentType + "; name=\"" + image.Name + "\"\r\n")
body.WriteString("Content-Transfer-Encoding: base64\r\nContent-ID: <" + image.CID + ">\r\n")
body.WriteString("Content-Disposition: inline; filename=\"" + image.Name + "\"\r\n\r\n")
body.WriteString(base64Lines(string(image.Bytes)))
body.WriteString("\r\n")
}
body.WriteString("--" + related + "--\r\n")
return body.String()
}
func base64Std(value string) string {
return base64.StdEncoding.EncodeToString([]byte(value))
}
func base64Lines(value string) string {
encoded := base64Std(value)
var out strings.Builder
for len(encoded) > 76 {
out.WriteString(encoded[:76] + "\r\n")
encoded = encoded[76:]
}
out.WriteString(encoded)
return out.String()
}
func mimeSubject(value string) string {
return "=?UTF-8?B?" + base64Std(value) + "?="
}