PHP code example of sendlayer / sendlayer-php
1. Go to this page and download the library: Download sendlayer/sendlayer-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sendlayer / sendlayer-php example snippets
endLayer\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();
}
$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 protected] ',
to: '[email protected] ',
subject: 'Welcome!',
text: 'Welcome to our platform!',
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'
);
use SendLayer\Exceptions\SendLayerException;
try {
$response = $sendlayer->Emails->send(/* ... */);
} catch (SendLayerException $e) {
// Handle other SendLayer errors
echo "Error: " . $e->getMessage();
}
try {
$response = $sendlayer->Emails->send(/* ... */);
} catch (SendLayerException $e) {
echo $e->getMessage(); // e.g. "Recipient email is suppressed"
foreach ($e->errors as $error) {
echo $error['Code']; // e.g. 14
echo $error['Message']; // e.g. "Recipient email is suppressed"
}
// Branch on a specific SendLayer error code:
if (in_array(14, array_column($e->errors, 'Code'), true)) {
// recipient suppressed
}
}
bash
composer