Offizielles Go-SDK für die E-Mail-API von toSend.
- Paket:
github.com/tosend/tosend-go - Voraussetzungen: Go 1.21+
- Repository: tosend/tosend-go
Installation
go get github.com/tosend/tosend-go
Schnellstart
package main
import (
"fmt"
"log"
"github.com/tosend/tosend-go"
)
func main() {
client := tosend.New("tsend_your_api_key")
resp, err := client.Send(&tosend.SendEmailRequest{
From: tosend.Address{Email: "hello@yourdomain.com", Name: "Your App"},
To: []tosend.Address{{Email: "user@example.com"}},
Subject: "Welcome!",
HTML: "<h1>Hello!</h1><p>Thanks for signing up.</p>",
})
if err != nil {
log.Fatal(err)
}
fmt.Println("Message ID:", resp.MessageID)
}
Konfiguration
Einfach
client := tosend.New("tsend_your_api_key")
Mit Optionen
import "time"
client := tosend.New("tsend_your_api_key",
tosend.WithBaseURL("https://api.tosend.com"),
tosend.WithTimeout(60 * time.Second),
)
Eigener HTTP-Client
httpClient := &http.Client{
Timeout: 60 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 10,
},
}
client := tosend.New("tsend_your_api_key",
tosend.WithHTTPClient(httpClient),
)
E-Mail senden
Einfache E-Mail
resp, err := client.Send(&tosend.SendEmailRequest{
From: tosend.Address{
Email: "hello@yourdomain.com",
Name: "Your App",
},
To: []tosend.Address{
{Email: "user@example.com"},
{Email: "another@example.com", Name: "John Doe"},
},
Subject: "Hello!",
HTML: "<h1>Welcome</h1>",
Text: "Welcome", // optional
})
Mit CC, BCC und Reply-To
resp, err := client.Send(&tosend.SendEmailRequest{
From: tosend.Address{Email: "hello@yourdomain.com"},
To: []tosend.Address{{Email: "user@example.com"}},
CC: []tosend.Address{{Email: "cc@example.com"}},
BCC: []tosend.Address{{Email: "bcc@example.com"}},
ReplyTo: &tosend.Address{Email: "support@yourdomain.com", Name: "Support"},
Subject: "Hello",
HTML: "<p>Hello World</p>",
})
Mit Anhängen
import (
"encoding/base64"
"os"
)
content, _ := os.ReadFile("invoice.pdf")
encoded := base64.StdEncoding.EncodeToString(content)
resp, err := client.Send(&tosend.SendEmailRequest{
From: tosend.Address{Email: "hello@yourdomain.com"},
To: []tosend.Address{{Email: "user@example.com"}},
Subject: "Your Invoice",
HTML: "<p>Please find your invoice attached.</p>",
Attachments: []tosend.Attachment{
{
Type: "application/pdf",
Name: "invoice.pdf",
Content: encoded,
},
},
})
Sammelversand
Mehrere E-Mails in einer Anfrage senden:
resp, err := client.Batch([]tosend.SendEmailRequest{
{
From: tosend.Address{Email: "hello@yourdomain.com"},
To: []tosend.Address{{Email: "user1@example.com"}},
Subject: "Hello User 1",
HTML: "<p>Welcome!</p>",
},
{
From: tosend.Address{Email: "hello@yourdomain.com"},
To: []tosend.Address{{Email: "user2@example.com"}},
Subject: "Hello User 2",
HTML: "<p>Welcome!</p>",
},
})
for _, result := range resp.Results {
if result.Status == "success" {
fmt.Println("Sent:", result.MessageID)
} else {
fmt.Println("Failed:", result.Message)
}
}
Kontodaten
info, err := client.GetAccountInfo()
if err != nil {
log.Fatal(err)
}
fmt.Println("Account:", info.Account.ID, info.Account.Status)
if info.Account.CreditBalance != nil { // nil = no credit limit
fmt.Println("Credits:", *info.Account.CreditBalance)
}
fmt.Println("Rate limit:", info.Account.LimitPerSecond, "per second")
for _, domain := range info.Domains {
fmt.Printf("%s: %s\n", domain.DomainName, domain.VerificationStatus)
}
Admin-API
tosend.NewAdmin liefert einen eigenen Client für die Admin-API. Er erwartet einen Admin-Schlüssel (tsend_admin_...) und gibt bei einem Versandschlüssel einen Fehler zurück. Jeder Aufruf braucht den passenden Scope am Schlüssel. Fehlt ein Scope, kommt ein *tosend.Error zurück, dessen Code() "insufficient_scope" ist.
admin, err := tosend.NewAdmin(os.Getenv("TOSEND_ADMIN_KEY"))
if err != nil {
log.Fatal(err)
}
// Act on one of your client teams (sends X-Account-Id)
client := admin.ForAccount(4203)
domain, err := client.CreateDomain("client.example.com")
detail, err := client.GetDomain(domain.ID)
fmt.Println(detail.DNSRecords.DKIM.Content) // show these records to your client
// Needs api_keys:write and emails:send
key, err := client.CreateSendingKey("Client: Acme Corp", "")
fmt.Println(key.APIKey) // returned once, store it now
// Needs emails:send
_, err = client.Send(&tosend.SendEmailRequest{
From: tosend.Address{Email: "hello@client.example.com"},
To: []tosend.Address{{Email: "user@example.com"}},
Subject: "Hi",
HTML: "<p>Hello</p>",
})
Außerdem verfügbar: GetInfo, ListDomains, DeleteDomain, ListEmails, GetEmail, ListSuppressions, DeleteSuppression, ListWebhooks, CreateWebhook, UpdateWebhook, DeleteWebhook, ListSendingKeys, DeleteSendingKey, ListTeams, CreateTeam, UpdateTeam und Batch. Listenmethoden nehmen Parameter für die Seiten und liefern ein *tosend.Page[T] mit Data, Total, Page und PerPage.
Fehlerbehandlung
resp, err := client.Send(&tosend.SendEmailRequest{
From: tosend.Address{Email: "hello@yourdomain.com"},
To: []tosend.Address{{Email: "user@example.com"}},
Subject: "Hello",
HTML: "<p>Hello</p>",
})
if err != nil {
if apiErr, ok := err.(*tosend.Error); ok {
fmt.Println("Error:", apiErr.Message)
fmt.Println("Status:", apiErr.StatusCode)
fmt.Println("Type:", apiErr.ErrorType) // often empty; branch on StatusCode
fmt.Println("Details:", apiErr.Errors) // map[string]any
fmt.Println("Code:", apiErr.Code()) // e.g. "insufficient_scope"
} else {
fmt.Println("Error:", err)
}
return
}
fmt.Println("Sent:", resp.MessageID)