Optimizing Email Sending with PHP Asynchronous Coroutines
This article explains how to use PHP asynchronous coroutines and Swoole to concurrently send large volumes of email, improving speed, stability, and resource usage, and includes a complete code example with PHPMailer integration.
In modern web applications, email sending is essential for tasks such as user registration verification, order confirmation, and password reset, but traditional synchronous sending becomes inefficient and unstable when handling large volumes.
This article details how to optimize email sending using PHP asynchronous coroutines, providing a concrete code example.
1. Introduction to PHP Asynchronous Coroutines
PHP asynchronous coroutines leverage an event‑loop mechanism to execute multiple tasks concurrently, achieving higher efficiency without the heavy resource overhead of multi‑threading or multi‑processing.
2. Principles of Optimizing Email Sending
Traditional synchronous email sending processes one email at a time, causing long delays and high server load when sending many messages.
By encapsulating each email send operation in an asynchronous coroutine, multiple emails can be dispatched concurrently, eliminating wait times and improving overall stability.
3. Code Example for Sending Email with PHP Asynchronous Coroutines
The following code demonstrates how to send emails asynchronously using Swoole coroutines and PHPMailer:
use SwooleCoroutine;
use SwooleCoroutineChannel;
use PHPMailerPHPMailerPHPMailer;
function sendMail($to, $subject, $body)
{
go(function () use ($to, $subject, $body) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('[email protected]');
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $body;
if ($mail->send()) {
echo "发送成功
";
} else {
echo "发送失败:" . $mail->ErrorInfo . "
";
}
});
}
$channel = new Channel();
go(function () use ($channel) {
for ($i = 1; $i <= 100; $i++) {
$channel->push(["[email protected]", "测试邮件{$i}", "这是一封测试邮件"]);
}
$channel->close();
});
go(function () use ($channel) {
while ($data = $channel->pop()) {
sendMail($data[0], $data[1], $data[2]);
}
});
Coroutine::create(function () {
Coroutine::sleep(1); // 等待所有邮件发送完成
swoole_event_exit(); // 退出事件循环
});The code first defines a sendMail function that uses the PHPMailer library to send an email; the function is executed inside a coroutine to avoid blocking.
A Channel is created to queue email data, and separate coroutines push email tasks into the channel and consume them for sending.
Finally, a coroutine waits for all emails to be sent and then exits the event loop.
4. Conclusion
Using PHP asynchronous coroutines significantly improves email sending speed and stability, reduces server resource consumption, and enhances overall application performance and responsiveness.
Developers can adapt the provided example to their specific requirements for efficient email delivery.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.