Documentation
¶
Overview ¶
Package sendamatic provides a Go client library for the Sendamatic email delivery API.
The library offers a simple and idiomatic Go API with context support, a fluent message builder interface, and comprehensive error handling for sending transactional emails.
Example usage:
client := sendamatic.NewClient("your-user-id", "your-password")
msg := sendamatic.NewMessage().
SetSender("[email protected]").
AddTo("[email protected]").
SetSubject("Hello").
SetTextBody("Hello World")
resp, err := client.Send(context.Background(), msg)
Index ¶
- type APIError
- type Attachment
- type Client
- type Header
- type Message
- func (m *Message) AddBCC(email string) *Message
- func (m *Message) AddCC(email string) *Message
- func (m *Message) AddHeader(name, value string) *Message
- func (m *Message) AddTo(email string) *Message
- func (m *Message) AttachFile(filename, mimeType string, data []byte) *Message
- func (m *Message) AttachFileFromPath(path, mimeType string) error
- func (m *Message) SetHTMLBody(body string) *Message
- func (m *Message) SetSender(email string) *Message
- func (m *Message) SetSubject(subject string) *Message
- func (m *Message) SetTextBody(body string) *Message
- func (m *Message) Validate() error
- type Option
- type SendResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIError ¶
type APIError struct {
StatusCode int `json:"-"`
Message string `json:"error"`
ValidationErrors string `json:"validation_errors,omitempty"`
JSONPath string `json:"json_path,omitempty"`
Sender string `json:"sender,omitempty"`
SMTPCode int `json:"smtp_code,omitempty"`
}
APIError represents an error response from the Sendamatic API. It includes the HTTP status code, error message, and optional additional context such as validation errors, JSON path information, and SMTP codes.
type Attachment ¶
type Attachment struct {
Filename string `json:"filename"`
Data string `json:"data"` // Base64-encoded file content
MimeType string `json:"mimetype"`
}
Attachment represents an email attachment with its filename, MIME type, and base64-encoded data.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a Sendamatic API client that handles authentication and HTTP communication with the Sendamatic email delivery service.
func NewClient ¶
NewClient creates and returns a new Client configured with the provided Sendamatic credentials. The userID and password are combined to form the API key used for authentication. Optional configuration functions can be provided to customize the client behavior.
Example:
client := sendamatic.NewClient("user-id", "password",
sendamatic.WithTimeout(60*time.Second))
func (*Client) Send ¶
Send sends an email message through the Sendamatic API using the provided context. The message is validated before sending. If validation fails or the API request fails, an error is returned. On success, a SendResponse containing per-recipient delivery information is returned.
The context can be used to set deadlines, timeouts, or cancel the request.
type Message ¶
type Message struct {
To []string `json:"to"`
CC []string `json:"cc,omitempty"`
BCC []string `json:"bcc,omitempty"`
Sender string `json:"sender"`
Subject string `json:"subject"`
TextBody string `json:"text_body,omitempty"`
HTMLBody string `json:"html_body,omitempty"`
Headers []Header `json:"headers,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}
Message represents an email message with all its components including recipients, content, headers, and attachments. Messages are constructed using the fluent builder pattern provided by the setter methods.
func NewMessage ¶
func NewMessage() *Message
NewMessage creates and returns a new empty Message with initialized slices for recipients, headers, and attachments. Use the setter methods to populate the message fields.
func (*Message) AddBCC ¶
AddBCC adds a recipient email address to the BCC (blind carbon copy) field. Returns the message for method chaining.
func (*Message) AddCC ¶
AddCC adds a recipient email address to the CC (carbon copy) field. Returns the message for method chaining.
func (*Message) AddHeader ¶
AddHeader adds a custom email header with the specified name and value. Common examples include "Reply-To", "X-Priority", or custom application headers. Returns the message for method chaining.
func (*Message) AddTo ¶
AddTo adds a recipient email address to the To field. Returns the message for method chaining.
func (*Message) AttachFile ¶
AttachFile adds a file attachment to the message from a byte slice. The data is automatically base64-encoded for transmission. Returns the message for method chaining.
func (*Message) AttachFileFromPath ¶
AttachFileFromPath reads a file from the filesystem and adds it as an attachment. The filename is extracted from the path. Returns an error if the file cannot be read. The file data is automatically base64-encoded for transmission.
func (*Message) SetHTMLBody ¶
SetHTMLBody sets the HTML body of the email. Returns the message for method chaining.
func (*Message) SetSender ¶
SetSender sets the sender email address for the message. Returns the message for method chaining.
func (*Message) SetSubject ¶
SetSubject sets the email subject line. Returns the message for method chaining.
func (*Message) SetTextBody ¶
SetTextBody sets the plain text body of the email. Returns the message for method chaining.
func (*Message) Validate ¶
Validate checks whether the message meets all required criteria for sending. It returns an error if any validation rules are violated:
- At least one recipient is required
- Maximum of 255 recipients allowed
- Sender must be specified
- Subject must be specified
- Either TextBody or HTMLBody (or both) must be provided
type Option ¶
type Option func(*Client)
Option is a function type that modifies a Client during initialization. Options follow the functional options pattern for configuring client behavior.
func WithBaseURL ¶
WithBaseURL returns an Option that sets a custom API base URL for the client. Use this to point to a different Sendamatic API endpoint or a testing environment.
Example:
client := sendamatic.NewClient("user", "pass",
sendamatic.WithBaseURL("https://custom.api.url"))
func WithHTTPClient ¶
WithHTTPClient returns an Option that replaces the default HTTP client with a custom one. This allows full control over HTTP behavior such as transport settings, connection pooling, and custom middleware.
Example:
customClient := &http.Client{
Timeout: 60 * time.Second,
Transport: customTransport,
}
client := sendamatic.NewClient("user", "pass",
sendamatic.WithHTTPClient(customClient))
func WithTimeout ¶
WithTimeout returns an Option that sets the HTTP client timeout duration. This determines how long the client will wait for a response before timing out. The default timeout is 30 seconds.
Example:
client := sendamatic.NewClient("user", "pass",
sendamatic.WithTimeout(60*time.Second))
type SendResponse ¶
type SendResponse struct {
StatusCode int
Recipients map[string][2]interface{} // Email address -> [status code, message ID]
}
SendResponse represents the response from a send email request. It contains the overall HTTP status code and per-recipient delivery information including individual status codes and message IDs.
func (*SendResponse) GetMessageID ¶
func (r *SendResponse) GetMessageID(email string) (string, bool)
GetMessageID returns the message ID for a specific recipient email address. The message ID can be used to track the email in logs or with the email provider. Returns the message ID and true if found, or empty string and false if not found.
func (*SendResponse) GetStatus ¶
func (r *SendResponse) GetStatus(email string) (int, bool)
GetStatus returns the delivery status code for a specific recipient email address. The status code indicates whether the email was accepted for delivery to that recipient. Returns the status code and true if found, or 0 and false if not found.
Note: The API returns status codes as JSON numbers which are decoded as float64, so this method performs the necessary type conversion to int.
func (*SendResponse) IsSuccess ¶
func (r *SendResponse) IsSuccess() bool
IsSuccess returns true if the email send request was successful (HTTP 200). Note that this checks the overall request status; individual recipients may still have failed. Use GetStatus to check per-recipient delivery status.