postalsys/emailengine-php

使用 EmailEngine.app 发送和接收电子邮件

v1.2.0 2022-04-13 12:10 UTC

This package is auto-updated.

Last update: 2024-09-13 17:55:51 UTC


README

EmailEngine提供的辅助库,EmailEngine 是一个应用程序,可以访问任何电子邮件账户,使用易于使用的 REST API 接收和发送电子邮件。

用法

加载和初始化类

use EmailEnginePhp\EmailEngine;

$ee = new EmailEngine(array(
    "access_token" => "3eb50ef80efb67885afb43844df8ae01e4eecb99c4defac3aa37ec5b8b4f1339",
    "service_secret" => "a23da152f5b88543f52420a0de0e0eb6",
    "ee_base_url" => "http://127.0.0.1:3000/",
    "redirect_url" => "http://127.0.0.1:5000/handler.php",
));

位置

  • access_token 是一个有效的访问令牌,用于验证 API 请求
  • service_secret 是共享密钥值,在服务配置页面设置(用于签名公开 URL)
  • ee_base_url 是 EmailEngine 的基本 URL(例如,不带任何路径参数的方案、主机和端口)
  • redirect_url 是托管身份验证表单的默认重定向 URL。添加账户后,用户将被重定向到该 URL。此外,将 accountstate 参数作为查询参数添加到该 URL。

托管身份验证

生成身份验证 URL。如果您将 null 设置为账户 ID,那么 EmailEngine 将生成一个新的唯一 ID,否则将使用您指定的任何内容。如果该账户已存在,则配置将被更新。

$auth_url = $ee->get_authentication_url(array("account" => null));
header('Location: ' . $auth_url);

获取 webhook 设置

$webhook_settings = $ee->get_webhook_settings();
echo "Webhooks are " . ($webhook_settings["enabled"] ? "enabled" : "disabled") . "\n";
echo "Webhooks URL is " . $webhook_settings["url"] . "\n";

设置 webhook 设置

您可以设置 webhook 设置的所有或部分设置值

$ee->set_webhook_settings(array(
    // are webhooks enabled or not
    "enabled" => true,
    // The URL webhooks are POSTed to
    "url" => "http://127.0.0.1:5000/webhooks.php",
    // Webhooks events to listen for, "*" means all events
    "events" => array("*"),
    // Additional message headers to include, "*" means all headers. Case insensitive.
    "headers" => array("Received", "List-ID"),
    // How many bytes of text content to include in the payload. Set to 0 or false to disable
    "text" => 1024 * 1024,
));

制作 API 请求

类公开了一个辅助方法,可以对 EmailEngine 进行 API 请求。

$response = $ee->request($method, $path, $payload = false);

位置

  • $method"get""post""put""delete" 之一
  • $path 是端点路径,例如 "/v1/stats"。如果需要,此路径还应包括任何查询参数,例如 "/v1/settings?proxyUrl=true"
  • $payloadPOSTPUT 请求的有效负载数组。例如 array("proxyUrl" => "socks://proxy.example.com:1080")

示例

注册新账户
// Register a new account
$account_response = $ee->request('post', '/v1/account', array(
    'account' => "example", // or null if you want it to be autogenerated by EmailEngine
    'name' => 'Andris Reinman',
    'email' => 'andris@ekiri.ee',
    'imap' => array(
        'auth' => array(
            'user' => 'andris',
            'pass' => 'secretvalue',
        ),
        'host' => 'turvaline.ekiri.ee',
        'port' => 993,
        'secure' => true,
    ),
    'smtp' => array(
        'auth' => array(
            'user' => 'andris',
            'pass' => 'secretvalue',
        ),
        'host' => 'turvaline.ekiri.ee',
        'port' => 465,
        'secure' => true,
    ),
));
等待账户连接
$account_id = "example";
$account_connected = false;
while (!$account_connected) {
    sleep(1);
    $account_info = $ee->request('get', "/v1/account/$account_id");
    if ($account_info["state"] == "connected") {
        $account_connected = true;
        echo "Account $account_id is connected\n";
    } else {
        echo "Account $account_id is ${account_info['state']}...\n";
    }
}
发送电子邮件
$account_id = "example";
$submit_response = $ee->request('post', "/v1/account/$account_id/submit", array(
    "from" => array(
        "name" => "Andris Reinman",
        "address" => "andris@ekiri.ee",
    ),
    "to" => array(
        array(
            "name" => "Ethereal",
            "address" => "andris@ethereal.email",
        ))
    ,
    "subject" => "Test message",
    "text" => "Hello from myself!",
    "html" => "<p>Hello from myself!</p>",
));

如果发送成功,则发送的消息也将上传到已发送邮件文件夹。

下载附件
$account_id = "example";
$attachment_id = "AAAAAQAABRQ";
$ee->download("/v1/account/$account_id/attachment/$attachment_id");