Skip to main content

Send an email

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

type SendEmailRequest struct {
	From    string   `json:"from"`
	To      []string `json:"to"`
	Subject string   `json:"subject"`
	HTML    string   `json:"html,omitempty"`
	Text    string   `json:"text,omitempty"`
}

type SendEmailResponse struct {
	ID string `json:"id"`
}

func main() {
	payload := SendEmailRequest{
		From:    "you@yourdomain.com",
		To:      []string{"user@example.com"},
		Subject: "Hello from Sendi",
		HTML:    "<p>It just works.</p>",
	}

	body, _ := json.Marshal(payload)
	req, _ := http.NewRequest("POST", "https://app.usesendi.com/api/emails", bytes.NewBuffer(body))
	req.Header.Set("Authorization", "Bearer snd_your_api_key")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	respBody, _ := io.ReadAll(resp.Body)
	var result SendEmailResponse
	json.Unmarshal(respBody, &result)

	fmt.Println("Sent:", result.ID)
}