Prerequisites
Before getting started, you’ll need to:- Authorize your sending domain
- Create and retrieve your SendLayer API key
Installation
Install the SendLayer Go SDK using thego get command:
Usage
After installation, you can use the SDK modules for sending emails, managing webhooks, and retrieving email events.Send Email in Golang
Initialize the client and callsl.Emails.Send() with a SendEmailRequest.
sendEmail.go
Send HTML Emails in Go
To send HTML emails, set theHtml field in the request payload.
sendEmail.go
You can include both
Text and Html to send plain text and HTML versions of the same email.Send Email to Multiple Recipients
UseEmailAddress objects to send to multiple recipients, including Cc, Bcc, and ReplyTo.
sendEmail.go
From and To accept either a string email or EmailAddress values. Cc, Bcc, and ReplyTo support the same flexible format.
There are limits to the number of email recipients you can add to a single request. See our rate limiting guide to learn more.
Include Attachments in Email
Include attachments by setting theAttachments field in SendEmailRequest.
sendEmail.go
Path to your local file path or hosted file URL, then set the file Type (MIME type).
SendLayer SDK supports both local and remote file attachments and allows you to add multiple attachment files. See our tutorial to learn more about attaching files to emails.
Email Request Parameters
The table below contains the supported fields inSendEmailRequest.
| Field | Type | Required | Description |
|---|---|---|---|
From | string or EmailAddress | Yes | Sender email, as a plain email string or EmailAddress (Email + optional Name) |
To | string, EmailAddress, or []EmailAddress | Yes | Recipient email(s), as one address or multiple addresses |
Subject | string | Yes | Email subject line |
Text | string | Yes | Plain text body |
Html | string | No | HTML body |
Cc | EmailAddress or []EmailAddress | No | Carbon-copy recipients |
Bcc | EmailAddress or []EmailAddress | No | Blind carbon-copy recipients |
ReplyTo | EmailAddress or []EmailAddress | No | Reply-to recipient(s) |
Attachments | []Attachment | No | File attachments with Path and MIME Type |
Headers | map[string]string | No | Custom email headers |
Tags | []string | No | Tags for categorizing emails |
Manage Webhooks in Go
With the SendLayer Go SDK, you can create, list, and delete webhooks.Create a New Webhook
CallWebhooks.Create() with a WebhookCreateRequest.
webhooks.go
Event field accepts the following options:
- bounce
- click
- open
- unsubscribe
- complaint
- delivery
Get All Webhooks
To view all the webhooks you’ve created, use theWebhooks.Get() method.
webhooks.go
Delete a Webhook
To delete a specific webhook, pass the webhook ID toWebhooks.Delete().
webhooks.go
Webhook Request Parameters
The table below contains the supported inputs in theWebhooks module.
| Parameter | Type | Required | Description |
|---|---|---|---|
WebhookURL | string | Yes (create) | Webhook endpoint URL where SendLayer sends events |
Event | string | Yes (create) | Event type for the webhook subscription |
webhookId | int | Yes (delete) | Unique webhook ID to delete |
Retrieve Email Events
Use theEvents module to retrieve email delivery and engagement events.
Get All Events
Initialize the client and callEvents.Get(nil) to retrieve recent events.
events.go
Filter Events
Pass aGetEventsRequest to filter by date range and event type.
events.go
Event Request Parameters
The table below contains supported fields inGetEventsRequest.
| Field | Type | Required | Description |
|---|---|---|---|
StartDate | *time.Time | No | Start date for filtering events |
EndDate | *time.Time | No | End date for filtering events |
Event | string | No | Filter by event type (for example, opened) |
MessageID | string | No | Filter by SendLayer message ID |
StartFrom | *int | No | Starting offset for paginated event results |
RetrieveCount | *int | No | Number of records to return |
Error Handling
The SDK returns typed errors you can inspect witherrors.As.
More Go SDK Examples
SendLayer Go SDK
View more details and examples on GitHub.