PHP code example of zone-eu / wildduck-php-client

1. Go to this page and download the library: Download zone-eu/wildduck-php-client 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/ */

    

zone-eu / wildduck-php-client example snippets


use Zone\Wildduck\WildduckClient;
use Zone\Wildduck\Dto\User\CreateUserDto;

// Initialize client
$client = new WildduckClient([
    'accessToken' => 'your-api-token',
    'apiUrl' => 'https://api.wildduck.email',
]);

// Create a user
$createDto = new CreateUserDto(
    username: 'john.doe',
    password: 'SecurePass123!',
    address: '[email protected]',
    name: 'John Doe'
);

$result = $client->users()->create($createDto);
echo "User created with ID: {$result->id}\n";

// Get user details
$user = $client->users()->get($result->id);
echo "Username: {$user->username}\n";
echo "Email: {$user->address}\n";

// Update user
$updateDto = new UpdateUserDto(
    name: 'John Updated Doe'
);
$client->users()->update($result->id, $updateDto);

// Delete user
$client->users()->delete($result->id);

use Zone\Wildduck\Dto\Message\UploadMessageDto;

// Upload a message
$uploadDto = new UploadMessageDto(
    raw: base64_encode($emailSource),
    mailbox: $mailboxId
);

$result = $client->messages()->upload($userId, $mailboxId, $uploadDto);

// Search messages
$messages = $client->messages()->search($userId, [
    'q' => 'from:[email protected]',
    'limit' => 10
]);

foreach ($messages->results as $message) {
    echo "Subject: {$message->subject}\n";
}

use Zone\Wildduck\Dto\Filter\CreateFilterDto;
use Zone\Wildduck\Dto\Shared\FilterQueryDto;
use Zone\Wildduck\Dto\Shared\FilterActionDto;

$createDto = new CreateFilterDto(
    name: 'Spam Filter',
    query: new FilterQueryDto(from: '[email protected]'),
    action: new FilterActionDto(delete: true)
);

$client->filters()->create($userId, $createDto);

// Stream user mailbox updates
$response = $client->events()->forUser($userId);
// Returns StreamedResponse that can be processed with EventSource

$client = new WildduckClient([
    'accessToken' => 'your-token',       // Required
    'apiUrl' => 'https://api.example.com', // Optional
    'apiVersion' => 'v1',                 // Optional
    'httpClient' => $customClient,        // Optional custom HTTP client
]);

// Request DTOs
use Zone\Wildduck\Dto\User\CreateUserDto;
use Zone\Wildduck\Dto\User\UpdateUserDto;

// Response DTOs
use Zone\Wildduck\Dto\User\UserDto;
use Zone\Wildduck\Dto\User\UserInfoDto;
use Zone\Wildduck\Dto\PaginatedResultDto;

class UserService extends AbstractService
{
    public function create(CreateUserDto|null $params = null): UserInfoDto
    {
        return $this->requestDto('post', '/users', $params, UserInfoDto::class);
    }

    public function get(string $id): UserDto
    {
        return $this->requestDto('get', "/users/{$id}", null, UserDto::class);
    }
}