Prerequisites
Before getting started, you’ll need to:- Authorize your sending domain
- Create and retrieve your SendLayer API key
Installation
Install the SendLayer PHP SDK using Composer:Usage
After installation, you’re ready to integrate the SDK into your code. The SDK includes modules for sending emails, managing webhooks, and retrieving events.Sending an Email
Create or edit a.php
file. Then import and initialize the client with your API key.
SendEmail.php
Sending HTML Emails
To send HTML emails, include thehtml
parameter when calling send()
.
SendEmail.php
You can include the
text
parameter to provide both plain text and HTML versions of your email.Sending to Multiple Recipients
You can send emails to multiple recipients and include CC/BCC addresses.SendEmail.php
name
and email
.
There are limits to the number of recipients you can add to a single request. See our rate limiting guide to learn more.
Including Attachments
You can add attachments by including theattachments
parameter in the payload.
SendEmail.php
The maximum email size allowed is 10MB. This includes the message and all attachments.
Email Parameters
Below are the supported parameters for sending emails with the PHP SDK.Parameter | Type | Required | Description |
---|---|---|---|
to | string or array | Yes | Email address(es) of the recipient(s). Can be a single string or an array of objects with email and name |
from | string or array | Yes | Email address of the sender. Can be a single string or an object with email and name |
subject | string | Yes | Subject line of the email |
text | string | No | Plain text version of the email content |
html | string | No | HTML version of the email content |
cc | array | No | Array of objects for CC recipients |
bcc | array | No | Array of objects for BCC recipients |
replyTo | array | No | Array of objects for reply-to addresses |
attachments | array | No | Array of objects with file path and MIME type |
tags | array | No | Array of strings used to tag emails |
headers | array | No | Associative array of custom headers |
Managing Webhooks
With the PHP SDK, you can create webhooks, retrieve them, and delete a specific webhook.Creating a New Webhook
Initialize the client and callWebhooks->create()
with required parameters url
and event
.
Webhooks.php
event
parameter accepts the following options:
- bounce
- click
- open
- unsubscribe
- complaint
- delivery
Getting All Webhooks
UseWebhooks->get()
to view all webhooks you’ve created.
Webhooks.php
Deleting a Webhook
UseWebhooks->delete()
with the numeric webhookId
.
Deleting a webhook cannot be undone.
Webhooks.php
Webhooks Parameters
The table below contains details about the supported parameters in theWebhooks
module.
Parameter | Type | Required | Description |
---|---|---|---|
url | string | Yes | The webhook endpoint URL where events will be sent |
event | string | Yes | The type of event to listen for. Options: bounce, click, open, unsubscribe, complaint, delivery |
webhookId | integer | Yes | Unique identifier for the webhook (used in delete operation) |
Retrieving Email Events
You can view all events connected to your API key.Getting All Events
Initialize the client and callEvents->get()
to retrieve events.
The
Events->get()
method retrieves the top 5 events if no filters are specified.Events.php
Filtering Events
You can filter events by date range, event type, message ID, and pagination options.Events.php
Events Parameters
The table below lists available parameters in theEvents
module.
Parameter | Type | Required | Description |
---|---|---|---|
startDate | DateTime or int | No | Start date for filtering events (Unix timestamp or DateTime) |
endDate | DateTime or int | No | End date for filtering events (Unix timestamp or DateTime) |
event | string | No | Filter by event type. Supported: accepted , rejected , delivered , opened , clicked , unsubscribed , complained , failed |
messageId | string | No | Filter by the email MessageId |
startFrom | integer | No | Starting position for pagination |
retrieveCount | integer | No | Number of event records to retrieve (defaults to 5) |
Error Handling
Use try/catch blocks to handle SDK errors. The SDK provides specific exception types for different failure scenarios.SendEmail.php
Exception Types
SendLayerException
: Base exception for all SendLayer errorsSendLayerAuthenticationException
: Invalid API key or authentication issuesSendLayerValidationException
: Invalid parameters or validation errorsSendLayerAPIException
: API-specific errors with status code and response data
More Examples
SendLayer PHP SDK
View more details and examples on GitHub.