mail builds and sends outbound email. It is a small transport and message
builder - not a marketing platform or a template engine. Zero dependencies,
standard library only.
go get github.com/goloop/mailsender := mail.NewSMTP(mail.SMTPConfig{
Host: "smtp.example.com",
From: mail.Address{Name: "App", Email: "no-reply@example.com"},
})
msg := mail.Message{
To: []mail.Address{{Email: "user@example.com"}},
Subject: "Welcome",
Text: "Hello and welcome.",
HTML: "<p>Hello and welcome.</p>",
}
if err := sender.Send(ctx, msg); err != nil {
return err
}For the common case:
mail.Send(ctx, sender, "user@example.com", "Hi", "body")Set Text and/or HTML:
- text only ->
text/plain; charset=utf-8 - HTML only ->
text/html; charset=utf-8 - both ->
multipart/alternative - with
Attachments->multipart/mixed
Addresses are validated, display names are RFC 2047 encoded, text is
quoted-printable and attachments are base64. A current Date, a random Message-ID and
MIME-Version headers are added automatically.
| Constructor | Use |
|---|---|
NewSMTP(SMTPConfig) |
production; STARTTLS (default), implicit TLS, or plain |
NewLogger(io.Writer) |
development; writes the built message |
NewCapture() |
tests; records messages, inspect with Last/Messages |
A worker or handler depends only on the Sender interface, so dev and prod use
the same code path.
mail does one send per call and returns the error. Retries, queues,
templating, bounce handling and vendor APIs are out of scope: they belong to the
surrounding application or its background job layer.
MIT - see LICENSE.
Encode attachments in one buffer and interrupt SMTP setup on cancellation. The v1 release keeps the existing import path and public API. Future incompatible changes require a new major version.