Documentation Index
Fetch the complete documentation index at: https://docs.mailbreeze.com/llms.txt
Use this file to discover all available pages before exploring further.
Installation
go get github.com/MailBreeze/mailbreeze-go
Requirements
- Go 1.21+
Quick Start
package main
import (
"context"
"fmt"
"log"
"os"
mailbreeze "github.com/MailBreeze/mailbreeze-go"
)
func main() {
client := mailbreeze.NewClient(os.Getenv("MAILBREEZE_API_KEY"))
email, err := client.Emails.Send(context.Background(), &mailbreeze.SendEmailParams{
From: "hello@yourdomain.com",
To: []string{"user@example.com"},
Subject: "Welcome!",
HTML: "<h1>Welcome to our platform!</h1>",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Email sent: %s\n", email.ID)
}
Configuration
client := mailbreeze.NewClient(
os.Getenv("MAILBREEZE_API_KEY"),
mailbreeze.WithBaseURL("https://api.mailbreeze.com"),
mailbreeze.WithTimeout(30 * time.Second),
mailbreeze.WithMaxRetries(3),
)
Resources
Emails
ctx := context.Background()
// Send email
email, err := client.Emails.Send(ctx, &mailbreeze.SendEmailParams{
From: "hello@yourdomain.com",
To: []string{"user@example.com"},
Subject: "Hello",
HTML: "<p>Hello!</p>",
})
// Send with template
email, err := client.Emails.Send(ctx, &mailbreeze.SendEmailParams{
From: "hello@yourdomain.com",
To: []string{"user@example.com"},
TemplateID: "welcome-template",
Variables: map[string]any{"name": "John"},
})
// List emails
result, err := client.Emails.List(ctx, &mailbreeze.ListEmailsParams{
Status: "delivered",
Page: 1,
Limit: 20,
})
for _, email := range result.Data {
fmt.Println(email.Subject)
}
// Get email
email, err := client.Emails.Get(ctx, "msg_xxx")
// Get stats
stats, err := client.Emails.Stats(ctx)
fmt.Printf("Total: %d, Sent: %d, Success rate: %.1f%%\n", stats.Total, stats.Sent, stats.SuccessRate)
Attachments
// Create upload URL
upload, err := client.Attachments.CreateUpload(ctx, &mailbreeze.CreateAttachmentUploadParams{
FileName: "report.pdf",
ContentType: "application/pdf",
FileSize: 1024000,
})
// Upload file
fileData, _ := os.ReadFile("report.pdf")
req, _ := http.NewRequest("PUT", upload.UploadURL, bytes.NewReader(fileData))
req.Header.Set("Content-Type", "application/pdf")
http.DefaultClient.Do(req)
// Confirm upload
attachment, err := client.Attachments.Confirm(ctx, &mailbreeze.ConfirmAttachmentParams{
UploadToken: upload.UploadToken,
})
// Use in email
client.Emails.Send(ctx, &mailbreeze.SendEmailParams{
AttachmentIds: []string{upload.AttachmentID},
// ... other params
})
Lists
// Create list
list, err := client.Lists.Create(ctx, &mailbreeze.CreateListParams{
Name: "Newsletter",
CustomFields: []mailbreeze.CustomFieldDefinition{
{Key: "company", Label: "Company", Type: "text"},
},
})
// List all
result, err := client.Lists.List(ctx, nil)
// Get, update, delete
list, err := client.Lists.Get(ctx, "lst_xxx")
list, err = client.Lists.Update(ctx, "lst_xxx", &mailbreeze.UpdateListParams{
Name: "New Name",
})
err = client.Lists.Delete(ctx, "lst_xxx")
// Get stats
stats, err := client.Lists.Stats(ctx, "lst_xxx")
Contacts
// Get contacts for a list
contacts := client.Contacts("lst_xxx")
// Create contact
contact, err := contacts.Create(ctx, &mailbreeze.CreateContactParams{
Email: "user@example.com",
FirstName: "John",
CustomFields: map[string]any{"company": "Acme"},
})
// List, get, update, delete
result, err := contacts.List(ctx, &mailbreeze.ListContactsParams{
Status: "active",
})
contact, err := contacts.Get(ctx, "cnt_xxx")
contact, err = contacts.Update(ctx, "cnt_xxx", &mailbreeze.UpdateContactParams{
FirstName: "Jane",
})
err = contacts.Delete(ctx, "cnt_xxx")
// Suppress contact
err = contacts.Suppress(ctx, "cnt_xxx", "bounced")
Verification
// Verify single email
result, err := client.Verification.Verify(ctx, &mailbreeze.VerifyEmailParams{
Email: "user@example.com",
})
if err != nil {
log.Fatal(err)
}
if result.IsValid {
fmt.Println("Safe to send")
}
// Batch verify
batch, err := client.Verification.Batch(ctx, &mailbreeze.BatchVerifyParams{
Emails: []string{"user1@example.com", "user2@example.com"},
})
// Poll for results
status, err := client.Verification.Get(ctx, batch.VerificationID)
// List and stats
result, err := client.Verification.List(ctx, nil)
stats, err := client.Verification.Stats(ctx)
Error Handling
import "errors"
_, err := client.Emails.Send(ctx, params)
if err != nil {
var mbErr *mailbreeze.Error
if errors.As(err, &mbErr) {
fmt.Println(mbErr.Code) // e.g., "VALIDATION_ERROR"
fmt.Println(mbErr.Message) // Human-readable message
fmt.Println(mbErr.Status) // HTTP status code
// Handle specific errors
switch mbErr.Code {
case "DNS_VERIFICATION_FAILED":
// Domain not verified
case "RATE_LIMIT_EXCEEDED":
// Retry after delay
}
}
}
Context Support
All methods accept acontext.Context for cancellation and timeouts:
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
email, err := client.Emails.Send(ctx, params)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
// Handle timeout
}
}
Features
- Idiomatic Go - Context support, error handling with
errors.Is/As - Type-safe - Struct types for all params and responses
- Automatic retries - Exponential backoff for failures
- API key redaction - Keys redacted in debug output
- 95%+ test coverage
Links
pkg.go.dev
Documentation
GitHub
Source code