Keplers Mail Service

Go

Go integration examples for Keplers Mail Service

Send emails using Go with Keplers Mail Service REST API.

Basic Setup

package main

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

type EmailRequest struct {
    To      []string `json:"to"`
    Subject string   `json:"subject"`
    Body    string   `json:"body"`
    IsHTML  bool     `json:"is_html"`
}

type EmailResponse struct {
    Success bool        `json:"success"`
    Data    interface{} `json:"data"`
    Message string      `json:"message"`
}

Send Email Function

func sendEmail(to, subject, body string, isHTML bool) (*EmailResponse, error) {
    email := EmailRequest{
        To:      []string{to},
        Subject: subject,
        Body:    body,
        IsHTML:  isHTML,
    }
    
    jsonData, err := json.Marshal(email)
    if err != nil {
        return nil, fmt.Errorf("failed to marshal email: %w", err)
    }
    
    req, err := http.NewRequest("POST", "https://api.keplars.com/api/v1/send-email/queue", bytes.NewBuffer(jsonData))
    if err != nil {
        return nil, fmt.Errorf("failed to create request: %w", err)
    }
    
    req.Header.Set("Authorization", "Bearer "+os.Getenv("KEPLERS_API_KEY"))
    req.Header.Set("Content-Type", "application/json")
    
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return nil, fmt.Errorf("failed to send request: %w", err)
    }
    defer resp.Body.Close()
    
    var emailResp EmailResponse
    if err := json.NewDecoder(resp.Body).Decode(&emailResp); err != nil {
        return nil, fmt.Errorf("failed to decode response: %w", err)
    }
    
    return &emailResp, nil
}

func sendInstantEmail(to, subject, body string, isHTML bool) (*EmailResponse, error) {
    email := EmailRequest{
        To:      []string{to},
        Subject: subject,
        Body:    body,
        IsHTML:  isHTML,
    }
    
    jsonData, _ := json.Marshal(email)
    
    req, _ := http.NewRequest("POST", "https://api.keplars.com/api/v1/send-email/instant", bytes.NewBuffer(jsonData))
    req.Header.Set("Authorization", "Bearer "+os.Getenv("KEPLERS_API_KEY"))
    req.Header.Set("Content-Type", "application/json")
    
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    
    var emailResp EmailResponse
    json.NewDecoder(resp.Body).Decode(&emailResp)
    
    return &emailResp, nil
}

Usage Examples

func main() {
    // Send welcome email
    result, err := sendEmail(
        "[email protected]",
        "Welcome!",
        "<h1>Welcome to our platform!</h1>",
        true,
    )
    
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }
    
    fmt.Printf("Email sent: %+v\n", result)
    
    // Send instant verification email
    _, err = sendInstantEmail(
        "[email protected]",
        "Verification Code",
        "Your code: 123456",
        false,
    )
    
    if err != nil {
        fmt.Printf("Error: %v\n", err)
    } else {
        fmt.Println("Verification email sent!")
    }
}

Simple Go integration using standard HTTP client for sending emails.

On this page