PHP code example of paubox / paubox-php

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

    

paubox / paubox-php example snippets



env = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();

$paubox = new Paubox\Paubox();

$message = new Paubox\Mail\Message();
$content = new Paubox\Mail\Content();
$content->setPlainText("Hello World");

$header = new Paubox\Mail\Header();
$header->setSubject("Testing!");
$header->setFrom("[email protected]");

$recipients = array();
array_push($recipients,'[email protected]');

$message->setHeader($header);
$message->setContent($content);
$message->setRecipients($recipients);

$sendMessageResponse = new Paubox\Mail\SendMessageResponse();
$sendMessageResponse = $paubox->sendMessage($message);
print_r($sendMessageResponse);


env = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();

$paubox = new Paubox\Paubox();

$message = new Paubox\Mail\Message();
$content = new Paubox\Mail\Content();
$content->setPlainText("Hello World");

$header = new Paubox\Mail\Header();
$header->setSubject("Testing!");
$header->setFrom("[email protected]");

$recipients = array();
array_push($recipients,'[email protected]');

$message->setHeader($header);
$message->setContent($content);
$message->setRecipients($recipients);
$message->setAllowNonTLS(true);

$sendMessageResponse = new Paubox\Mail\SendMessageResponse();
$sendMessageResponse = $paubox->sendMessage($message);
print_r($sendMessageResponse);


env = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();

$paubox = new Paubox\Paubox();

$message = new Paubox\Mail\Message();
$content = new Paubox\Mail\Content();
$content->setPlainText("Hello World");

$header = new Paubox\Mail\Header();
$header->setSubject("Testing!");
$header->setFrom("[email protected]");

$recipients = array();
array_push($recipients,'[email protected]');

$message->setHeader($header);
$message->setContent($content);
$message->setRecipients($recipients);
$message->setForceSecureNotification("true");

$sendMessageResponse = new Paubox\Mail\SendMessageResponse();
$sendMessageResponse = $paubox->sendMessage($message);
print_r($sendMessageResponse);


env = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();

$paubox = new Paubox\Paubox();

$message = new Paubox\Mail\Message();
$content = new Paubox\Mail\Content();
$content->setPlainText("Hello World");
$content->setHtmlText("<html><head></head><body>Hello World</body></html>");

$header = new Paubox\Mail\Header();
$header->setSubject("Testing!");
$header->setFrom("[email protected]");
$header->setReplyTo("[email protected]");

$firstAttachment = new Paubox\Mail\Attachment();
$firstAttachment->setFileName("hello_world.txt");
$firstAttachment->setContentType("text/plain");
$firstAttachment->setContent("SGVsbG8gV29ybGQh\n");

$secondAttachment = new Paubox\Mail\Attachment();
$secondAttachment->setFileName("hello_world2.txt");
$secondAttachment->setContentType("text/plain");
$secondAttachment->setContent("SGVsbG8gV29ybGQh\n");

$attachments = array();
array_push($attachments,$firstAttachment);
array_push($attachments,$secondAttachment);

$recipients = array();
array_push($recipients,'[email protected]');

$bcc = array();
array_push($bcc, '[email protected]');

$cc = array();
array_push($cc, '[email protected]');

$message->setHeader($header);
$message->setContent($content);
$message->setAttachments($attachments);
$message->setRecipients($recipients);
$message->setBcc($bcc);

$sendMessageResponse = new Paubox\Mail\SendMessageResponse();
$sendMessageResponse = $paubox->sendMessage($message);
print_r($sendMessageResponse);


env = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();

$paubox = new Paubox\Paubox();

$resp = $paubox->getEmailDisposition('SOURCE_TRACKING_ID');
print_r($resp);


ms = new Paubox\PauboxForms();

$form = $forms->getForm('YOUR-FORM-UUID');
echo $form->title;       // "Patient Intake Form"
echo $form->form_html;   // "<form>...</form>"
print_r($form->form_json);


ms = new Paubox\PauboxForms();

$submission = new Paubox\Forms\FormSubmission();
$submission->setFormData([
    'first_name' => 'Jane',
    'last_name'  => 'Smith',
    'email'      => '[email protected]',
]);

$result = $forms->submitForm('YOUR-FORM-UUID', $submission);
// $result === true on success


ms = new Paubox\PauboxForms();

$attachment = new Paubox\Forms\FormAttachment();
$attachment->setName('consent.pdf');
$attachment->setContent(base64_encode(file_get_contents('/path/to/consent.pdf')));

$submission = new Paubox\Forms\FormSubmission();
$submission->setFormData(['first_name' => 'Jane', 'last_name' => 'Smith']);
$submission->setAttachments([$attachment]);

$result = $forms->submitForm('YOUR-FORM-UUID', $submission);


ption 1: pass the key directly
$forms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

