You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.5异步发送邮件咨询:如何实现邮件快速发送?

How to Implement Async Email Sending or Speed Up Email Delivery in Laravel

Great question! Synchronous email sending can slow down your application's response time since it waits for the email to be fully processed before responding to the user. Let's walk through the most effective ways to make this async and faster, tailored to your existing Laravel setup.

1. Use Laravel Queues for Async Sending

This is the standard approach to offload email tasks to background processes, so your app can immediately return a response to users while emails are sent in the background. There are two straightforward ways to set this up:

Option A: Make Your Mailable Queueable (No Changes to Call Code)

Simply update your DemoMail class to implement the ShouldQueue interface. Laravel will automatically queue the email instead of sending it synchronously whenever you call send().

First, configure your queue driver (we'll use the database driver as an easy-to-set-up example):

  1. Update your .env file to set the queue connection:
    QUEUE_CONNECTION=database
    
  2. Create the queue jobs table and run migrations:
    php artisan queue:table
    php artisan migrate
    

Then modify your DemoMail class:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class DemoMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    // Keep your existing class properties and constructor (if any)

    public function build()
    {
        return $this->view('emails.demo');
    }
}

Now your original sending code works exactly as before, but runs asynchronously:

$email = Auth::user()->email;
Mail::to($email)->send(new DemoMail());

Don't forget to start the queue worker to process the queued emails:

php artisan queue:work

For production environments, use a process manager like Supervisor to keep the worker running permanently (so it doesn't stop if your server restarts).

Option B: Use queue() or later() Directly

If you only want specific emails to be async (without modifying the mailable class), replace the send() method with queue():

$email = Auth::user()->email;
Mail::to($email)->queue(new DemoMail());

To delay sending (e.g., send the email 10 minutes later):

use Carbon\Carbon;

$email = Auth::user()->email;
Mail::to($email)->later(Carbon::now()->addMinutes(10), new DemoMail());

2. Switch to a Third-Party Email Service

Local SMTP or sendmail is slow and unreliable. Using a service like Mailgun, SendGrid, or Postmark will drastically speed up email delivery—even if you send synchronously. These services use API-based sending, which is far faster than establishing repeated SMTP connections.

Configure it in your .env file (example with Mailgun):

MAIL_MAILER=mailgun
MAILGUN_DOMAIN=your-mailgun-domain.com
MAILGUN_SECRET=your-mailgun-api-key
MAIL_FROM_ADDRESS=your-sender-email@example.com
MAIL_FROM_NAME="Your App Name"

Combine this with queues for the best performance: your app will fire off a fast API request to the service, and the service handles the actual email delivery at scale.

3. Additional Small Optimizations

  • Cache Email Views: Pre-render and cache your email views to save time during sending. Run this command during deployment:
    php artisan view:cache
    
  • Tweak Queue Settings: Adjust queue configurations in config/queue.php (like retry attempts or timeout values) to match your application's needs.
  • Batch Emails: If sending to multiple users, use Mail::bcc() or batch queue jobs to reduce overhead.

内容的提问来源于stack exchange,提问作者Leoh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:22:31