定制 drewlabs/envoyer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

drewlabs/envoyer

Composer 安装命令:

composer require drewlabs/envoyer

包简介

Envoyer client library

README 文档

README

Client library for sending text message, email, etc... notification using various adapters / drivers.

Usage

The library requires drivers for supported providers like [ Amazon AWS SES ], [ TWILIO Sendgrid ] driver (for sending mail using [ Twilio Sendgrid PHP API ]), [SMTP] driver (for sending mails using an SMTP connection), [ Twilio Message API ] (For sending Text message using Twilio), and SMPP driver (for sending message using an SMPP connection to an SMPP server), etc.

-- Sending an mail

For simple mail (mail without attachment), there is no need to implements [Drewlabs\Contracts\Notification\AttachmentsNotification] as it is only required for mails with attachments.

// 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

-- Integration

composer require drewlabs/envoyer-smtp

-- Sending mail

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

-- Installation

composer require drewlabs/envoyer-twilio

-- Sending Text Message

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

-- Installation

composer require drewlabs/envoyer-smpp

-- Sending Text messages

// SendMessage.php
$result = $command->driver('smpp')->send(new Message);
  • Custom Driver

Custom / Thrird party drivers implementations must implements [Drewlabs\Envoyer\Contracts\ClientInterface] and define the custom logic to send notification.

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

The mail builder class is a fluent interface for building mail notifications.

Example:

// 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 Messages builder

The text message builder is a fluent interface for building text messages to be send as notification instance.

// Create a messageable class
$message = Messageable::new()
                        ->from('...')
                        ->to('...')
                        ->content("...");
    
// Use required driver to send the notification message
  • Driver Enum

To avoid typo error for driver names, implementation comes with PHP constant that can be use when contructing command:

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

$result = Command::driver(Drivers::AWS_SES)->send(/* ... */);
  • The Drivers registry

The drivers registry is a singleton class that allow application developper to register and create notification drivers.

use Drewlabs\Envoyer\DriverRegistryFacade;

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

Note The factory function/closure passed as parameter to defineDriver must return an instance of Drewlabs\Envoyer\Contracts\ClientInterface else the driver resolver will throw an exception.

drewlabs/envoyer 适用场景与选型建议

drewlabs/envoyer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 131 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mail」 「sms」 「notification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 drewlabs/envoyer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 drewlabs/envoyer 我们能提供哪些服务?
定制开发 / 二次开发

基于 drewlabs/envoyer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 131
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-11