Skip to main content
Package: unosend/unosend-php · PHP: 8.1+ · Dependencies: Guzzle HTTP

Installation

composer require unosend/unosend-php

Quick Start

<?php
require 'vendor/autoload.php';

use Unosend\Unosend;

$unosend = new Unosend('un_your_api_key');

$result = $unosend->emails->send([
    'from' => 'hello@yourdomain.com',
    'to' => ['user@example.com'],
    'subject' => 'Hello from Unosend!',
    'html' => '<h1>Welcome!</h1><p>Thanks for signing up.</p>'
]);

echo "Email sent: " . $result->id . "\n";

Configuration

$unosend = new Unosend('un_your_api_key', [
    'base_url' => 'https://www.unosend.co/api/v1', // default
    'timeout' => 30, // seconds
]);
Never hardcode your API key. Use environment variables: $_ENV['UNOSEND_API_KEY'] or getenv('UNOSEND_API_KEY')

Emails

Send an Email

$result = $unosend->emails->send([
    'from' => 'John Doe <john@yourdomain.com>',
    'to' => ['user@example.com'],
    'subject' => 'Order Confirmation',
    'html' => '<h1>Order #1234</h1><p>Your order has been confirmed.</p>',
    'text' => 'Order #1234 - Your order has been confirmed.',
    'reply_to' => 'support@yourdomain.com',
    'cc' => ['manager@yourdomain.com'],
    'bcc' => ['archive@yourdomain.com'],
    'tags' => [['name' => 'category', 'value' => 'order']],
    'tracking' => ['open' => true, 'click' => true],
]);

Send with Attachments

$result = $unosend->emails->send([
    'from' => 'billing@yourdomain.com',
    'to' => ['customer@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')),
            'content_type' => 'application/pdf',
        ]
    ],
]);

Send with Templates

$result = $unosend->emails->send([
    'from' => 'hello@yourdomain.com',
    'to' => ['user@example.com'],
    'subject' => 'Welcome, {{first_name}}!',
    'template_id' => 'tmpl_abc123',
    'template_data' => [
        'first_name' => 'Alice',
        'company' => 'Acme Inc',
    ],
]);

Send Batch Emails

$results = $unosend->emails->sendBatch([
    [
        'from' => 'hello@yourdomain.com',
        'to' => ['alice@example.com'],
        'subject' => 'Welcome, Alice!',
        'html' => '<h1>Welcome!</h1>',
    ],
    [
        'from' => 'hello@yourdomain.com',
        'to' => ['bob@example.com'],
        'subject' => 'Welcome, Bob!',
        'html' => '<h1>Welcome!</h1>',
    ],
]);

Get & List Emails

// Get email details
$email = $unosend->emails->get('email_abc123');
echo $email->status; // 'delivered'

// List emails
$emails = $unosend->emails->list(['limit' => 50, 'status' => 'delivered']);
foreach ($emails->data as $email) {
    echo "{$email->to} — {$email->status}\n";
}

Domains

// Create domain
$domain = $unosend->domains->create(['domain' => 'yourdomain.com']);
print_r($domain->dns_records);

// Verify
$verified = $unosend->domains->verify($domain->id);
echo "Verified: " . ($verified->verified ? 'yes' : 'no');

Contacts

// Create audience
$audience = $unosend->audiences->create(['name' => 'Newsletter Subscribers']);

// Add contact
$contact = $unosend->contacts->create($audience->id, [
    'email' => 'user@example.com',
    'first_name' => 'Alice',
    'last_name' => 'Smith',
    'metadata' => ['plan' => 'pro'],
]);

SMS

$sms = $unosend->sms->send([
    'to' => '+14155551234',
    'message' => 'Your verification code is 384729. Expires in 10 minutes.',
]);

echo "SMS sent: {$sms->id} (Cost: \${$sms->cost})\n";

WhatsApp

$msg = $unosend->whatsapp->send([
    'to' => '+919876543210',
    'type' => 'template',
    'template' => 'order_update',
    'language' => 'en',
    'variables' => ['Alice', 'ORD-1234'],
]);

Error Handling

use Unosend\Exceptions\UnosendException;
use Unosend\Exceptions\AuthenticationException;
use Unosend\Exceptions\ValidationException;
use Unosend\Exceptions\RateLimitException;

try {
    $result = $unosend->emails->send([
        'from' => 'hello@yourdomain.com',
        'to' => ['user@example.com'],
        'subject' => 'Hello',
        'html' => '<h1>Hello</h1>',
    ]);
    echo "Sent: {$result->id}\n";
} catch (AuthenticationException $e) {
    echo "Invalid API key\n";
} catch (ValidationException $e) {
    echo "Validation error: {$e->getMessage()}\n";
} catch (RateLimitException $e) {
    echo "Rate limited. Retry after {$e->getRetryAfter()} seconds\n";
} catch (UnosendException $e) {
    echo "Error {$e->getStatusCode()}: {$e->getMessage()}\n";
}

Framework Examples

Laravel

// config/services.php
'unosend' => [
    'api_key' => env('UNOSEND_API_KEY'),
],

// app/Services/EmailService.php
use Unosend\Unosend;

class EmailService
{
    private Unosend $client;

    public function __construct()
    {
        $this->client = new Unosend(config('services.unosend.api_key'));
    }

    public function sendWelcome(string $email, string $name): string
    {
        $result = $this->client->emails->send([
            'from' => 'welcome@yourdomain.com',
            'to' => [$email],
            'subject' => "Welcome, {$name}!",
            'html' => "<h1>Welcome, {$name}!</h1><p>Thanks for joining.</p>",
        ]);

        return $result->id;
    }
}

Symfony

// src/Service/EmailService.php
namespace App\Service;

use Unosend\Unosend;

class EmailService
{
    private Unosend $client;

    public function __construct(string $apiKey)
    {
        $this->client = new Unosend($apiKey);
    }

    public function send(string $to, string $subject, string $html): string
    {
        $result = $this->client->emails->send([
            'from' => 'hello@yourdomain.com',
            'to' => [$to],
            'subject' => $subject,
            'html' => $html,
        ]);

        return $result->id;
    }
}

Environment Variables

.env
UNOSEND_API_KEY=un_your_api_key

Full API Reference

See all available endpoints and parameters