Documentation
¶
Overview ¶
Package mail abstracts outbound email sending for chalk. The two implementations are:
- SMTPMailer: real SMTP delivery via net/smtp. Used in production and in dev when a Mailhog (or similar) container is running.
- StderrMailer: writes the message to a logger instead of sending. The fallback for dev when no SMTP host is configured; the dev can copy URLs from chalkd's stderr.
Selection at startup is driven by CHALK_SMTP_HOST. If empty, the stderr fallback is used. Other env vars (CHALK_SMTP_PORT, CHALK_SMTP_FROM) tune the SMTP path.
The Mailer interface is intentionally narrow: a single Send method taking address + subject + plaintext body. HTML email is out of scope for chalk's use cases (invite links, email-change verification links, admin notices) which are all link-driven and don't need rich formatting.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Host is the SMTP server hostname or IP. Empty = stderr fallback.
Host string
// Port is the SMTP port. Defaults to 25 if Host is set and Port
// is 0. For Mailhog use 1025.
Port int
// From is the envelope-from address. Defaults to
// "chalk@<hostname>" if empty.
From string
// FromName is the display name placed before From in the
// Sender header. Optional.
FromName string
// AuthUser / AuthPass: SMTP AUTH credentials. Empty = no auth.
// For Mailhog leave both empty.
AuthUser string
AuthPass string
// Logger is used by both implementations for error reporting
// and (in the stderr fallback) the actual "delivery" output.
// If nil, the package falls back to the stdlib log default.
Logger *log.Logger
}
Config drives Mailer selection at startup. All fields are optional; an empty Host selects the StderrMailer fallback. Construct via LoadConfigFromEnv() rather than hand-rolling.
func LoadConfigFromEnv ¶
LoadConfigFromEnv reads CHALK_SMTP_* env vars into a Config. Defaults: Port=25 when Host is set, From="chalk@<hostname>".
type Mailer ¶
type Mailer interface {
// Send delivers a plaintext message. Returns nil on success.
// Implementations are responsible for adding common headers
// (From, To, Subject, Date, Message-ID, MIME-Version).
Send(ctx context.Context, to, subject, body string) error
}
Mailer is the interface every implementation satisfies. ctx allows the SMTP implementation to honor request timeouts; the stderr implementation ignores it.
func New ¶
New constructs a Mailer from the given Config. If Host is empty, returns a StderrMailer; otherwise returns an SMTPMailer.
Failures during SMTP construction are deferred to Send time so the server starts cleanly even when the mail server is temporarily unavailable (we don't want chalkd to refuse to boot just because Mailhog isn't up yet).
type SMTPMailer ¶
type SMTPMailer struct {
// contains filtered or unexported fields
}
SMTPMailer delivers messages via SMTP. Uses net/smtp from the standard library — minimal feature set (no STARTTLS by default, no DSN, no DKIM signing) but covers what chalk needs.
Behavior:
- PLAIN AUTH if authUser is non-empty.
- No TLS by default (Mailhog doesn't support it on 1025). To enable TLS, future-proof: add a CHALK_SMTP_TLS=1 toggle. Punted to a later phase; chalk dev runs against Mailhog (no TLS) and chalk production hasn't shipped yet.
- Honors ctx via a goroutine-cancel pattern: if ctx is done before Send returns, we return ctx.Err() and the in-flight SMTP exchange continues to completion in the background (rather than leaving a half-spoken protocol on the wire).
type StderrMailer ¶
type StderrMailer struct {
// contains filtered or unexported fields
}
StderrMailer writes outgoing messages to a logger instead of sending them over SMTP. The intended use case is dev/test runs where no SMTP server is configured: the developer can see invite URLs and email-change verification URLs by tailing chalkd's stderr.
The format is intentionally noisy and human-readable, with clear delimiters so URLs are easy to spot:
─── chalk:mail ─── to=user@example subject="..." ────────── From: chalk <chalk@localhost> ... full body ... ──────────── end mail ────────────────────────────────────────