noveth / notify
用于向多个平台发送消息的 PHP 通知库
0.2.5
2015-09-23 10:09 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- codeclimate/php-test-reporter: 0.1.*
- phpunit/phpunit: ~4.4
This package is not auto-updated.
Last update: 2024-10-02 10:50:21 UTC
README
通知库
描述
一个用于向多个提供商发送通知的 PHP 库。
##实现
当前
- 电子邮件
- Slack
计划
- HipChat
要求
- PHP 5.4+
- CURL
安装
使用 Composer
要使用 Composer 安装 Notify,只需将以下内容添加到您的 composer.json 文件中
{ "require": { "noveth/notify": "0.*" } }
或者运行以下命令
composer require noveth/notify
用法
配置
包括默认配置,如果未找到用户配置,则回退到该配置。要开始使用自定义配置,请将 Config.php.example 重命名为 Config.php。
电子邮件
使用 Notify 发送电子邮件非常简单,您只需传递 3 个参数,它将返回 true 或 false。
bool Notify\Email::send(string $to, string $subject, string $template [, array $replace ] );
在 send 方法中使用了 3 个必需参数,如果在使用模板进行字符串替换时,还有 1 个可选的第四个参数。
以下是参数示例
// Target email address $to = 'example@example.com'; // Subject line $subject = 'An example email'; // Template file name // You can tell Notify if its a html email or not depending on the file extension // .html files will automatically be sent as a html email $template = 'user/registration.html'; // If you use tags in your template this is where you pass what to replace them with $replace = [ 'test tag' => 'test content' ];
Slack
通知也可以发送到 Slack,但这需要比使用电子邮件更多一点的设置(别担心,这并不难)。要使用 Slack 通知,您需要首先在您的 Slack 团队设置中设置一个 webhook。
按照上面的配置步骤开始使用自定义配置,然后向您的 Config.php 文件中添加以下行。
// Replace {YOUR_ENDPOINT} with the webhook URL provided by slack. Config::$SLACKWEBHOOK = '{YOUR_ENDPOINT}'; // Replace {NAME_OF_USER} with the user you want the message to be sent as (this can be anything). Config::$SLACKUSERNAME = '{NAME_OF_USER}';
Slack 通知的发送方式与电子邮件类似,但只有一个必需参数和一个是可选的。
bool Notify\Slack::send(string $message [, string $target]);
第二个参数可以是房间或用户,这可以通过以下语法完成。如果未发送,它将使用 Slack webhook 设置中的默认房间。
$target = '#channel'; $target = '@user';