Nodemailer for Go

github.com/malcolmston/nodemailer

Nodemailer-style email composition and SMTP sending for Go.

 GitHubports nodemailer/nodemailer

A from-scratch, standard-library-only Go take on Node.js's Nodemailer: compose rich MIME email with a fluent builder and deliver it through pluggable transports, with no cgo and no third-party dependencies. A Message assembles From/To/Cc/Bcc/Reply-To, a subject, plain-text and HTML bodies, custom headers, file attachments and inline (CID) images; addresses are parsed and validated through net/mail. Build emits correct MIME — multipart/alternative for text+html, wrapped in multipart/related for inline resources and multipart/mixed for attachments, with quoted-printable bodies, base64 attachments, RFC 2047 encoded words, CRLF endings and header folding. A Transport interface carries the bytes: SMTPTransport speaks PLAIN auth over STARTTLS or implicit TLS, while MemoryTransport and JSONTransport capture messages for tests. Set the Date, Message-ID and boundary explicitly for byte-for-byte deterministic output.

Install

shell
$ go get github.com/malcolmston/nodemailer

Quick start

main.go
import "github.com/malcolmston/nodemailer"

msg := nodemailer.New().
	SetFrom("Ada Lovelace <ada@example.com>").
	AddTo("grace@example.com").
	SetSubject("Progress report").
	SetText("Plain-text version.").
	SetHTML("<p>HTML version</p>")

tr := nodemailer.NewTransporter(&nodemailer.MemoryTransport{})
info, _ := tr.SendMail(msg)
fmt.Println(info.MessageID)

Features

  • Fluent Message builder — New() plus SetFrom, AddTo, AddCc, AddBcc, AddReplyTo, SetSubject, SetText, SetHTML
  • Attachments & inline images — Attach, AttachBytes and Embed (cid: resources) over the Attachment struct
  • Address parsing & validation via ParseAddress / ParseAddressList on net/mail, with deferred errors surfaced by Err
  • Automatic MIME structure from Build — single part, multipart/alternative, wrapped in multipart/related for inline and multipart/mixed for attachments
  • Correct encoding — quoted-printable bodies, base64 attachments, RFC 2047 encoded words for non-ASCII subjects/names/filenames, CRLF endings and header folding
  • Pluggable Transport interface — Send(from, to, raw) decouples composition from delivery
  • SMTPTransport over net/smtp — PLAIN auth, implicit TLS or STARTTLS, custom TLSConfig and LocalName
  • Test transports — MemoryTransport captures raw messages (Last) and JSONTransport records a JSON serialisation of each send
  • Transporter.SendMail mirrors nodemailer's flow, deriving the Envelope and returning Info with MessageID, Envelope and Raw
  • Deterministic output — SetDate, SetMessageID and SetBoundary make Build byte-for-byte stable for golden tests
  • Zero dependencies — pure Go standard library, no cgo, nothing to audit but the toolchain
Nodemailer