PHP code example of duva-mail / duva

1. Go to this page and download the library: Download duva-mail/duva 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/ */

    

duva-mail / duva example snippets


use Duva\Client;

$duva = new Client(apiKey: 'dv_...', domain: 'example.com'); // or DUVA_API_KEY / DUVA_DOMAIN

$message = $duva->messages->send([
    'from' => 'Example <[email protected]>',
    'to' => ['[email protected]'],
    'subject' => 'Your order',
    'text' => 'Thank you for your order.',
]);
echo $message->id, ' ', $message->status; // "queued": always asynchronous

foreach ($duva->events->listAll(type: 'bounced') as $event) {
    echo $event->type, ' ', $event->detail['recipient'] ?? '', "\n";
}

use Duva\Webhooks;
use Duva\Exceptions\WebhookSignatureException;

try {
    $event = Webhooks::constructEvent(getenv('DUVA_WEBHOOK_SECRET'), $headers, $rawBody);
    echo $event->type, ' ', $event->data['message_id'] ?? '';
} catch (WebhookSignatureException) {
    // respond 400
}

use Duva\Exceptions\NotFoundException;
use Duva\Exceptions\QuotaExceededException;
use Duva\Exceptions\ValidationException;

try {
    $duva->messages->send([...]);
} catch (ValidationException $e) {
    var_dump($e->fields); // [['field' => 'to[0]', 'message' => '...']]
} catch (QuotaExceededException $e) {
    echo "retry in {$e->retryAfter}s";
} catch (NotFoundException) {
    // the API key, domain or resource could not be found
}

use Duva\Attachment;

$attachment = Attachment::fromFile('./invoice.pdf');
$duva->messages->send([..., 'attachments' => [$attachment]]);

use Duva\Http\TestTransport;

$transport = new TestTransport(fn ($config, $spec) => TestTransport::response(['status' => 'ok']));
$duva = new Client(apiKey: 'dv_test', domain: 'example.com', transport: $transport);
$duva->health();
$transport->requests[0]->path; // "/health"
bash
composer install
composer test                                                       # unit tests
php scripts/fetch-conformance.php && composer test:conformance
composer stan                                                        # PHPStan, level max