Thinkphp5 + Swoole实现邮箱异步通知

在 ThinkPHP 中实现邮箱异步通知的常见做法是通过队列系统来处理异步任务,结合 Swoole 来处理异步发送邮件的请求。这样可以避免同步处理邮件发送导致的阻塞,提高响应速度。

以下是基于 ThinkPHP5 框架和 Swoole 的异步邮件通知实现步骤:

一、安装 Swoole

首先,你需要确保 Swoole 已经正确安装,可以通过 Composer 安装:

bash 复制代码
pecl install swoole

二、邮件发送配置

和 Redis 队列的方案一样,我们需要先配置邮件发送,还是使用 PHPMailer 或者其他的邮件库来发送邮件。

在项目的 config.php 中配置邮件相关信息:

php 复制代码
return [
    'email' => [
        'host' => 'smtp.example.com',
        'username' => 'your-email@example.com',
        'password' => 'your-password',
        'port' => 465,
        'from' => 'your-email@example.com',
        'from_name' => 'Your Name',
    ],
];

三、创建邮件服务类

邮件服务类 MailService 负责处理邮件发送的逻辑。这里使用 PHPMailer 作为邮件发送工具。

application/common/service/MailService.php:

php 复制代码
<?php

namespace app\common\service;

use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Config;

class MailService
{
    public static function sendMail($to, $subject, $body)
    {
        $mail = new PHPMailer(true);
        try {
            $mail->isSMTP();
            $mail->Host       = Config::get('email.host');
            $mail->SMTPAuth   = true;
            $mail->Username   = Config::get('email.username');
            $mail->Password   = Config::get('email.password');
            $mail->SMTPSecure = 'ssl'; 
            $mail->Port       = Config::get('email.port');

            $mail->setFrom(Config::get('email.from'), Config::get('email.from_name'));
            $mail->addAddress($to);

            $mail->isHTML(true);
            $mail->Subject = $subject;
            $mail->Body    = $body;

            $mail->send();
            return true;
        } catch (\Exception $e) {
            return $mail->ErrorInfo;
        }
    }
}

四、使用 Swoole 实现异步任务

1. 创建 Swoole Server

在项目的 command 目录下创建 SwooleServer.php,用于处理 Swoole 的服务和任务。

php 复制代码
<?php

namespace app\command;

use Swoole\Server;
use app\common\service\MailService;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class SwooleServer extends Command
{
    protected function configure()
    {
        $this->setName('swoole:server')->setDescription('Start Swoole Server');
    }

    protected function execute(Input $input, Output $output)
    {
        $server = new Server("127.0.0.1", 9501);

        // 设置 Swoole 的配置
        $server->set([
            'worker_num' => 4,
            'task_worker_num' => 4,
        ]);

        // 当有客户端连接时触发
        $server->on('receive', function (Server $server, $fd, $reactor_id, $data) {
            // 处理客户端发送的任务数据
            $taskData = json_decode($data, true);

            if ($taskData && isset($taskData['to'], $taskData['subject'], $taskData['body'])) {
                // 投递任务给 Task Worker
                $server->task($taskData);
            }
            $server->send($fd, "Mail task received.");
        });

        // 处理异步任务
        $server->on('task', function (Server $server, $task_id, $from_worker_id, $data) {
            // 使用 MailService 发送邮件
            $result = MailService::sendMail($data['to'], $data['subject'], $data['body']);

            // 返回任务处理结果
            $server->finish($result);
        });

        // 任务处理完成时触发
        $server->on('finish', function (Server $server, $task_id, $data) {
            // 任务完成后的逻辑
        });

        $output->writeln('Swoole Server started.');

        // 启动 Swoole Server
        $server->start();
    }
}
2. 注册 Swoole 命令

在 application/command.php 中注册 SwooleServer 命令:

php 复制代码
return [
    'app\command\SwooleServer',
];
3. 启动 Swoole Server

使用命令行启动 Swoole Server:

php 复制代码
php think swoole:server

Swoole 服务器启动后,它将监听 127.0.0.1:9501,客户端可以通过这个端口向服务器发送任务请求。

五、在控制器中使用 Swoole 异步发送邮件

现在我们可以在控制器中通过向 Swoole Server 发送请求来处理异步邮件通知。

application/controller/UserController.php:

php 复制代码
<?php

namespace app\controller;

use Swoole\Client;
use think\Controller;

class UserController extends Controller
{
    public function register()
    {
        // 用户注册逻辑
        $email = 'user@example.com';
        $subject = '欢迎注册';
        $body = '感谢您注册我们的网站!';

        // 创建一个 Swoole 客户端
        $client = new Client(SWOOLE_SOCK_TCP);

        // 连接到 Swoole Server
        if ($client->connect('127.0.0.1', 9501, 0.5)) {
            // 发送任务数据到 Swoole Server
            $client->send(json_encode([
                'to' => $email,
                'subject' => $subject,
                'body' => $body
            ]));

            // 接收 Swoole Server 的反馈
            $response = $client->recv();
            $client->close();

            return json(['message' => $response]);
        } else {
            return json(['error' => 'Unable to connect to Swoole Server']);
        }
    }
}

六、总结

通过 Swoole 实现的异步任务处理机制,可以将耗时操作如发送邮件等操作放入后台异步执行,提升用户体验和系统性能。Swoole 的 Task 机制非常适合处理这种场景,结合 ThinkPHP 使得开发异步任务更加简单高效。

相关推荐
FungLeo35 分钟前
FengCMS · PHP+MySQL 升级实录· 依赖生命周期与来源核验:jQuery 4.0 迁移 + SHA-256 核验脚本
mysql·php·jquery·依赖管理·fengcms
kitsch0x971 小时前
MITRE Engage 中的网络欺骗
安全·web安全·php
FungLeo1 小时前
FengCMS · PHP+MySQL 升级实录 · CI 落地:PHP 8.4/8.5 矩阵、隔离凭证、诊断门槛失败
mysql·ci/cd·php·持续集成·fengcms
码途行者9 小时前
每天10分钟学Go:第17课 接口interface-只规定“能做什么“,不管它是什么
开发语言·后端·golang
geovindu12 小时前
python:DecisionTree
开发语言·后端·python·算法
Cosolar12 小时前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录
java·后端·面试
geovindu13 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
码事漫谈13 小时前
黄仁勋的来时路
后端
bai27314 小时前
path.join('C:\a', '\b') 给我 C:\a\b,path.resolve 给 C:\b:Windows 上 51 行路径实测
javascript·后端
hackhase14 小时前
Django 多表数据整合:7 张异构表如何收敛成 4 类页面
后端·mysql