当前位置: 首页 > article >正文

Thinkphp5 + Swoole实现邮箱异步通知

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

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

一、安装 Swoole

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

pecl install swoole

二、邮件发送配置

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

在项目的 config.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

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

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 命令:

return [
    'app\command\SwooleServer',
];
3. 启动 Swoole Server

使用命令行启动 Swoole Server:

php think swoole:server

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

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

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

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


http://www.kler.cn/news/305558.html

相关文章:

  • 重新认识一下JNIEnv
  • 【学习笔记】SSL密码套件的选择
  • 微信小程序-formData使用
  • VSCode C++ Tasks.json基本信息介绍
  • PDF——压缩大小的方法
  • HC-SR501人体红外传感器详解(STM32)
  • 【笔记】CCF直播:《如何在国际会议上有效交流》(2024-9-15)
  • rust解说
  • Vue介绍、窗体内操作、窗体间操作学习
  • 9.11 codeforces Div 2
  • SOME/IP通信协议在汽车业务具体示例
  • c# sqlhelper类
  • lvgl | guider应用笔记
  • java项目之网上商城系统设计与实现(源码+文档)
  • Tomcat_WebApp
  • 020、二级Java选择题综合知识点(持续更新版)
  • 树莓派Pico2(RP2350)开发环境搭建
  • Linux内核初始化过程中加载TCP/IP协议栈
  • ios xib 子控件约束置灰不能添加约束
  • 【modou网络库】Reactor架构与TCP通信机制分析
  • 基于hispark_taurus开发板示例学习OpenHarmony编译(1)
  • 记录工作中遇到的问题(持续更新~)
  • TikTok云手机解决运营效率低、封号问题
  • QT消息对话框学习
  • 用户登陆网址都发生了什么?
  • 网络原理1-传输层
  • [mysql]mysql的运算符
  • it基础软件运维管理:从操作系统到数据库,再到中间件和应用系统
  • 测试ASP.NET Core的WebApi项目调用WebService
  • 血缘解析<二>:如何解析带CTE语句的Sql