带有队列 550 错误的 Laravel 电子邮件(每秒电子邮件太多)


Laravel email with queue 550 error (too many emails per second)

我们的电子邮件无法使用带有 Redis 队列的 Laravel 发送。

触发错误的代码如下:->onQueue('emails')

$job = (new SendNewEmail($sender, $recipients))->onQueue('emails');
$job_result = $this->dispatch($job);

结合这项工作:

use InteractsWithQueue;

我们的错误消息是:

Feb 09 17:15:57 laravel: message repeated 7947 times: [ production.ERROR: exception 'Swift_TransportException' with message 'Expected response code 354 but got code "550", with message "550 5.7.0 Requested action not taken: too many emails per second "' in /home/laravel/app/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:383 Stack trace: #0 /home/laravel/app/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(281): 

我们的错误只发生在使用Sendgrid而不是Mailtrap,后者欺骗电子邮件发送。我已经与Sendgrid进行了交谈,电子邮件从未接触过他们的服务器,当我的错误发生时,他们的服务完全处于活动状态。所以,错误似乎是在我这边。

有什么想法吗?

似乎

只有Mailtrap发送此错误,因此请开设另一个帐户或升级到付费计划。

我终于想出了如何设置整个Laravel应用程序以根据配置限制邮件。

AppServiceProviderboot()函数中,

$throttleRate = config('mail.throttleToMessagesPerMin');
if ($throttleRate) {
    $throttlerPlugin = new 'Swift_Plugins_ThrottlerPlugin($throttleRate, 'Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE);
    Mail::getSwiftMailer()->registerPlugin($throttlerPlugin);
}

config/mail.php 中添加以下行:

'throttleToMessagesPerMin' => env('MAIL_THROTTLE_TO_MESSAGES_PER_MIN', null), //https://mailtrap.io has a rate limit of 2 emails/sec per inbox, but consider being even more conservative.

.env文件中,添加如下行:

MAIL_THROTTLE_TO_MESSAGES_PER_MIN=50

唯一的问题是,如果QUEUE_DRIVER=sync,它似乎不会影响通过later()功能发送的邮件。

仅用于调试!
如果您不希望收到超过 5 封电子邮件,并且没有更改邮件陷阱的选项,请尝试:

foreach ($emails as $email) {
    ...
    Mail::send(... $email);                                                                      
    if(env('MAIL_HOST', false) == 'smtp.mailtrap.io'){
        sleep(1); //use usleep(500000) for half a second or less
    }
}

使用 sleep() 是一个非常糟糕的做法。理论上,此代码应仅在测试环境或调试模式下执行。

也许你应该确保它真的是通过Sendgrid而不是mailtrap发送的。他们的硬速率限制目前似乎是每秒 3k 个请求,而免费计划:)的 mailtrap 每秒 3 个请求

我用sleep(5)等待五秒钟,然后再使用mailtrap。

foreach ($this->suscriptores as $suscriptor)  {
    'Mail::to($suscriptor->email)
           ->send(new BoletinMail($suscriptor, $sermones, $entradas));
    sleep(5);
}

我在foreach内使用了sleep(5).foreach 遍历存储在数据库中的所有电子邮件,sleep(5) 将停止循环五秒钟,然后继续处理下一封电子邮件。

我在 Laravel v5.8 上通过手动设置身份验证路由实现了这一目标。路由位于文件 routes'web.php 上。以下是需要添加到该文件的路由:

Auth::routes();
Route::get('email/verify', 'Auth'VerificationController@show')->name('verification.notice');
Route::get('email/verify/{id}', 'Auth'VerificationController@verify')->name('verification.verify');
Route::group(['middleware' => 'throttle:1,1'], function(){
    Route::get('email/resend', 'Auth'VerificationController@resend')->name('verification.resend');
});

解释:

  • 不要将任何参数传递给路由Auth::routes();以手动配置身份验证路由。
  • 使用中间件throttle:1,1将路由email/resend包装在Route::group内(这两个数字表示最大重试次数和这些最大重试次数的时间(以分钟为单位)

我还删除了文件app'Http'Controllers'Auth'VerificationController.php __construct 函数中的一行代码。

我删除了这个:

$this->middleware('throttle:6,1')->only('verify', 'resend');
您需要对

队列进行速率限制emails

"官方"方法是设置 Redis 队列驱动程序。但这既困难又耗时。

所以我编写了自定义队列工作线程 mxl/laravel-queue-rate-limit,它使用 Illuminate'Cache'RateLimiter 来限制作业执行的速率(与 Laravel 内部用于速率限制 HTTP 请求

的相同)。

config/queue.phpemails指定队列的速率限制(例如每秒 2 封电子邮件):

'rateLimit' => [
    'emails' => [
        'allows' => 2,
        'every' => 1
    ]
]

并为此队列运行 worker :

$ php artisan queue:work --queue emails

我在使用邮件陷阱时遇到了这个问题。我在一秒钟内发送了 10 封邮件,它被视为垃圾邮件。我不得不在每份工作之间耽搁。看看解决方案(它的Laravel-我有system_jobs表并使用queue=database

创建一个静态函数来检查上次作业时间,然后self::DELAY_IN_SECONDS添加到它 - 您希望作业之间有多少秒:

public static function addSecondsToQueue() {
        $job = SystemJobs::orderBy('available_at', 'desc')->first();
        if($job) {
            $now = Carbon::now()->timestamp;
            $jobTimestamp = $job->available_at + self::DELAY_IN_SECONDS;
            $result = $jobTimestamp - $now;
            return $result;
        } else {
            return 0;
        }
    }

然后使用它来延迟发送消息(考虑到队列中的最后一个作业)

Mail::to($mail)->later(SystemJobs::addSecondsToQueue(), new SendMailable($params));