Installation
composer require unosend/unosend-php
Requirements
- PHP 8.0 or higher
- Composer
Environment Variables
Never hardcode your API key. Use environment variables instead.
UNOSEND_API_KEY=un_your_api_key
<?php
require_once 'vendor/autoload.php';
use Unosend\Unosend;
$unosend = new Unosend(getenv('UNOSEND_API_KEY'));
Basic Usage
<?php
require_once 'vendor/autoload.php';
use Unosend\Unosend;
$unosend = new Unosend('un_your_api_key');
// Send an email
$email = $unosend->emails->send([
'from' => '[email protected]',
'to' => '[email protected]',
'subject' => 'Welcome!',
'html' => '<h1>Hello!</h1><p>Welcome to our platform.</p>'
]);
echo "Email sent: " . $email['id'];
All SDK methods return an associative array with the response data:
// Successful response
$email = $unosend->emails->send([...]);
// Response array
[
'id' => 'em_xxxxxxxxxxxxxxxxxxxxxxxx',
'from' => '[email protected]',
'to' => ['[email protected]'],
'subject' => 'Welcome!',
'status' => 'queued',
'createdAt' => '2024-01-15T10:30:00Z'
]
// Access response data
echo $email['id'];
echo $email['status'];
Send to Multiple Recipients
Send with Attachments
$email = $unosend->emails->send([
'from' => '[email protected]',
'to' => '[email protected]',
'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
// 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);
// 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' => '[email protected]',
'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
use Unosend\Exceptions\UnosendException;
try {
$email = $unosend->emails->send([
'from' => '[email protected]',
'to' => '[email protected]',
'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
// 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
Add to your config/services.php:'unosend' => [
'key' => env('UNOSEND_API_KEY'),
],
Create an email service: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' => '[email protected]',
'to' => $email,
'subject' => "Welcome, {$name}!",
'html' => "<h1>Hello {$name}</h1><p>Welcome to our platform!</p>"
]);
}
}
Available Methods
| Method | Description |
|---|
$emails->send() | Send an email |
$emails->get($id) | Get email details by ID |
$emails->list() | List all emails |
$domains->create($name) | Add a domain |
$domains->verify($id) | Verify domain DNS |
$domains->list() | List all domains |
$domains->delete($id) | Delete a domain |
$audiences->create($name) | Create an audience |
$audiences->list() | List all audiences |
$contacts->create($audienceId, $data) | Add a contact to audience |
$contacts->list($audienceId) | List contacts in audience |
$contacts->update($contactId, $data) | Update a contact |
$contacts->delete($contactId) | Delete a contact |
View full API reference
Explore all available API endpoints