kiwilan / php-notifier
PHP Notifier 是一个用于发送邮件或 Discord 或 Slack 通知的包。
Requires
- php: ^8.1
- symfony/mailer: ^6.4 || ^7
Requires (Dev)
- guzzlehttp/guzzle: ^7.8
- laravel/pint: ^1.0
- pestphp/pest: ^2.15
- phpstan/phpstan: ^1.10
- spatie/ray: ^1.28
README
PHP Notifier 是一个用于发送邮件或 Discord 或 Slack 通知的包。
重要
此包不支持推送通知或短信(如果您感兴趣,欢迎提交 PR)。
安装
您可以通过 composer 安装此包
composer require kiwilan/php-notifier
注意
对于 Laravel,您可以使用 kiwilan/notifier-laravel
包。
用法
此包提供对 Discord 和 Slack Webhook 的支持,以及使用 symfony/mailer
的电子邮件。
- Discord:支持消息和丰富嵌入 Webhook。
- Slack:支持消息和附件 Webhook(不支持旧版 API)。
- 邮件:支持使用
symfony/mailer
的消息和附件。
Discord
您可以发送包含用户和头像的简单消息。默认用户和头像将是 Webhook 的名称和头像。
use Kiwilan\Notifier\Notifier; $notifier = new Notifier(); $discord = $notifier->discord('https://discord.com/api/webhooks/1234567890/ABCDEFGHIJKLMN0123456789') ->message('Hello, Discord!') ->user('Notifier', 'https://raw.githubusercontent.com/kiwilan/php-notifier/main/docs/banner.jpg') ->send();
您还可以发送丰富嵌入。
use Kiwilan\Notifier\Notifier; $notifier = new Notifier(); $discord = $notifier->discord($webhook) ->rich('Rich advanced') ->title('Notifier') ->user('Notifier', 'https://raw.githubusercontent.com/kiwilan/php-notifier/main/docs/banner.jpg') ->url('https://ewilan-riviere.com') ->author('Author', 'https://ewilan-riviere.com', 'https://raw.githubusercontent.com/kiwilan/php-notifier/main/docs/banner.jpg') ->color('#3498db') ->timestamp() ->fields([ ['name' => 'Field 1', 'value' => 'Value 1'], ['name' => 'Field 2', 'value' => 'Value 2'], ], inline: true) ->thumbnail('https://raw.githubusercontent.com/kiwilan/php-notifier/main/docs/banner.jpg') ->image('https://raw.githubusercontent.com/kiwilan/php-notifier/main/docs/banner.jpg') ->footer('Footer', 'https://raw.githubusercontent.com/kiwilan/php-notifier/main/docs/banner.jpg') ->send();
邮件
邮件使用 symfony/mailer
发送电子邮件。
use Kiwilan\Notifier\Notifier; $notifier = new Notifier(); $mailConfig = $notifier->mail('smtp') ->mailer('smtp') ->host('mailpit') ->port(1025) ->username(null) ->password(null) ->encryption('tls');
$mailConfig->from('hello@example.com', 'Hello') ->to('to@example.com', 'To') ->subject('Hello, Mail!') ->message('Hello, Mail!') ->html('<h1>Hello, Mail!</h1>') ->send();
注意
如果没有设置 html
,则 message
将用作 HTML 内容。如果设置了 html
但没有设置 message
,则 html
将使用 strip_tags
方法作为纯文本内容。
可以通过 to
方法添加多个收件人。
use Symfony\Component\Mime\Address; $mailConfig->from('hello@example.com', 'Hello') ->to([ new Address('to1@example.com', 'To1'), new Address('to2@example.com', 'To2'), ]) ->send();
您可以使用 addAttachment
方法添加附件。
$mailConfig->addAttachment('path/to/file.txt', 'file.txt') ->send();
Slack
您可以发送简单消息。
use Kiwilan\Notifier\Notifier; $notifier = new Notifier(); $slack = $notifier->slack('https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX') ->message('Hello, Slack!') ->send();
您还可以发送附件。
use Kiwilan\Notifier\Notifier; $notifier = new Notifier(); $slack = $notifier->slack($webhook) ->attachment('*Hello, Slack!*') ->color('#36a64f') ->pretext('Optional pre-text that appears above the attachment block') ->author('Kiwilan', 'https://github.com/kiwilan') ->title('php-notifier', 'https://github.com/kiwilan/php-notifier') ->text('Optional text that appears within the attachment') ->fields([ [ 'title' => 'Priority', 'value' => 'High', 'short' => false, ], [ 'title' => 'Priority', 'value' => 'High', 'short' => false, ], ]) ->imageUrl('https://raw.githubusercontent.com/kiwilan/php-notifier/main/docs/banner.jpg') ->footer('Slack API', 'https://raw.githubusercontent.com/kiwilan/php-notifier/main/docs/banner.jpg') ->timestamp(new DateTime()) ->send();
HTTP
您可以使用 http
方法发送 HTTP 请求。
use Kiwilan\Notifier\Notifier; $notifier = new Notifier(); $http = $notifier->http('https://jsonplaceholder.typicode.com/posts') ->method('POST') ->send(); $statusCode = $http->getStatusCode(); $body = $http->getResponseBody(); $headers = $http->getResponseHeaders();
客户端
HTTP 请求使用本机流上下文发送数据,可以使用 curl
和 guzzle
作为选项(默认为 stream
)。
警告
如果您使用 guzzle
,则需要安装 guzzlehttp/guzzle
包。
use Kiwilan\Notifier\Notifier; $notifier = new Notifier(); $stream = $notifier->client('stream') // default ->discord($webhook) ->message('Hello, Discord!') ->send(); $curl = $notifier->client('curl') // use curl instead of stream ->discord($webhook) ->message('Hello, Discord!') ->send(); $guzzle = $notifier->client('guzzle') // use guzzle instead of stream (need guzzlehttp/guzzle package) ->discord($webhook) ->message('Hello, Discord!') ->send();
要了解请求是否成功,您可以使用 isSuccess
方法。
$notifier = new Notifier(); $discord = $notifier->discord($webhook) ->message('Hello, Discord!') ->send(); if ($discord->isSuccess()) { echo 'Message sent!'; }
测试
composer test
更新日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请查看 我们的安全策略 了解如何报告安全漏洞。
致谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件。