// Option 2: use the PAUBOX_FORMS_API_KEY environment variable
$forms = new Paubox\PauboxForms();

$forms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY', 'https://staging-forms.example.com');

try {
    $forms->updateForm($formId, ['title' => $newTitle]);
} catch (Paubox\Forms\PauboxFormsException $e) {
    error_log('Forms update failed: ' . $e->getMessage());
    // Read the body explicitly — do NOT log $e->getMessage() alone if
    // the caller might echo the exception unfiltered.
    if ($e->getStatusCode() === 401) {
        // handle unauthorized (missing, invalid, or wrong-scope key)
    }
}


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

$form = $forms->getFormById('YOUR-FORM-UUID');
echo $form->title;
echo $form->active;


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

$result = $forms->listForms([
    'customer_id' => 12345,        // your Paubox customer ID
    'search'   => 'intake',        // match against form titles
    'archived' => false,
    'active'   => true,
    'order'    => 'desc',          // "asc" or "desc"
    'order_by' => 'updated_at',    // "title", "updated_at", "submission_count", or "created_at"
    'page'     => 1,               // 1-based
    'items'    => 25,              // max 100
]);

foreach ($result->results as $form) {
    echo $form->title . "\n";
}
print_r($result->page_info); // count, pages, page, items


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

$form = new Paubox\Forms\Form();
$form->setTitle('Patient Intake Form');
$form->setFormJson(['fields' => [['name' => 'first_name', 'type' => 'text']]]);
$form->setCustomerId(12345);
$form->setDescription('Collects new patient information');
$form->setRecipient('[email protected]');
$form->setActive(true);

$newFormId = $forms->createForm($form);
echo $newFormId; // "NEW-FORM-UUID"


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

$response = $forms->updateForm('YOUR-FORM-UUID', [
    'title'  => 'Patient Intake Form (v2)',
    'active' => false,
]);
echo $response->detail; // "Form updated successfully"


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

$forms->archiveForm('YOUR-FORM-UUID');
$forms->unarchiveForm('YOUR-FORM-UUID');


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

$newForm = $forms->copyForm('SOURCE-FORM-UUID', 'Patient Intake Form (Copy)');
echo $newForm->id;
echo $newForm->title;


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

$stats = $forms->getFormStats();
// or: $stats = $forms->getFormStats(['customer_id' => 12345]);

echo $stats->active_form_count;
echo $stats->total_submission_count;
echo $stats->submissions_last_7_days;


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

$result = $forms->listSubmissions('YOUR-FORM-UUID', [
    'order'    => 'desc',          // "asc" or "desc"
    'order_by' => 'created_at',    // "submitter_email" or "created_at"
    'page'     => 1,
    'items'    => 25,              // max 100
]);

foreach ($result->data as $submission) {
    print_r($submission);
}
echo $result->total;


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

// All submissions for a form
$csv = $forms->getSubmissionsCsv('YOUR-FORM-UUID');
file_put_contents('submissions.csv', $csv);

// A single submission
$csv = $forms->getSubmissionCsv('YOUR-FORM-UUID', 'SUBMISSION-ID');
file_put_contents('submission.csv', $csv);


ms = new Paubox\PauboxForms('YOUR-SCOPED-API-KEY');

$pdf = $forms->getSubmissionPdf('YOUR-FORM-UUID', 'SUBMISSION-ID');
file_put_contents('submission.pdf', $pdf);


hooks = new Paubox\PauboxWebhooks('YOUR-SCOPED-API-KEY');

$endpoint = $webhooks->createWebhookEndpoint([
    'target_url' => 'https://example.com/paubox-webhook',
    'events'     => ['forms.submission.created'],
]);

echo $endpoint->id;              // UUID
echo $endpoint->signing_secret;  // whsec_... — store this now

$result = $webhooks->listWebhookEndpoints();

foreach ($result->data as $endpoint) {
    echo "{$endpoint->id} {$endpoint->target_url} {$endpoint->status}\n";
}

echo $result->page_info->count;  // total matching, not the length of this page

$page2 = $webhooks->listWebhookEndpoints(['page' => 2, 'items' => 25]);

$endpoint = $webhooks->getWebhookEndpoint('2ec66c21-bf48-48eb-8d28-f80b2d6b77c7');

$endpoint = $webhooks->updateWebhookEndpoint($id, ['status' => 'disabled']);

$webhooks->deleteWebhookEndpoint($id);

use Paubox\Webhooks\PauboxWebhooksException;

try {
    $webhooks->createWebhookEndpoint($params);
} catch (PauboxWebhooksException $e) {
    echo $e->getStatusCode();    // 422
    echo $e->getMessage();       // createWebhookEndpoint failed: HTTP 422: target_url: ...
    echo $e->getResponseBody();  // raw response
}
bash
$ composer