Prerequisites
Before getting started, you’ll need to:- Authorize your sending domain
- Create and retrieve your SendLayer API key
Installation
Install the SendLayer Ruby SDK using RubyGems:Usage
After the installation completes, you’re ready to start integrating the SDK into your code. The SDK includes modules to send emails, manage webhooks, and retrieve events.Sending an Email in Ruby
Create a new.rb
file or edit an existing one. Then require the sendlayer
gem and initialize the client with your API key. Once done, you
send_email.rb
emails.send()
method to send emails using Ruby. The method requires 4 parameters:
from
: Sender email addressto
: Recipient email address(es)subject
: The subject of your emailtext
orhtml
: This is the email content. You can send plain text or HTML messages to users
send_email.rb
Sending HTML Emails in Ruby
To send HTML emails, include thehtml
parameter in the send
call.
send_email.rb
You can include the
text
parameter to have both plain text and HTML versions of your email.Sending to Multiple Recipients
You can send emails to multiple recipients and includecc
, and bcc
email addresses.
send_email.rb
email
and name
keys.
There are limits to the number of recipients you can add to a single request. See our rate limiting guide.
Including Attachments
When sending emails in Ruby with the SendLayer SDK, you can add attachments by including theattachments
parameter in your email payload.
send_email.rb
The maximum email size is 10MB (message plus attachments).
path
and type
parameters. Make sure to specify the correct MIME type
.
You can attach multiple files to your email payload, including local and remote files.
Email Parameters
Below are the supported parameters for sending emails with the Ruby SDK.Parameter | Type | Required | Description |
---|---|---|---|
to | String or Array<Hash> | Yes | Recipient email(s). String or array of { email, name } hashes |
from | String or Hash | Yes | Sender email. String or { email, name } hash |
subject | String | Yes | Subject line of the email |
text | String | Yes | Plain text version of the email content |
html | String | No | HTML version of the email content |
cc | String or Array<Hash> | No | CC recipient(s). String or array of { email, name } hashes |
bcc | String or Array<Hash> | No | BCC recipient(s). String or array of { email, name } hashes |
reply_to | String or Array<Hash> | No | Reply-to address(es). String or array of { email, name } hashes |
attachments | Array<Hash> | No | Attachment objects with path and type keys |
tags | Array<String> | No | Tags to associate with the email |
headers | Hash | No | Custom headers as key-value pairs |
Managing Webhooks
With the SendLayer Ruby SDK, you can create webhooks, list existing webhooks, and delete a webhook.Creating a New Webhook
After initializing the client, call thewebhooks.create
method and pass the required parameters. You’ll need to pass the url
and event
parameters.
webhooks.rb
event
parameter supports the following values:
- bounce
- click
- open
- unsubscribe
- complaint
- delivery
See our guide to learn more about managing webhooks and understanding webhook payload format.
Getting All Webhooks
Usewebhooks.get
to retrieve all webhooks created on your SendLayer account.
webhooks.rb
Deleting a Webhook
Usewebhooks.delete(webhook_id)
to delete a webhook by its numeric ID.
Deleting a webhook cannot be undone.
webhooks.rb
Webhooks Parameters
Parameter | Type | Required | Description |
---|---|---|---|
url | String | Yes | Webhook endpoint URL where events will be sent |
event | String | Yes | Event to listen for. One of: bounce, click, open, unsubscribe, complaint, delivery |
webhook_id | Integer | Yes | Unique identifier for the webhook (used for deleting a webhook) |
Retrieving Email Events
You can retrieve events associated with your API key.Getting All Events
Callevents.get
with no parameters to retrieve recent events.
If no filter is provided, the SDK returns the top 5 events.
events.rb
Filtering Events
You can use optional parameters to filter results. Here’s an example:events.rb
Events Parameters
Parameter | Type | Required | Description |
---|---|---|---|
start_date | Time | No | Start date for filtering events |
end_date | Time | No | End date for filtering events |
event | String | No | Filter by event type. Supported: accepted , rejected , delivered , opened , clicked , unsubscribed , complained , failed |
message_id | String | No | Filter by email MessageId |
start_from | Integer | No | Starting number for pagination |
retrieve_count | Integer | No | Number of records to retrieve (defaults to 5) |
Error Handling
The SDK provides custom exception types for better error handling.Exception Types
SendLayer::SendLayerError
: Base exception for all SendLayer errorsSendLayer::SendLayerAPIError
: API-specific errors with status code and response dataSendLayer::SendLayerAuthenticationError
: Invalid API key or authentication issuesSendLayer::SendLayerValidationError
: Invalid parameters or validation errorsSendLayer::SendLayerNotFoundError
: Resource not found (404 errors)SendLayer::SendLayerRateLimitError
: Rate limit exceeded (429 errors)SendLayer::SendLayerInternalServerError
: Server errors (5xx errors)
More Examples
SendLayer Ruby SDK
View more details and examples on GitHub.