mailer

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

CLAUDE:SUMMARY Outbox SMTP via Resend (HTTP port 443) — Enqueue, SendNow, RunWorker avec backoff exponentiel.

CLAUDE:SUMMARY Backend SMTPS (port 465 implicit TLS) — utilisé pour OVH Email Pro.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mailer

type Mailer struct {
	DB       *sql.DB
	APIKey   string // Resend API key (fallback si SMTPHost vide)
	SMTPHost string // ex "ssl0.ovh.net" — si défini, backend SMTPS port 465
	SMTPPort int    // 0 → default 465
	SMTPUser string // ex "contact@assokit.org"
	SMTPPass string
	From     string // ex "contact@assokit.org"
	AdminTo  string
	Logger   *slog.Logger
}

Mailer gère l'envoi d'emails. Deux backends supportés :

  • SMTPS (ex OVH Email Pro, port 465 implicit TLS) si SMTPHost est défini
  • Resend (HTTP /emails) si APIKey est défini

Si les deux sont vides → mailer désactivé, outbox accumule en status=pending, drainé au prochain restart avec config valide. Si les deux définis → SMTPS prime.

func (*Mailer) ApplyBackoff

func (m *Mailer) ApplyBackoff(ctx context.Context, msg OutboxMsg, errMsg string)

ApplyBackoff applique le backoff exponentiel 30/60/120/240/480s sur un envoi échoué. Exporté pour permettre les tests unitaires de la logique de retry.

func (*Mailer) Enqueue

func (m *Mailer) Enqueue(ctx context.Context, to, subject, bodyText, bodyHTML string) error

Enqueue insère un email en status=pending dans email_outbox.

func (*Mailer) RunWorker

func (m *Mailer) RunWorker(ctx context.Context)

RunWorker démarre la boucle de drain de l'outbox. Ticker 30s.

func (*Mailer) SendNow

func (m *Mailer) SendNow(ctx context.Context, msg OutboxMsg) error

SendNow tente d'envoyer immédiatement. Backend SMTPS si SMTPHost défini, sinon Resend si APIKey défini. Si rien, no-op (reste pending). Backoff expo appliqué sur erreur transitoire.

type OutboxMsg

type OutboxMsg struct {
	ID         string
	ToAddr     string
	Subject    string
	BodyText   string
	BodyHTML   string
	Attempts   int
	RetryAfter sql.NullString
}

OutboxMsg représente un message en attente dans email_outbox.