Laravel email with queue 550 error (too many emails per second)
Seems like only Mailtrap sends this error, so either open another account or upgrade to a paid plan.
I finally figured out how to set up the entire Laravel app to throttle mail based on a config.
In the boot()
function of AppServiceProvider
,
$throttleRate = config('mail.throttleToMessagesPerMin');if ($throttleRate) { $throttlerPlugin = new \Swift_Plugins_ThrottlerPlugin($throttleRate, \Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE); Mail::getSwiftMailer()->registerPlugin($throttlerPlugin);}
In config/mail.php
, add this line:
'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.
In your .env
files, add a line like:
MAIL_THROTTLE_TO_MESSAGES_PER_MIN=50
The only problem is that it doesn't seem to affect mail sent via the later()
function if QUEUE_DRIVER=sync
.
For debugging only!
If you don't expect more then 5 emails and don't have the option to change mailtrap, try:
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 }}
Using sleep() is a really bad practice. In theory this code should only execute in test environment or in debug mode.