Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Our emails are failing to send using Laravel with a Redis Queue.

The code that triggers the error is this: ->onQueue('emails')

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

In combination with this in the job:

use InteractsWithQueue;

Our error message is:

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): 

Our error only happens using Sendgrid and not Mailtrap, which spoofs emailing sending. I've talked with Sendgrid and the emails never touched their servers and their service was fully active when my error occurred. So, the error appears to be on my end.

Any thoughts?

like image 267
tim peterson Avatar asked Feb 09 '16 23:02

tim peterson


People also ask

How many emails do you get per second?

Today, approximately 333.2 billion emails are sent per day, which works out at well over 3.5 million emails per second. That's a lot of emails.

How do I send an email using laravel?

Laravel uses free feature-rich library SwiftMailer to send emails. Using the library function, we can easily send emails without too many hassles. The e-mail templates are loaded in the same way as views, which means you can use the Blade syntax and inject data into your templates. Sends email.


2 Answers

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.

like image 69
Ryan Avatar answered Sep 18 '22 06:09

Ryan


Seems like only Mailtrap sends this error, so either open another account or upgrade to a paid plan.

like image 26
Dorian Avatar answered Sep 19 '22 06:09

Dorian