The official PHP SDK for interacting with the SendLayer API, providing a simple and intuitive interface for sending emails, managing webhooks, and retrieving email events.
- Email Sending: Send transactional emails with support for HTML, text, attachments, and advanced options
- Webhook Management: Create, retrieve, and delete webhooks for real-time event notifications
- Event Tracking: Retrieve email events and analytics data
- Error Handling: Comprehensive exception handling with specific error types
- Validation: Built-in validation for email addresses, URLs, and API parameters
- File Attachments: Support for both local and remote file attachments
- Modern PHP: Built with PHP 7.4+ features and PSR-4 autoloading
composer require sendlayer/sendlayer-php- Download the SDK
- Include the autoloader in your project
- Install dependencies:
composer install
<?php
require_once 'vendor/autoload.php';
use SendLayer\SendLayer;
use SendLayer\Exceptions\SendLayerException;
// Initialize the SDK with your API key
$sendlayer = new SendLayer('your-api-key-here');
try {
// Send a simple email
$response = $sendlayer->Emails->send(
from: '[email protected]',
to: '[email protected]',
subject: 'Test Email',
text: 'This is a test email sent using the SendLayer PHP SDK'
);
echo "Email sent successfully! Message ID: " . $response['MessageID'];
} catch (SendLayerException $e) {
echo "Error: " . $e->getMessage();
}You can pass additional configuration options when initializing the SDK:
$config = [
'timeout' => 30, // HTTP timeout in seconds
'attachmentURLTimeout' => 30000, // Attachment URL timeout in milliseconds
'guzzle' => [ // Additional Guzzle HTTP client options
'verify' => false, // Disable SSL verification (not recommended for production)
'proxy' => 'http://proxy:8080' // Use proxy
]
];
$sendlayer = new SendLayer('your-api-key-here', $config);$response = $sendlayer->Emails->send(
from: '[email protected]',
to: '[email protected]',
subject: 'Welcome!',
text: 'Welcome to our platform!'
);$response = $sendlayer->Emails->send(
from: '[email protected]',
to: '[email protected]',
subject: 'Welcome!',
html: '<h1>Welcome!</h1><p>Welcome to our platform!</p>'
);$response = $sendlayer->Emails->send(
from: ['email' => '[email protected]', 'name' => 'John Doe'],
to: '[email protected]',
subject: 'Welcome!',
text: 'Welcome to our platform!'
);$response = $sendlayer->Emails->send(
from: '[email protected]',
to: ['[email protected]', '[email protected]'],
subject: 'Welcome!',
text: 'Welcome to our platform!'
);$response = $sendlayer->Emails->send(
from: '[email protected]',
to: '[email protected]',
subject: 'Welcome!',
text: 'Welcome to our platform!',
cc: '[email protected]',
bcc: '[email protected]'
);$response = $sendlayer->Emails->send(
from: '[email protected]',
to: '[email protected]',
subject: 'Document attached',
text: 'Please find the attached document.',
attachments: [
[
'path' => '/path/to/document.pdf',
'type' => 'application/pdf'
],
[
'path' => 'https://example.com/image.jpg',
'type' => 'image/jpeg'
]
]
);$response = $sendlayer->Emails->send(
from: '[email protected]',
to: '[email protected]',
subject: 'Welcome!',
text: 'Welcome to our platform!',
headers: [
'X-Custom-Header' => 'Custom Value',
'X-Priority' => '1'
],
tags: ['welcome', 'onboarding']
);$response = $sendlayer->Webhooks->create(
url: 'https://your-domain.com/webhook',
event: 'delivery'
);$webhooks = $sendlayer->Webhooks->get();$sendlayer->Webhooks->delete(webhookId: 123);$events = $sendlayer->Events->get();$events = $sendlayer->Events->get(
startDate: new DateTime('2024-01-01'),
endDate: new DateTime('2024-01-31'),
event: 'delivered',
retrieveCount: 50
);$events = $sendlayer->Events->get(
messageId: 'message-id-here'
);The SDK provides specific exception types for different error scenarios:
use SendLayer\Exceptions\SendLayerException;
try {
$response = $sendlayer->Emails->send(/* ... */);
} catch (SendLayerException $e) {
// Handle other SendLayer errors
echo "Error: " . $e->getMessage();
}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 dataSendLayerNotFoundException: Resource not found (404 errors)SendLayerRateLimitException: Rate limit exceeded (429 errors)SendLayerInternalServerException: Server errors (5xx errors)
bounce: Email bouncedclick: Link was clickedopen: Email was openedunsubscribe: User unsubscribedcomplaint: User marked as spamdelivery: Email was delivered
accepted: Email was accepted by the serverrejected: Email was rejecteddelivered: Email was deliveredopened: Email was openedclicked: Link was clickedunsubscribed: User unsubscribedcomplained: User marked as spamfailed: Email delivery failed
- PHP 7.4 or higher
- Guzzle HTTP Client 7.0 or higher
- JSON extension
- cURL extension (recommended)
This SDK is licensed under the MIT License. See the LICENSE file for details.
For support, please contact:
- Email: [email protected]
- Documentation: https://developers.sendlayer.com
- GitHub Issues: https://github.com/sendlayer/sendlayer-php/issues
We welcome contributions! Please see our Contributing Guide for details.
See CHANGELOG.md for a list of changes and version history.