drewlabs/envoyer

Envoyer 客户端库

v0.2.5 2024-08-15 00:59 UTC

This package is auto-updated.

Last update: 2024-09-18 18:12:00 UTC


README

用于通过各种适配器/驱动程序发送文本消息、电子邮件等通知的客户端库。

用法

该库需要支持提供商(如[ Amazon AWS SES ])、[ TWILIO Sendgrid ] 驱动程序(用于通过[ Twilio Sendgrid PHP API ]发送邮件)、[SMTP] 驱动程序(用于通过SMTP连接发送邮件)、[ Twilio Message API ](用于通过Twilio发送文本消息)和 SMPP 驱动程序(用于通过SMPP连接到SMPP服务器发送消息)等驱动程序。

-- 发送电子邮件

对于简单的邮件(无附件的邮件),无需实现[Drewlabs\Contracts\Notification\AttachmentsNotification],因为它仅适用于带附件的邮件。

// Create a notification class

// Mail.php

use Drewlabs\Envoyer\Contracts\AttachmentsAware;
use Drewlabs\Envoyer\Contracts\NotificationInterface;
use SplFileInfo;

/** @package Drewlabs\Notifications\Tests\Stubs */
class Mail implements NotificationInterface, AttachmentsAware
{

    public function setAttachments($attachments = [])
    {
    }

    public function getAttachments()
    {
        return [
            new SplFileInfo(__DIR__ . '/../contents/bordereau.pdf'),
        ];
    }
    public function getCc()
    {
        return null;
    }

    public function getSubject()
    {
        return "Successful registration";
    }

    public function getSender()
    {
        return new Address;
    }

    public function getReceiver()
    {
        return "azandrewdevelopper@gmail.com";
    }

    public function getContent()
    {
        return "<p>Hey Azandrew! Thank you for your registration</p>";
    }
}

// defines a test driver to use
DriverRegistryFacade::defineDriver('test', static function () {
    return new TestDriver();
});

// Send notification using command interface
$result = $command->driver('test')->send(new Mail);
  • SMTP

-- 集成

composer require drewlabs/envoyer-smtp

-- 发送邮件

// SendMail.php
$result = $command->driver('smtp')->send(new Mail);
  • Twilio Message API

-- 安装

composer require drewlabs/envoyer-twilio

-- 发送文本消息

// SendMessage.php
$result = $command->driver('smtp')->send(new Message);
  • SMPP 服务器

-- 安装

composer require drewlabs/envoyer-smpp

-- 发送文本消息

// SendMessage.php
$result = $command->driver('smpp')->send(new Message);
  • 自定义驱动程序

自定义/第三方驱动程序的实现必须实现[Drewlabs\Envoyer\Contracts\ClientInterface]并定义发送通知的定制逻辑。

use Drewlabs\Envoyer\Contracts\ClientInterface;
use Drewlabs\Envoyer\Contracts\NotificationInterface;
use Drewlabs\Envoyer\Contracts\NotificationResult;
use RuntimeException;

class TestDriver implements ClientInterface
{

    public function __construct()
    {
    }

    public function sendRequest(NotificationInterface $instance): NotificationResult
    {
        // TODO : Provide send request implementation logic
    }
}

API

  • 邮件构建器

mail builder 类是构建邮件通知的流畅接口。

示例

// Start building the mail
$mail = \Drewlabs\Envoyer\Mail::new()
    ->from('...', '...')
    ->to('...')
    ->bCc('...')
    ->subject('...')
    ->attach(new SplFileInfo(__DIR__ . '/...'))
    ->content(require __DIR__ . '/...');

// TODO: Send the mail messge using the required driver API
  • 文本消息构建器

text message builder 是构建要作为通知实例发送的文本消息的流畅接口。

// Create a messageable class
$message = Messageable::new()
                        ->from('...')
                        ->to('...')
                        ->content("...");
    
// Use required driver to send the notification message
  • 驱动程序枚举

为了避免驱动程序名称的拼写错误,实现中包含PHP常量,可以在构建命令时使用。

use Drewlabs\Notifications\Command;
use Drewlabs\Notifications\Drivers;

$result = Command::driver(Drivers::AWS_SES)->send(/* ... */);
  • 驱动程序注册表

drivers registry 是一个单例类,允许应用程序开发者注册和创建通知驱动程序。

use Drewlabs\Envoyer\DriverRegistryFacade;

DriverRegistryFacade::defineDriver('test', static function () {
    return new TestProvider();
});

注意 传递给 defineDriver 的工厂函数/闭包必须返回 Drewlabs\Envoyer\Contracts\ClientInterface 的实例,否则驱动程序解析器将抛出异常。