> For the complete documentation index, see [llms.txt](https://docs.mailtrap.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mailtrap.io/guides/sdk/go-integration.md).

# Go

Integrate Mailtrap with Go apps and projects for email sending using SDK, SMTP, or RESTful API

<a href="https://github.com/mailtrap/mailtrap-go" class="button primary">Mailtrap Go SDK on GitHub</a>

### Overview

Mailtrap can be integrated with Go apps and projects for email sending.

### Email API/SMTP for Go

#### SDK integration

The [Mailtrap Go SDK](https://github.com/mailtrap/mailtrap-go) provides an idiomatic Go interface for the full Mailtrap feature set, with `context.Context` support and typed errors on every call. The SDK supports:

* Transactional, bulk & batch email sending
* Email template management
* Sending domain management
* Suppression list management
* Webhook management
* Email logs and sending stats
* Sandbox email sending, projects, inboxes & message inspection
* Contact management (contacts, lists, fields, imports & exports)
* Account & organization management

### Installation

Install the SDK:

{% code title="Terminal" %}

```bash
go get github.com/mailtrap/mailtrap-go
```

{% endcode %}

Requires Go 1.23 or newer.

### Minimal Example

Here's a minimal example to send your first email:

{% code title="main.go" %}

```go
package main

import (
	"context"
	"fmt"
	"log"

	mailtrap "github.com/mailtrap/mailtrap-go"
)

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

	resp, _, err := client.Send(context.Background(), &mailtrap.SendRequest{
		From:    mailtrap.Address{Email: "hello@example.com", Name: "Mailtrap Test"},
		To:      []mailtrap.Address{{Email: "recipient@example.com"}},
		Subject: "Hello from Mailtrap!",
		Text:    "Welcome to Mailtrap Email Sending!",
		HTML:    "<p>Welcome to <strong>Mailtrap</strong> Email Sending!</p>",
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.MessageIDs)
}
```

{% endcode %}

{% hint style="info" %}
Get your API token from your Mailtrap account under **Settings → API Tokens**.
{% endhint %}

#### SMTP integration

To integrate SMTP with your Go app, navigate to the **Integrations** tab and copy-paste the credentials.

{% hint style="info" %}
SMTP integration is compatible with any Go framework or library that sends emails via SMTP.
{% endhint %}

<div align="left" data-with-frame="true"><img src="https://365478608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgkNigAKiqQtQub1GOdjY%2Fuploads%2Fgit-blob-f2ed463c71849e9c2110dc1dd3f4e442c53b193f%2Fmailtrap-go-smtp-integration.png?alt=media" alt="" width="563"></div>

The standard library's `net/smtp` package is all you need — no third-party dependencies:

{% code title="main.go" %}

```go
package main

import (
	"log"
	"net/smtp"
)

func main() {
	// Copy host, port, username, and password from the Integrations tab.
	host := "live.smtp.mailtrap.io"
	auth := smtp.PlainAuth("", "api", "your-api-token", host)

	msg := []byte("From: hello@example.com\r\n" +
		"To: recipient@example.com\r\n" +
		"Subject: Hello from Mailtrap!\r\n" +
		"\r\n" +
		"Welcome to Mailtrap Email Sending!\r\n")

	err := smtp.SendMail(host+":587", auth, "hello@example.com",
		[]string{"recipient@example.com"}, msg)
	if err != nil {
		log.Fatal(err)
	}
}
```

{% endcode %}

Read more in the [SMTP integration guide](/email-api-smtp/setup/smtp-integration.md).

#### RESTful API integration

To integrate Mailtrap using RESTful API, use the configuration available among **Code samples** under the API section.

API integration can be used with any Go framework or library that supports HTTP requests. For more details, refer to the [API documentation](https://api-docs.mailtrap.io/docs/mailtrap-api-docs/5tjdeg9545058-mailtrap-api).

<div align="left" data-with-frame="true"><img src="https://365478608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgkNigAKiqQtQub1GOdjY%2Fuploads%2Fgit-blob-87e872c3b3c53bce2a45b83b951440f0cd1d62ee%2Fmailtrap-go-api-integration.png?alt=media" alt="" width="563"></div>

Read more about API integration [here](/email-api-smtp/setup/api-integration.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mailtrap.io/guides/sdk/go-integration.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
