Skip to content

Latest commit

 

History

History

mailtrap

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

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),
)