Recently, I was tasked with diagnosing why emails sent by a web application to a company internal email address were not always arriving in the inbox.
This internal email was hosted on Google Apps, which has a receiving rate limit of 60 per minute.
Any emails that exceed the quota are dropped, though the documentation says they are bounced.
THIS IS A LIE!
To solve this problem, I used Laravel event listeners and the magic of @antirez’s Redis.
Add the following events to your app/Providers/EventServiceProvider.php file’s listen array.
The MessageSent listener will use Redis to count the number of messages sent.
The current minute is stored as part of the redis key.
When a new minute starts, a new key will be used.
To prevent the keys from overlapping to the next hour, an expiration value is set to 30 minutes.
The MessageSending event runs before an email gets sent and is where the rate limiting actually occurs.
This event can be tuned to fit your needs.