shieldon / messenger
为PHP简化发送消息到第三方服务。
Requires
- php: >=7.1.0
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-09-12 04:26:02 UTC
README
为PHP简化发送消息到第三方服务。
支持的模块
- Telegram
- Line Notify
- Rocket Chat
- Slack
- Slack Webhook
- 邮件
- SMTP
- SendGrid
- MailGun
- MailGun (SMTP)
- Gmail (SMTP)
- Yahoo (SMTP)
- Outlook (SMTP)
未来将会有更多模块...
安装
使用PHP Composer
composer require shieldon/messenger
或者,下载它并包含Messenger自动加载器。
require './autoload.php';
基本用法
公共API方法
- send
- debugMode
- printResult
除了Mailer类,唯一的公共API是send()
。你只需要在初始化实例时将所需的数据字段分配给构造函数。
Telegram
打开你的Telegram应用,添加BotFather
开始新的对话。输入命令/newbot
获取你的API密钥。
请记住,确保你的频道类型是公开的。如果你想发送消息到你的私人频道,搜索一下会有解决方案。
$apiKey = 'your_api_key'; $channel = '@your_channel'; $telegram = new \Shieldon\Messenger\Telegram($apiKey, $channel); if ($telegram->send('say something!')) { echo 'Message has been sent to your Telegram group.'; } else { echo 'Failed to send message.' . "\n"; echo $telegram->printResult(); }
Line Notify
可以在注册页面点击生成token
按钮来获取访问令牌。
一旦你获得了为选择的聊天组开发者访问令牌,邀请Line Notify
机器人加入你的Line群组,然后以下代码将按预期工作。
$accessToken = 'your_access_token'; $line = new \Shieldon\Messenger\LineNotify($accessToken); if ($line->send('say something!')) { echo 'Message has been sent to your Line group.'; } else { echo 'Failed to send message.' . "\n"; echo $line->printResult(); }
RocketChat
$accessToken = 'your_auth_token'; $userId = 'your_user_id'; $serverUrl = 'https://your_rocket_chat.com:3000'; $channel = '#general'; $rocketChat = new \Shieldon\Messenger\RocketChat($accessToken, $userId, $serverUrl, $channel); if ($rocketChat->send('say something!')) { echo 'Message has been sent to your RocketChat channel.'; } else { echo 'Failed to send message.' . "\n"; echo $rocketChat->printResult(); }
Slack
请仔细阅读Slack官方的API文档以了解你需要的信息。
指南
- 创建应用
- 给你的应用分配
channels:read
和chat:write:bot
权限。 - 将你的应用分配到你的工作空间。
- 获取机器人的访问令牌。
- 将你的应用添加到你想发送消息的频道。
$botToken = 'xoxb-551837935968-920623655894-TI1zWtaDLCkTzZaFFuyfzL56'; $channel = '#general'; $slack = new \Shieldon\Messenger\Slack($botToken, $channel); if ($slack->send('say something!')) { echo 'Message has been sent to your Slack channel.'; } else { echo 'Failed to send message.' . "\n"; echo $slack->printResult(); }
Slack Webhook
这是发送消息的最简单方法。请仔细阅读Slack官方的API文档以了解你需要的信息。
$webhook = 'https://hooks.slack.com/services/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'; $slack = new \Shieldon\Messenger\SlackWebhook($webhook); if ($slack->send('say something!')) { echo 'Message has been sent to your Slack channel.'; } else { echo 'Failed to send message.' . "\n"; echo $slack->printResult(); }
ok.
Mailer使用
公共API方法
- send
- addTo
- addCc
- addBcc
- addReplyTo
- addRecipient
- setRecipients
- setSubject
- setSender
- debugMode
- printResult
使用Mailer时无需提及内容类型,内容类型会自动检测。
邮件
原生PHP邮件函数。要使用此类,请确保你在php.ini
中正确设置了设置。
$mail = new \Shieldon\Messenger\Mail(); $mail->addSender('example.sender@gmail.com'); $mail->addRecipient('example.recipient@gmail.com'); $mail->setSubject('Foo, bar.'); if ($mail->send('say something!')) { echo 'Email has been delivered via PHP\'s native mail function.'; } else { echo 'Failed to send email.' . "\n"; echo $maingun->printResult(); }
SMTP
一个非常简单的SMTP客户端。
$user = 'email@your_domain.com'; $pass = '12345678'; $host = '127.0.0.1'; $port = '25'; $mail = new \Shieldon\Messenger\Smtp($user, $pass, $host, $port); $mail->addSender('email@your_domain.com'); $mail->addRecipient('do-not-reply@gmail.com'); $mail->setSubject('Foo, bar.'); if ($mail->send('say something!')) { echo 'Email has been delivered via SMTP.'; } else { echo 'Failed to send email.' . "\n"; echo $maingun->printResult(); }
注意
如果你想使用SMTPS
或STARTTLS
,$host
应该有一个前缀。
例如
$host = 'ssl://smtp.gmail.com'; // SMTPS $host = 'tls://smtp.gmail.com'; // STARTTLS
SendGrid
如果你有SendGrid API密钥,你也可以轻松通过SendGrid发送消息。
$apiKey = 'your_api_key'; $sendgrid = new \Shieldon\Messenger\Sendgrid($apiKey); $sendgrid->addSender('example.sender@gmail.com'); $sendgrid->addRecipient('example.recipient@gmail.com'); $sendgrid->setSubject('Foo, bar.'); if ($sendgrid->send('say something!')) { echo 'Email has been delivered via SendGrid API.'; } else { echo 'Failed to send email.' . "\n"; echo $maingun->printResult(); }
MailGun
$apiKey = 'your_api_key'; $domain = 'your_domain_name'; $maingun = new \Shieldon\Messenger\Mailgun($apiKey, $domain); $maingun->addSender('example.sender@gmail.com'); $maingun->addRecipient('example.recipient@gmail.com'); $maingun->setSubject('Foo, bar.'); if ($maingun->send('say something!')) { echo 'Email has been delivered via MailGun API.'; } else { echo 'Failed to send email.' . "\n"; echo $maingun->printResult(); }
MailGun SMTP
扩展自Smtp
,一个可用的MailGun SMTP客户端。
$user = 'your@gmail.com'; $pass = 'your_password'; $maingun = new \Shieldon\Messenger\Smtp\Mailgun($user, $pass); $maingun->addSender('example.sender@gmail.com'); $maingun->addRecipient('example.recipient@gmail.com'); $maingun->setSubject('Foo, bar.'); if ($maingun->send('say something!')) { echo 'Email has been delivered via MainGun SMTP server.'; } else { echo 'Failed to send email.' . "\n"; echo $maingun->printResult(); }
Gmail
扩展自Smtp
,一个可用的Gmail SMTP客户端。
$user = 'your@gmail.com'; $pass = 'your_password'; $gmail = new \Shieldon\Messenger\Smtp\Gmail($user, $pass); $gmail->addSender('your@gmail.com'); $gmail->addRecipient('test@gmail.com'); $gmail->setSubject('Foo, bar.'); if ($gmail->send('say something!')) { echo 'Email has been delivered via Gmail SMTP server.'; } else { echo 'Failed to send email.' . "\n"; echo $gmail->printResult(); }
注意
Google不喜欢人们通过脚本使用他们的SMTP服务器发送电子邮件,为了确保它能够无问题地工作,你必须正确设置设置
- 检查你的谷歌账户 -> 访问低安全应用 -> 开启
- 使用你使用谷歌账户发送电子邮件的主机,并确认你已信任该设备。
Yahoo Mail
扩展自Smtp
,一个可用的Yahoo SMTP客户端。
$user = 'your@yahoo.com'; $pass = 'your_password'; $yahooMail = new \Shieldon\Messenger\Smtp\Yahoo($user, $pass); $yahooMail->addSender('your@yahoo.com'); $yahooMail->addRecipient('test@gmail.com'); $yahooMail->setSubject('Foo, bar.'); if ($yahooMail->send('say something!')) { echo 'Email has been delivered via Yahoo SMTP server.'; } else { echo 'Failed to send email.' . "\n"; echo $yahooMail->printResult(); }
注意:你可以使用你的账户密码,但如果你遇到以下错误
(#AUTH005) Too many bad auth attempts error when trying to send email.
这是因为在默认情况下,Yahoo可能不允许第三方产品通过SMTP服务器访问。要解决这个问题
- 前往 账户安全,
- 在 管理应用密码 部分,为该应用创建密码。
- 使用您的应用密码而不是账户密码。
Outlook 邮件(Office365)
扩展自Smtp
,一个可用的Yahoo SMTP客户端。
$user = 'your@outlook.com'; $pass = 'your_password'; $outlook = new \Shieldon\Messenger\Smtp\Outlook($user, $pass); $outlook->addSender('your@outlook.com'); $outlook->addRecipient('test@gmail.com'); $outlook->setSubject('Foo, bar.'); if ($outlook->send('say something!')) { echo 'Email has been delivered via Office365 SMTP server.'; } else { echo 'Failed to send email.' . "\n"; echo $yahooMail->printResult(); }
注意
首次通过 Office365 SMTP 服务器发送邮件时,您将收到来自 Outlook.com 团队 的通知邮件以确认您的操作。
完成验证后,您将能够通过 Office365 SMTP 服务器发送邮件。
调试
debugMode()
如果您想捕获异常,请启用 debugMode
。当发生错误时,此选项将抛出异常。
例如
$mail = new \Shieldon\Messenger\Smtp($user, $pass, $host, $port); $mail->debugMode(true); $mail->addSender('email@your_domain.com'); $mail->addRecipient('do-not-reply@gmail.com'); $mail->setSubject('Foo, bar.'); try { $mail->send('say something!'); } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; }
printResult()
如果您想打印执行结果,可以使用 printResult()
。
例如
$mail = new \Shieldon\Messenger\Smtp($user, $pass, $host, $port); $mail->addSender('email@your_domain.com'); $mail->addRecipient('do-not-reply@gmail.com'); $mail->setSubject('Foo, bar.'); $mail->send('say something!'); echo $mail->printResult();
如果邮件发送成功,结果将如下所示
success: true
message: Email is sent.
--- result ---
connection: 220 smtp.gmail.com ESMTP x11sm6715821pfn.53 - gsmtp
hello: 250 smtp.gmail.com at your service
auth_type: 334 VXNlcm5hbWU6
user: 334 UGFzc3dvcmQ6
pass: 235 2.7.0 Accepted
from: 250 2.1.0 OK x11sm6715821pfn.53 - gsmtp
to: 250 2.1.5 OK x11sm6715821pfn.53 - gsmtp
cc: 250 2.1.5 OK x11sm6715821pfn.53 - gsmtp
bcc: 250 2.1.5 OK x11sm6715821pfn.53 - gsmtp
data: 354 Go ahead x11sm6715821pfn.53 - gsmtp
send: 250 2.0.0 OK 1579887885 x11sm6715821pfn.53 - gsmtp
quit: 221 2.0.0 closing connection x11sm6715821pfn.53 - gsmtp
作者
消息传递库由来自台湾的 Terry L. 提供。
Shieldon 消息传递最初是为 Shieldon 防火墙的一部分而设计的,当其网络应用可能遭受攻击时,向网站管理员或开发者发送通知。如果您正在寻找一个用于保护网站的网络安全库,Shieldon 防火墙 可能是您的良好选择。
许可证
MIT