mail

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func LoadConfigFromEnv(logger *log.Logger) Config

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

func New(cfg Config) Mailer

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).

func (*SMTPMailer) Send

func (m *SMTPMailer) Send(ctx context.Context, to, subject, body string) error

Send composes the message with standard headers and delivers it via SMTP. ctx cancellation only affects the wait; the underlying goroutine completes to leave the SMTP connection clean.

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 ────────────────────────────────────────

func (*StderrMailer) Send

func (m *StderrMailer) Send(ctx context.Context, to, subject, body string) error

Send writes the message to the logger. Always returns nil; the stderr fallback can't fail meaningfully (a write failure would be a logger-level concern, not a mail-delivery one).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL