Nodemailer for Go
Nodemailer-style email composition and SMTP sending for Go.
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
$ go get github.com/malcolmston/nodemailerQuick start
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
Messagebuilder —New()plusSetFrom,AddTo,AddCc,AddBcc,AddReplyTo,SetSubject,SetText,SetHTML - Attachments & inline images —
Attach,AttachBytesandEmbed(cid: resources) over theAttachmentstruct - Address parsing & validation via
ParseAddress/ParseAddressListonnet/mail, with deferred errors surfaced byErr - Automatic MIME structure from
Build— single part,multipart/alternative, wrapped inmultipart/relatedfor inline andmultipart/mixedfor attachments - Correct encoding — quoted-printable bodies, base64 attachments, RFC 2047 encoded words for non-ASCII subjects/names/filenames, CRLF endings and header folding
- Pluggable
Transportinterface —Send(from, to, raw)decouples composition from delivery SMTPTransportovernet/smtp— PLAIN auth, implicitTLSorSTARTTLS, customTLSConfigandLocalName- Test transports —
MemoryTransportcaptures raw messages (Last) andJSONTransportrecords a JSON serialisation of each send Transporter.SendMailmirrors nodemailer's flow, deriving theEnvelopeand returningInfowithMessageID,EnvelopeandRaw- Deterministic output —
SetDate,SetMessageIDandSetBoundarymakeBuildbyte-for-byte stable for golden tests - Zero dependencies — pure Go standard library, no cgo, nothing to audit but the toolchain