跳到正文

SDK

PHP SDK

适用于 PHP 8.1 及更高版本的 deveryidea/voaaro 0.4.0:使用 curl 或任意 PSR-18 客户端,调用方式与 resend-php 相同,内置重试和分页迭代器。

安装

Shell
composer require deveryidea/voaaro

版本 0.4.0。需要 PHP 8.1 或更高版本以及 ext-json。没有必需的依赖:如果已安装 ext-curl 则会使用它,否则请传入一个 PSR-18 客户端(Guzzle、Symfony HttpClient 等)。

配置

PHP
use Voaaro\Voaaro;

$voaaro = Voaaro::client(getenv('VOAARO_API_KEY'), baseUrl: 'https://mail.example.com', maxRetries: 2);

参数:apiKey、baseUrl、transport(PSR-18 客户端)、headers、timeout(默认 30 秒)、maxRetries(默认 2)、sleep(两次尝试之间的等待,用于加快测试)。未提供密钥或地址时,会读取 VOAARO_API_KEY 和 VOAARO_BASE_URL。

发送

PHP
use Voaaro\Attachment;

$email = $voaaro->emails->send(
    [
        'from' => 'Acme <notifications@example.com>',
        'to' => ['client@example.org'],
        'subject' => 'Welcome',
        'html' => '<p>Your account is ready.</p>',
        'attachments' => [Attachment::fromPath('/path/guide.pdf')],
    ],
    ['idempotencyKey' => 'welcome-42'],
);
echo $email->id;

Attachment::fromPath() 读取本地文件;content 还接受 base64、字节、流或 SplFileInfo(使用 Attachment::fromContent() 可强制按原始字节处理)。

重试与幂等性

  • 临时性错误最多重试 maxRetries 次(默认 2 次):网络错误(TransporterException)、429 rate_limit_exceeded、500、502、503、504。daily_quota_exceeded 和其他 4xx 错误从不重试。等待时间:以 Retry-After 为准(最多 60 秒),否则依次为 0.5 秒、1 秒、2 秒……并加入随机抖动,最多 8 秒。
  • 所有创建操作都接受选项 ['idempotencyKey' => …]。如果未提供,SDK 会为每个 POST 生成一个(UUID v4),并在各次尝试之间保持不变。
  • maxRetries: 0:不重试,也不生成键。
PHP
$voaaro->contacts->create(['email' => 'ada@example.org'], ['idempotencyKey' => 'signup-42']);
$noRetry = Voaaro::client($key, baseUrl: $url, maxRetries: 0);

分页

每个基于游标的列表都有一个迭代器:它是一个由 Resource 组成的 Generator,按每页 100 条(或 limit)请求数据,只要 has_more 为真就沿着 after 继续。

PHP
foreach ($voaaro->contacts->all(['segmentId' => $segmentId, 'unsubscribed' => false]) as $contact) {
    echo $contact->email, "\n";
}

$all = iterator_to_array($voaaro->suppressions->all(['reason' => 'bounce']), false);
列表迭代器
X->list($options)X->all($options):emails、emails->receiving、apiKeys、domains、contacts、contacts->imports、segments、audiences、topics、contactProperties、broadcasts、forms、newsletterTemplates、automations、webhooks、suppressions
contacts->segments->list($contact)、audiences->contacts->list($id)、webhooks->deliveries->list($id)->all($contact 或 $id, $options)
segments->contacts($id)、forms->signups($id)、automations->runs($id)、emails->events()allContacts($id)、allSignups($id)、allRuns($id)、allEvents()
任意路由$voaaro->paginate('/path', $filters)

列表的筛选条件会生效;after 设定起点;不接受 before。

其他功能

PHP
$voaaro->batch->send([...], ['batchValidation' => 'permissive']);
$voaaro->emails->get($id);
$voaaro->emails->list(['limit' => 20]);
$voaaro->emails->update($id, ['scheduledAt' => 'in 2 hours']);
$voaaro->emails->cancel($id);
$voaaro->emails->events(['type' => 'bounced', 'createdAfter' => '2026-11-01T00:00:00Z']);
$voaaro->emails->receiving->list(['limit' => 10]); // personal key with receiving
$voaaro->domains->create(['name' => 'notify.example.com']);
$voaaro->suppressions->add(['email' => 'former.customer@example.org', 'domain' => 'example.com']);
$voaaro->apiKeys->create(['name' => 'CI', 'permission' => 'sending_access']);
$voaaro->contacts->create(['email' => 'ada@example.org', 'firstName' => 'Ada']);
$voaaro->broadcasts->create(['segmentId' => $segmentId, 'from' => $from, 'subject' => $subject, 'html' => $html, 'send' => true]);

