Skip to content

mailtrap/mailtrap-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mailtrap Go client

CI Go Reference License: MIT

The official Go client library for the Mailtrap email delivery platform — transactional and bulk sending, email sandbox (testing), and email marketing.

Prerequisites

To get the most out of this Mailtrap.io Go SDK:

Installation

go get github.com/mailtrap/mailtrap-go

Requires Go 1.23 or newer.

Usage

Create a client with your API token:

client, err := mailtrap.NewClient("your-api-token")
if err != nil {
	log.Fatal(err)
}

Sandbox vs Production (easy switching)

Mailtrap lets you test safely in the Email Sandbox and then switch to Production sending with a single flag. In sandbox mode Send captures the email in a sandbox instead of delivering it to real recipients.

Keep the sandbox ID configured and toggle WithSandbox from configuration — the ID is ignored outside sandbox mode, so the exact same code switches environments without touching any call site:

isSandbox := os.Getenv("MAILTRAP_USE_SANDBOX") == "true"

client, err := mailtrap.NewClient("your-api-token",
	mailtrap.WithSandbox(isSandbox),
	mailtrap.WithSandboxID(3000001), // ignored unless sandbox mode is on
)
if err != nil {
	log.Fatal(err)
}

resp, _, err := client.Send(context.Background(), &mailtrap.SendRequest{
	From:    mailtrap.Address{Email: "sender@example.com", Name: "Example"},
	To:      []mailtrap.Address{{Email: "recipient@example.com"}},
	Subject: "Hello from mailtrap-go",
	Text:    "Captured by the sandbox when MAILTRAP_USE_SANDBOX=true.",
})

Supported functionality & Examples

Email API (sending):

  • Transactional, bulk & batch sending — text/HTML, templates, attachments, categories, custom variables & headers — examples/sending

Email API (management):

Email Sandbox (Testing):

Account & organization management:

Contact management:

Errors

Non-2xx responses decode into typed errors that work with errors.As:

_, _, err := client.Projects.List(ctx)

var ve *mailtrap.ValidationError
if errors.As(err, &ve) {
	fmt.Println(ve.Fields) // field -> messages
}

var rle *mailtrap.RateLimitError
if errors.As(err, &rle) {
	time.Sleep(rle.RetryAfter)
}

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mailtrap/mailtrap-go. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Code of Conduct.

License

This library is released under the MIT License.

About

Official mailtrap.io Go client

Resources

License

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages