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 使得开发异步任务更加简单高效。

相关推荐
小徐不徐说2 小时前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ZhengEnCi3 小时前
模型训练完整流程:从数据准备到模型上线的 10 个关键步骤(全文可跑)
后端·算法
王中阳Go3 小时前
后端转 Agent 岗面试怎么答:8 个高频问题与应答口径
人工智能·后端·python·大语言模型
程序猿DD3 小时前
OctaFuse Gateway 2.18.0:MCP 接入支持 OAuth,供应商粘性绑定可视化管理
后端
ZhengEnCi5 小时前
Agent 规则管理:让 Memory 做索引、让 Skill 存规则——上下文上限实测与分工方案
后端
nice_lcj5205 小时前
深入理解 Java 反射:从 Class 对象到方法调用(含性能实测 + 迷你 IOC 实战 + 面试考点)
java·开发语言·后端·面试
骑着蜗牛撵大象3276 小时前
2026 后端 AI 工程化:Spring AI 管调用、MCP 管工具、Agent 内嵌管自治
java·人工智能·后端·spring·ai·可观测性·mcp
摸爬滚打的小李6 小时前
Spring XML + WAR + Tomcat 项目启动
后端
IT_陈寒7 小时前
SpringBoot自动配置的坑,这次终于爬出来了
前端·人工智能·后端
RainbowFish7 小时前
# RAG 与智能体记忆:从原理到工程实践 > 一篇给学习者的总结。
后端