资源:emails(含 attachments 和 receiving)、batch、apiKeys、domains、suppressions、contacts、segments、audiences、topics、contactProperties、broadcasts、newsletter、forms、newsletterTemplates、automations、events、webhooks(含 deliveries)、organizations(运营方密钥 mko_…)。

新闻通讯

PHP
$voaaro->newsletter->updateSettings(['from' => 'Acme <news@example.com>', 'postalAddress' => "Acme\n12 Lilac Street\nSpringfield"]);
$overview = $voaaro->newsletter->overview(['days' => 30]);

$form = $voaaro->forms->create(['name' => 'Website newsletter', 'doubleOptIn' => true, 'segmentIds' => [$segmentId]]);
$voaaro->segments->preview(['match' => 'all', 'conditions' => [['field' => 'opened', 'op' => 'none', 'days' => 90]]]);

$voaaro->broadcasts->links($broadcastId);
$voaaro->broadcasts->chooseWinner($broadcastId, 'b');
file_put_contents('broadcast.csv', $voaaro->broadcasts->export($broadcastId));

$template = $voaaro->newsletterTemplates->create(['name' => 'Monthly letter', 'subject' => "What's new", 'html' => $html]);
$voaaro->automations->create(['name' => 'After purchase', 'trigger' => ['type' => 'event', 'event' => 'purchase'], 'steps' => $steps, 'status' => 'active']);
$voaaro->events->send(['email' => 'ada@example.org', 'name' => 'purchase', 'data' => ['total' => 42]]);
$voaaro->contacts->cleanup(['days' => 180]); // simulation; 'dryRun' => false to act

嵌套的键(trigger、steps、fields、ab)与 API 一样采用 snake_case;顶层的键可以用 camelCase 或 snake_case。当密钥覆盖多个域名时,用 domain 选项指定域名。路由详情:新闻通讯。

从您的应用进行订阅和退订(服务器端:newsletter->subscribe()、unsubscribe()、getSubscription();浏览器端:PublicForm、PublicForm::solveChallenge),以及营销邮件定时发送(schedule、unschedule、sendNow、listScheduled、check):请参见指南您自己的订阅表单、从您的应用退订和定时发送营销邮件,其中提供了每种 SDK 的示例。

联系人质量与送达率:验证地址但不保存(contacts->verifyEmail())或重新验证联系人的地址(contacts->reverify()),按参与度和地址状态筛选(engagement、emailStatus),导出 CSV(contacts->export()),导入时排除无效地址(skipInvalid),以及营销邮件的送达率评分(broadcasts->deliverability()):请参见联系人、分组和主题和营销邮件,其中提供了每种 SDK 的示例。

错误

PHP
use Voaaro\Exceptions\ErrorException;

try {
    $voaaro->emails->send($params);
} catch (ErrorException $e) {
    echo $e->getStatusCode(), ' ', $e->getErrorName(), ': ', $e->getMessage();
    $e->getRetryAfter(); // seconds from Retry-After, or null
}

Webhook

PHP
use Voaaro\Webhook;

$event = Webhook::verify(file_get_contents('php://input'), getallheaders(), getenv('VOAARO_WEBHOOK_SECRET'));

如果签名错误或已过期(5 分钟),会抛出 WebhookVerificationException。多个签名密钥:传入数组 [新密钥, 旧密钥] 代替单个签名密钥(空值会被忽略);只要其中一个与请求头中的某个 v1 匹配,通知即被接受。完整示例和轮换:签名。

管理端点(详情):

PHP
$hook = $voaaro->webhooks->create(['endpoint' => 'https://app.example.com/webhooks/voaaro', 'events' => ['email.bounced']]);
$secret = $hook->signing_secret; // shown only once
$voaaro->webhooks->rotateSecret($hook->id, ['graceHours' => 24]);
foreach ($voaaro->webhooks->deliveries->all($hook->id, ['status' => 'failed']) as $d) {
    $voaaro->webhooks->deliveries->replay($hook->id, $d->id);
}
Esc