unione/unione-php

UniOne SDK 包,提供与 UniOne API 交互的方法。


README

Latest Stable Version Total Downloads

此 SDK 包含与 UniOne API 交互的方法:[https://docs.unione.io/en/web-api-ref#web-api](https://docs.unione.io/en/web-api-ref#web-api)

安装

使用 Composer 安装此包

composer require unione/unione-php

用法

发送邮件

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  // Example for EU instance.
  $client = new Unione\UnioneClient('YOUR-API-KEY', 'eu1.unione.io');

  $recipients = [
    [
      "email" => 'john@example.com',
      "substitutions" => [
        "to_name" => "John Smith"
      ],
    ],
    [
      "email" => 'liza@example.com',
      "substitutions" => [
        "to_name" => "Liza Frank"
      ],
    ]
  ];

  $body = [
    "html" => "<b>Test mail, {{to_name}}</b>",
    "plaintext" => "Hello, {{to_name}}",
    "amp" => "<!doctype html><html amp4email><head> <meta charset=\"utf-8\"><script async src=\"https://cdn.ampproject.org/v0.js\"></script> <style amp4email-boilerplate>body[visibility:hidden]</style></head><body> Hello, AMP4EMAIL world.</body></html>"
  ];

  // You can use email object can be used to prepare the message array.
  // But the email send method accepts an array, that can be composed without
  // SDK utils.
  $mail = new Unione\Model\Email($recipients, $body);
  $mail->setFromEmail('user@example.com');
  $mail->setSubject('test letter');
  $response = $client->emails()->send($mail->toArray());

查看 API 文档 以获取更多详细信息。

查看 模板引擎文档 以了解替换细节。

发送订阅邮件

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');

  $params = [
    "from_email" => "john@example.com",
    "from_name" => "John Smith",
    "to_email" => "user@example.com"
  ];
  $response = $client->emails()->subscribe($params);

API 文档

设置模板

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');

  $params = [
    "template" => [
      "name" => "First template",
      "body" => [
        "html" => "<b>Hello, {{to_name}}</b>",
        "plaintext" => "Hello, {{to_name}}",
        "amp" => "<!doctype html><html amp4email><head> <meta charset=\"utf-8\"><script async src=\"https://cdn.ampproject.org/v0.js\"></script> <style amp4email-boilerplate>body[visibility:hidden]</style></head><body> Hello, AMP4EMAIL world.</body></html>"
      ],
      "subject" => "Test template mail",
      "from_email" => "test@example.com",
      "from_name" => "Example From",
    ]
  ];
  $response = $client->templates()->set($params);

API 文档

获取模板

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  $response = $client->templates()->get('YOUR-TEMPLATE-ID');

API 文档

获取模板列表

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');

  $params = [
    "limit" => 50,
    "offset" => 0
  ];
  $response = $client->templates()->list($params);

API 文档

删除模板

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  $response = $client->templates()->delete('YOUR-TEMPLATE-ID');

API 文档

设置 webhook

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');

  $params = [
    "url" => "https://yourhost.example.com/unione-webhook",
    "events" => [
      "email_status" => [
        "delivered",
        "opened",
        "clicked",
        "unsubscribed",
        "soft_bounced",
        "hard_bounced",
        "spam"
      ]
    ]
  ];
  $response = $client->webhooks()->set($params);

API 文档

指定的 URL 将从 Unione 接收请求。有关请求数据的更多信息,请参阅 API 文档

这是如何在回调处理程序中检查消息完整性的方法

$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
// $body contains a callback request body.
if ($client->webhooks()->verify($body) === TRUE) {
  // The webhook is confirmed, result can be processed.
}

获取 webhook

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  $response = $client->webhooks()->get('YOUR-WEBHOOK-URL');

API 文档

获取所有或部分 webhook 列表

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  $response = $client->webhooks()->list();

API 文档

删除 webhook

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  $response = $client->webhooks()->delete('YOUR-WEBHOOK-URL');

API 文档

附加信息

通用 API 方法

对于 SDK 中尚未实现的 API 方法,您可以使用 UnioneClient::httpRequest()。以下是一个“设置”抑制方法的示例

  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  $response = $client->httpRequest('suppression/set.json', ["email" => "user@example.com", "cause" => "unsubscribed"]);

设置 Guzzle HTTP 客户端配置

Unione 客户端接受一个数组,该数组包含 Guzzle 配置作为第三个参数。在创建客户端时,您可以传递一些额外的选项(例如 connect_timeout),以便将其应用于所有请求。

这是一个添加历史处理程序以保存入站请求和响应的更高级示例。

  $container = [];
  $history = Middleware::history($container);
  $handlerStack = HandlerStack::create();
  $handlerStack->push($history);
  $config = ['handler' => $handlerStack];
  $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME', $config);

查看 Guzzle 文档