SDK
PHP SDK
The official PHP SDK for Unosend. Supports PHP 8.0+ with PSR compatibility.
Installation
terminal
composer require unosend/unosend-phpRequirements
- PHP 8.0 or higher
- Composer
Environment Variables
Never hardcode your API key. Use environment variables instead:
.env
UNOSEND_API_KEY=un_your_api_keyindex.php
<?php
require_once 'vendor/autoload.php';
use Unosend\Unosend;
$unosend = new Unosend(getenv('UNOSEND_API_KEY'));Basic Usage
send_email.php
<?php
require_once 'vendor/autoload.php';
use Unosend\Unosend;
$unosend = new Unosend('un_your_api_key');
// Send an email
$email = $unosend->emails->send([
'from' => 'hello@yourdomain.com',
'to' => 'user@example.com',
'subject' => 'Welcome!',
'html' => '<h1>Hello!</h1><p>Welcome to our platform.</p>'
]);
echo "Email sent: " . $email['id'];Response Format
All SDK methods return an associative array with the response data:
response.php
// Successful response
$email = $unosend->emails->send([...]);
// Response array
[
'id' => 'em_xxxxxxxxxxxxxxxxxxxxxxxx',
'from' => 'hello@yourdomain.com',
'to' => ['user@example.com'],
'subject' => 'Welcome!',
'status' => 'queued',
'createdAt' => '2024-01-15T10:30:00Z'
]
// Access response data
echo $email['id'];
echo $email['status'];Send to Multiple Recipients
multiple.php
$email = $unosend->emails->send([
'from' => 'hello@yourdomain.com',
'to' => ['user1@example.com', 'user2@example.com'],
'subject' => 'Team Update',
'html' => '<h1>Important Update</h1>',
'cc' => ['manager@example.com'],
'bcc' => ['archive@example.com'],
'replyTo' => 'support@yourdomain.com'
]);Send with Attachments
attachments.php
$email = $unosend->emails->send([
'from' => 'hello@yourdomain.com',
'to' => 'user@example.com',
'subject' => 'Your Invoice',
'html' => '<p>Please find your invoice attached.</p>',
'attachments' => [
[
'filename' => 'invoice.pdf',
'content' => base64_encode(file_get_contents('invoice.pdf')),
'contentType' => 'application/pdf'
]
]
]);Working with Domains
domains.php
// Add a domain
$domain = $unosend->domains->create('yourdomain.com');
echo "Domain added: " . $domain['id'];
echo "DNS Records: " . print_r($domain['records'], true);
// List all domains
$domains = $unosend->domains->list();
foreach ($domains as $d) {
echo $d['name'] . " - " . $d['status'] . "\n";
}
// Verify domain DNS
$domain = $unosend->domains->verify($domainId);
echo "Domain status: " . $domain['status'];
// Delete a domain
$unosend->domains->delete($domainId);Working with Audiences & Contacts
audiences.php
// Create an audience
$audience = $unosend->audiences->create('Newsletter Subscribers');
echo "Audience created: " . $audience['id'];
// Add a contact to the audience
$contact = $unosend->contacts->create($audience['id'], [
'email' => 'user@example.com',
'firstName' => 'John',
'lastName' => 'Doe'
]);
echo "Contact added: " . $contact['id'];
// List contacts
$contacts = $unosend->contacts->list($audience['id']);
foreach ($contacts as $c) {
echo $c['email'] . " - " . $c['firstName'] . "\n";
}
// Update a contact
$contact = $unosend->contacts->update($contactId, [
'firstName' => 'Jane'
]);
// Delete a contact
$unosend->contacts->delete($contactId);Error Handling
errors.php
use Unosend\Exceptions\UnosendException;
try {
$email = $unosend->emails->send([
'from' => 'hello@yourdomain.com',
'to' => 'user@example.com',
'subject' => 'Test',
'html' => '<p>Hello</p>'
]);
echo "Success! Email ID: " . $email['id'];
} catch (UnosendException $e) {
echo "Error " . $e->getCode() . ": " . $e->getMessage();
// Handle specific error codes
if ($e->getCode() === 401) {
echo "Invalid API key";
} elseif ($e->getCode() === 429) {
echo "Rate limit exceeded";
}
}Custom Configuration
config.php
// Custom base URL (for self-hosted instances)
$unosend = new Unosend('un_your_api_key', [
'baseUrl' => 'https://your-instance.com/api/v1'
]);
// Custom timeout
$unosend = new Unosend('un_your_api_key', [
'timeout' => 60
]);Laravel Integration
config/services.php
// Add to config/services.php
'unosend' => [
'key' => env('UNOSEND_API_KEY'),
],app/Services/EmailService.php
<?php
namespace App\Services;
use Unosend\Unosend;
class EmailService
{
private Unosend $unosend;
public function __construct()
{
$this->unosend = new Unosend(config('services.unosend.key'));
}
public function sendWelcomeEmail(string $email, string $name): array
{
return $this->unosend->emails->send([
'from' => 'hello@yourdomain.com',
'to' => $email,
'subject' => "Welcome, {$name}!",
'html' => "<h1>Hello {$name}</h1><p>Welcome to our platform!</p>"
]);
}
}