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

相关推荐
小江的记录本3 分钟前
【JVM虚拟机】JVM调优:常用JVM参数、调优核心指标、OOM排查、GC日志分析、Arthas工具使用(附《思维导图》+《面试高频考点清单》)
java·jvm·spring boot·后端·python·spring·面试
程序员cxuan15 分钟前
我花了两天时间,终于把 Codex 额度掉太快的问题整明白了!!
人工智能·后端·程序员
IT_陈寒16 分钟前
Vue这个动态响应坑把我整不会了
前端·人工智能·后端
金銀銅鐵16 分钟前
[Java] 用图形化界面演示 iadd, isub, iconst_<i> 指令的效果
java·后端·python
AskHarries28 分钟前
做国内还是出海
后端
bill328227804339 分钟前
AE 的 Animation Composer 描边动效玩法 + 极简 MG 制作流程(含示例)
php·composer
日月云棠1 小时前
10 Integer —— 最常用的整数包装类深度解析
java·后端
大鸡腿同学1 小时前
大模型为何总 “胡说八道”?做完 RAG 知识库,我看懂了它的底层逻辑
后端
一 乐1 小时前
高校实习信息发布网站|基于Spring Boot的高校实习信息发布网站的设计与实现(源码+数据库+文档)
java·数据库·spring boot·后端·论文·毕设·高校实习信息发布网站
安久11 小时前
springboot图片上传至服务器本地保存
后端