mailtrap

package
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2026 License: MIT Imports: 6 Imported by: 0

README

Mailtrap

go.dev reference

Mailtrap is an email delivery platform for sending, testing, and controlling emails. This service sends transactional emails through the Mailtrap Email Sending API.

Prerequisites

To use the Mailtrap notification service, you will need:

  • A Mailtrap API token — create one under API Tokens.
  • A verified sending domain and a sender email address that belongs to it.

Usage

package main

import (
	"context"
	"log"

	"github.com/nikoksr/notify"
	"github.com/nikoksr/notify/service/mailtrap"
)

func main() {
	mailtrapSvc := mailtrap.New("your-api-token", "sender@your-domain.com")

	mailtrapSvc.AddReceivers("recipient@example.com")

	notifier := notify.New()
	notifier.UseServices(mailtrapSvc)

	if err := notifier.Send(context.Background(), "subject", "message"); err != nil {
		log.Fatalf("notifier.Send() failed: %s", err.Error())
	}

	log.Println("notification sent")
}

Options

Sender name

By default only the sender email address is sent. Use WithSenderName to also set a display name:

mailtrapSvc := mailtrap.New(
	"your-api-token",
	"sender@your-domain.com",
	mailtrap.WithSenderName("Acme Notifications"),
)
Plain-text messages

The message body is sent as HTML by default. Switch to plain text with BodyFormat:

mailtrapSvc := mailtrap.New("your-api-token", "sender@your-domain.com")
mailtrapSvc.BodyFormat(mailtrap.PlainText)
Bulk sending

To route messages through the Mailtrap bulk sending host (intended for high-volume sending), use WithBulkHost:

mailtrapSvc := mailtrap.New(
	"your-api-token",
	"sender@your-domain.com",
	mailtrap.WithBulkHost(),
)
Sandbox (testing)

To capture emails in a Mailtrap test inbox instead of delivering them, use WithSandbox with your inbox ID. The sandbox API requires the inbox ID as part of the request path:

mailtrapSvc := mailtrap.New(
	"your-api-token",
	"sender@your-domain.com",
	mailtrap.WithSandbox("your-inbox-id"),
)
Custom HTTP client

To configure timeouts, proxies, or a custom transport, provide your own HTTP client with WithHTTPClient:

import (
	"net/http"
	"time"

	"github.com/nikoksr/notify/service/mailtrap"
)

httpClient := &http.Client{Timeout: 10 * time.Second}

mailtrapSvc := mailtrap.New(
	"your-api-token",
	"sender@your-domain.com",
	mailtrap.WithHTTPClient(httpClient),
)

Documentation

Overview

Package mailtrap provides a notify.Notifier implementation that sends transactional emails through the Mailtrap Email Sending API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BodyType

type BodyType int

BodyType is used to specify the format of the message body.

const (
	// HTML specifies that the message body is HTML. This is the default.
	HTML BodyType = iota
	// PlainText specifies that the message body is plain text.
	PlainText
)

type Mailtrap

type Mailtrap struct {
	// contains filtered or unexported fields
}

Mailtrap holds the data required to communicate with the Mailtrap Email Sending API.

func New

func New(apiKey, senderAddress string, opts ...Option) *Mailtrap

New returns a new instance of a Mailtrap notification service. You will need a Mailtrap API token, which can be created at https://mailtrap.io/api-tokens. See https://api-docs.mailtrap.io for the full API documentation.

func (*Mailtrap) AddReceivers

func (m *Mailtrap) AddReceivers(addresses ...string)

AddReceivers takes email addresses and adds them to the internal address list. The Send method will send a given message to all those addresses.

func (*Mailtrap) BodyFormat

func (m *Mailtrap) BodyFormat(format BodyType)

BodyFormat can be used to specify the format of the message body. Default BodyType is HTML.

func (Mailtrap) Send

func (m Mailtrap) Send(ctx context.Context, subject, message string) error

Send takes a message subject and a message body and sends them to all previously set receivers. Message body supports html as markup language by default; use BodyFormat(PlainText) to send plain text instead.

type Option

type Option func(*Mailtrap)

Option describes a functional parameter for the Mailtrap constructor.

func WithBulkHost

func WithBulkHost() Option

WithBulkHost routes messages through the Mailtrap bulk sending host, which is intended for high-volume sending.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient sets a custom HTTP client for communicating with the Mailtrap API. This is useful for configuring timeouts, proxies, or custom transports.

func WithHost

func WithHost(host string) Option

WithHost overrides the Mailtrap API host. This is primarily useful for testing or for pointing the client at a custom endpoint.

func WithSandbox

func WithSandbox(inboxID string) Option

WithSandbox routes messages through the Mailtrap sandbox host, capturing emails in the given test inbox instead of delivering them. The sandbox API requires the inbox ID as part of the request path, so it must be provided here. See https://api-docs.mailtrap.io for details.

func WithSenderName

func WithSenderName(name string) Option

WithSenderName sets the display name used for the sender ("from") address. If not set, only the sender email address is sent.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL