Backend Development 5 min read

Using ThinkPHP 6 for Email Queue Processing

This article explains how to set up and use ThinkPHP 6’s email queue functionality, covering Redis installation, queue configuration, creating a SendMailJob class, pushing jobs to the queue, and running the queue listener to improve email sending performance and reliability.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using ThinkPHP 6 for Email Queue Processing

As web applications grow, the demand for sending emails increases, and using a queue can improve performance and reliability. ThinkPHP 6 provides convenient email sending and queue handling features, which this guide demonstrates step by step.

1. Install and configure the queue service

1. Install Redis

Redis is an open‑source in‑memory data store used as a database, cache, and message broker. Because queue data must be persisted, Redis is used to store the queue.

2. Configure the queue connection

Edit config/queue.php to set the default driver to Redis and define the connection parameters:

return [
    // 默认驱动
    'default' => env('queue.driver', 'redis'),
    // 队列连接参数
    'connections' => [
        'sync' => [
            'driver' => 'sync',
        ],
        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('queue.redis.queue', 'default'),
            'retry_after' => 90,
            'block_for' => null,
        ],
    ],
    // 监听的任务类命名空间
    'queue_class' => [
        // 'AppJobs'
    ],
];

3. Start the queue listener

Run the following command in the terminal to start listening for queued jobs:

php think queue:listen

2. Use the queue to send emails

1. Create an email‑sending job

Create a job class in app/job that contains the logic for sending an email. Example SendMailJob :

<?php
namespace app\job;

use app\common\Mail;
use think\queue\Job;

class SendMailJob
{
    /**
     * Send the email message.
     *
     * @param Job $job
     * @param array $data email message data
     */
    public function fire(Job $job, $data)
    {
        try {
            // 发送邮件
            Mail::send($data['to'], $data['subject'], $data['content']);
            // 执行任务成功,删除任务
            $job->delete();
        } catch (Exception $e) {
            // 执行任务失败,重新放入任务队列中
            // 系统会自动新建一个可重试任务并放入队列,该任务结束后重新尝试执行当前任务
            $job->release(); // 或者 $job->failed();
        }
    }
}

2. Add the job to the queue

Wherever an email needs to be sent, push a new SendMailJob instance onto the queue:

use think\facade\Queue;

// 添加一条SendMailJob任务到队列中
Queue::push(new SendMailJob($to, $subject, $content));

The variables $to , $subject , and $content represent the recipient, email subject, and body respectively.

3. Queue listener executes the task

After the listener is started, it automatically retrieves jobs from the queue and runs them. Successful execution removes the job; failures cause the job to be released back into the queue for retry until it succeeds or reaches the maximum retry limit configured in the .env file.

Conclusion

This guide has shown how to use ThinkPHP 6 for email queue processing, including installing Redis, configuring the queue, creating a mail‑sending job, pushing jobs to the queue, and running the listener to achieve better performance and reliability.

backendredisphpThinkPHPEmail Queue
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.