unisender/unigo-php

UniGo SDK 包,提供与 Unisender Go API 交互的方法。

1.0.1 2023-12-22 09:44 UTC

This package is not auto-updated.

Last update: 2024-09-13 12:57:07 UTC


README

Latest Stable Version Total Downloads

此 SDK 包含用于轻松与 Unisender Go API 交互的方法:https://godocs.unisender.ru/web-api-ref#web-api

安装

使用 Composer 安装此包

composer require unisender/unigo-php

用法

发送邮件

  $client = new Unisender\UniGoClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  // Example for GO1 server.
  $client = new Unisender\UniGoClient('YOUR-API-KEY', 'go1.unisender.ru');

  $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 Unisender\Model\Email($recipients, $body);
  $mail->setFromEmail('user@example.com');
  $mail->setSubject('test letter');
  $response = $client->emails()->send($mail->toArray());

查看API 文档获取更多详情。

查看模板引擎文档获取替换详情。

发送订阅邮件

  $client = new Unisender\UniGoClient('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 Unisender\UniGoClient('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 Unisender\UniGoClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  $response = $client->templates()->get('YOUR-TEMPLATE-ID');

API 文档

获取模板列表

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

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

API 文档

删除模板

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

API 文档

设置 webhook

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

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

API 文档

指定的 URL 将收到 Unisender Go 的请求。有关请求数据的更多信息,请参阅 API 文档

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


$client = new Unisender\UniGoClient('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 Unisender\UniGoClient('YOUR-API-KEY', 'YOUR-HOST-NAME');
  $response = $client->webhooks()->get('YOUR-WEBHOOK-URL');

API 文档

获取所有或部分 webhook 列表

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

API 文档

删除 webhook

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

API 文档

附加信息

通用 API 方法

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

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

设置 Guzzle HTTP 客户端配置

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

以下是一个更高级的示例,用于添加历史处理器以保存传出请求和响应。

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

请参阅Guzzle 文档