duta / duta-php
The official PHP SDK for Duta, transactional email for Malaysia, with a Laravel mail driver.
Requires
- php: ^8.1
- ext-json: *
- guzzlehttp/guzzle: ^7.8
Requires (Dev)
- orchestra/testbench: ^8.0 || ^9.0 || ^10.0
- phpunit/phpunit: ^10.5 || ^11.0
Suggests
- laravel/framework: Send Laravel mail through Duta with MAIL_MAILER=duta
Provides
None
Conflicts
None
Replaces
None
README
The official PHP SDK for Duta, transactional email for Malaysia, with a Laravel mail driver.
- PHP 8.1+. One dependency, Guzzle. Laravel 12 and 13.
- Laravel: set
MAIL_MAILER=dutaand every Mailable sends through Duta. - Retries rate limits and server errors safely: every send carries an idempotency key, so a retry can never send twice.
Upgrading from 0.1.x
0.2.0 is a new SDK for Duta's current API, not an update of 0.1.x, which was written for an earlier version of Duta that no longer runs.
Install
composer require duta/duta-php
Laravel
The package registers itself. Add your key to .env and choose the mailer:
DUTA_API_KEY=duta_xxxxxxxxxxxxxxxx MAIL_MAILER=duta
Your existing mail code now sends through Duta:
Mail::to($order->customer)->send(new OrderReceipt($order));
Optional: put the key in config/services.php instead of the environment:
'duta' => ['key' => env('DUTA_API_KEY')],
A Mailable can set two Duta-specific headers:
public function headers(): Headers { return new Headers(text: [ 'X-Duta-Tag-type' => 'receipt', // a tag: type=receipt 'X-Duta-Idempotency-Key' => "receipt-{$this->order->id}", // safe if the job runs twice ]); }
For everything else, inject Duta\Duta:
public function __construct(private \Duta\Duta $duta) {}
Send an email
$duta = new Duta\Duta(getenv('DUTA_API_KEY')); $sent = $duta->emails->send([ 'from' => 'Kedai <resit@kedai.my>', 'to' => 'siti@example.com', 'subject' => 'Resit #1042', 'html' => '<p>Terima kasih.</p>', ]); echo $sent['id'];
Errors throw Duta\DutaException. getErrorCode() is Duta's
error code and getRequestId()
finds the request on the Logs screen.
try { $duta->emails->send($email, ['idempotency_key' => "receipt-{$order->id}"]); } catch (Duta\DutaException $e) { report($e->getErrorCode() . ' ' . $e->getRequestId()); }
Batch and paging
$duta->batch->send([$first, $second], ['validation' => 'permissive']); foreach ($duta->emails->listAll(['status' => 'bounced']) as $email) { echo $email['id'], PHP_EOL; }
Verify webhooks
$event = Duta\Webhook::verify( $request->getContent(), // the raw body $request->headers->all(), config('services.duta.webhook_secret'), );
It throws Duta\WebhookVerificationException when the signature is wrong or
the delivery is more than five minutes old.
Everything else
emails |
send, get, list, listAll |
batch |
send |
domains |
create, list, get, verify, remove |
apiKeys |
create, list, remove |
webhooks |
create, list, get, remove, enable, test, deliveries, verify |
suppressions |
create, list, listAll, remove |
logs |
list, listAll, get |
usage |
get |
Options: new Duta\Duta($key, ['base_url' => ..., 'timeout' => 30.0, 'max_retries' => 2]).
Full documentation: https://docs.duta.indra.sh