Skip to content

jetemail/jetemail-go

Repository files navigation

JetEmail Go SDK

The official Go SDK for the JetEmail transactional email API.

CI

Installation

go get github.com/jetemail/jetemail-go

Sending an Email

package 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)
}

Sending with Plain Text

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.",
})

Attachments

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

CC, BCC, Reply-To, and Custom Headers

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

Batch Emails

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)

Webhook Verification

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

Custom Tolerance

err := jetemail.VerifyWebhookWithTolerance(params, 10*time.Minute)

Webhook Event Types

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

Error Handling

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

Custom HTTP Client

httpClient := &http.Client{
	Timeout: 60 * time.Second,
}
client := jetemail.NewCustomClient(httpClient, "your-api-key")

License

MIT

About

JetEmail Go SDK

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages