主题
08 通知(短信 / 邮件)
架构
业务代码 → NotifyService::sendMail() / sendSms()
→ 读取 kd_configs mail/sms 分组
→ 按 driver 字段选择驱动实例
→ 驱动 send()失败抛 NotifyException;业务钩子可用 sendMailQuiet() 静默失败。
内置驱动
| 类型 | driver | 类 |
|---|---|---|
| 邮件 | smtp | SmtpMailDriver(PHPMailer) |
| 短信 | aliyun | AliyunSmsDriver |
| 短信 | smsbao | SmsbaoDriver |
插件驱动
安装 sms / mail 插件后,在 系统设置 → 通知 中选择驱动名(与插件 name() 一致):
| 插件 | driver |
|---|---|
| 腾讯云短信 | tencent |
| 云片短信 | yunpian |
| SendCloud | sendcloud |
| SUBMAIL | submail |
加载顺序:NotifyService 内置 → extend/notify/ → public/plugins/sms|mail/ 已安装插件
开发 sms 插件
php
namespace sms\my_sms;
use app\common\lib\SmsPlugin;
use app\common\notify\NotifyException;
class MySms extends SmsPlugin
{
public function name(): string { return 'my_sms'; }
public function title(): string { return '我的短信'; }
public function configFields(): array
{
return [
['name' => 'api_key', 'label' => '密钥', 'type' => 'password', 'required' => true],
];
}
public function send(array $config, string $phone, string $content, array $params = []): void
{
// 失败 throw new NotifyException('消息', 'ERROR_CODE');
}
}开发 mail 插件
实现 MailDriverInterface::send($config, $to, $subject, $html),继承 MailPlugin。
extend 目录扩展(可选)
不经过插件系统,也可在:
extend/notify/sms/MySmsDriver.php → namespace notify\sms; class MySmsDriver
extend/notify/mail/MyMailDriver.php实现对应 Interface 即可自动加载。
配置存储
kd_configs 表,group = mail 或 sms,含 driver 及各驱动字段。
下一章:09-开通模块