The official Go SDK for the JetEmail transactional email API.
go get github.com/jetemail/jetemail-gopackage main
import (
"fmt"
"log"
"github.com/jetemail/jetemail-go"
)
func main() {
client := jetemail.NewClient("your-api-key")
resp, err := client.Emails.Send(&jetemail.SendEmailRequest{
From: "You <you@yourdomain.com>",
To: []string{"recipient@example.com"},
Subject: "Hello from JetEmail",
Html: "<p>Welcome!</p>",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Email sent: %s\n", resp.Id)
}resp, err := client.Emails.Send(&jetemail.SendEmailRequest{
From: "you@yourdomain.com",
To: []string{"recipient@example.com"},
Subject: "Plain text email",
Text: "Hello, this is a plain text email.",
})resp, err := client.Emails.Send(&jetemail.SendEmailRequest{
From: "you@yourdomain.com",
To: []string{"recipient@example.com"},
Subject: "With attachment",
Html: "<p>See attached.</p>",
Attachments: []jetemail.Attachment{
{
Filename: "report.pdf",
Data: "base64-encoded-content-here",
},
},
})resp, err := client.Emails.Send(&jetemail.SendEmailRequest{
From: "you@yourdomain.com",
To: []string{"recipient@example.com"},
Subject: "Full options",
Html: "<p>Hello</p>",
Cc: []string{"cc@example.com"},
Bcc: []string{"bcc@example.com"},
ReplyTo: []string{"reply@example.com"},
Headers: map[string]string{
"X-Custom-Header": "custom-value",
},
})Send up to 100 emails in a single request:
emails := []*jetemail.SendEmailRequest{
{
From: "you@yourdomain.com",
To: []string{"alice@example.com"},
Subject: "Hello Alice",
Html: "<p>Hi Alice!</p>",
},
{
From: "you@yourdomain.com",
To: []string{"bob@example.com"},
Subject: "Hello Bob",
Text: "Hi Bob!",
},
}
resp, err := client.Batch.Send(emails)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Sent: %d, Failed: %d\n", resp.Summary.Successful, resp.Summary.Failed)Verify incoming webhook signatures to ensure requests are authentic:
import (
"io"
"net/http"
"github.com/jetemail/jetemail-go"
)
func webhookHandler(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
err = jetemail.VerifyWebhook(jetemail.VerifyWebhookParams{
Signature: r.Header.Get("X-Webhook-Signature"),
Timestamp: r.Header.Get("X-Webhook-Timestamp"),
Body: body,
Secret: "your-webhook-secret",
})
if err != nil {
http.Error(w, "Invalid signature", http.StatusUnauthorized)
return
}
// Process the webhook event...
w.WriteHeader(http.StatusOK)
}err := jetemail.VerifyWebhookWithTolerance(params, 10*time.Minute)| Constant | Value |
|---|---|
EventOutboundDelivered |
outbound.delivered |
EventOutboundBounced |
outbound.bounced |
EventOutboundRejected |
outbound.rejected |
EventOutboundDeferred |
outbound.deferred |
EventOutboundSpam |
outbound.spam |
EventOutboundDropped |
outbound.dropped |
EventOutboundVirus |
outbound.virus |
EventOutboundOpened |
outbound.opened |
EventOutboundClicked |
outbound.clicked |
EventOutboundComplaint |
outbound.complaint |
resp, err := client.Emails.Send(params)
if err != nil {
if apiErr, ok := err.(*jetemail.APIError); ok {
fmt.Printf("API error %d: %s\n", apiErr.StatusCode, apiErr.Message)
} else {
fmt.Printf("Error: %s\n", err)
}
}httpClient := &http.Client{
Timeout: 60 * time.Second,
}
client := jetemail.NewCustomClient(httpClient, "your-api-key")MIT