Mailtrap is an email delivery platform for sending, testing, and controlling emails. This service sends transactional emails through the Mailtrap Email Sending API.
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.
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")
}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"),
)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)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(),
)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"),
)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),
)