mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 18:04:37 -04:00
Implement logic to throttle emails
This commit is contained in:
parent
6c80df9835
commit
96f61865d8
@ -48,3 +48,4 @@ POSTMARK_API_TOKEN=
|
|||||||
|
|
||||||
GOOGLE_MAPS_API_KEY=
|
GOOGLE_MAPS_API_KEY=
|
||||||
API_SECRET=superdoopersecrethere
|
API_SECRET=superdoopersecrethere
|
||||||
|
ERROR_EMAIL=
|
||||||
|
76
app/Utils/Traits/ThrottlesEmail.php
Normal file
76
app/Utils/Traits/ThrottlesEmail.php
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://opensource.org/licenses/AAL
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Utils\Traits;
|
||||||
|
|
||||||
|
use App\Models\Company;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Facades\Mail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ThrottlesEmail
|
||||||
|
* @package App\Utils\Traits
|
||||||
|
*/
|
||||||
|
trait ThrottlesEmail
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public function getDailyEmailLimit(Company $company)
|
||||||
|
{
|
||||||
|
$limit = config('ninja.daily_email_limit');
|
||||||
|
|
||||||
|
$limit += $company->created_at->diffInMonths() * 100;
|
||||||
|
|
||||||
|
return min($limit, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isThrottled(Company $company)
|
||||||
|
{
|
||||||
|
|
||||||
|
$key = $company->company_key;
|
||||||
|
|
||||||
|
// http://stackoverflow.com/questions/1375501/how-do-i-throttle-my-sites-api-users
|
||||||
|
$day = 60 * 60 * 24;
|
||||||
|
$day_limit = $this->getDailyEmailLimit();
|
||||||
|
$day_throttle = Cache::get("email_day_throttle:{$key}", null);
|
||||||
|
$last_api_request = Cache::get("last_email_request:{$key}", 0);
|
||||||
|
$last_api_diff = time() - $last_api_request;
|
||||||
|
|
||||||
|
if (is_null($day_throttle)) {
|
||||||
|
$new_day_throttle = 0;
|
||||||
|
} else {
|
||||||
|
$new_day_throttle = $day_throttle - $last_api_diff;
|
||||||
|
$new_day_throttle = $new_day_throttle < 0 ? 0 : $new_day_throttle;
|
||||||
|
$new_day_throttle += $day / $day_limit;
|
||||||
|
$day_hits_remaining = floor(($day - $new_day_throttle) * $day_limit / $day);
|
||||||
|
$day_hits_remaining = $day_hits_remaining >= 0 ? $day_hits_remaining : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cache::put("email_day_throttle:{$key}", $new_day_throttle, 60);
|
||||||
|
Cache::put("last_email_request:{$key}", time(), 60);
|
||||||
|
|
||||||
|
if ($new_day_throttle > $day) {
|
||||||
|
$error_email = config('ninja.error_email');
|
||||||
|
if ($error_email && ! Cache::get("throttle_notified:{$key}")) {
|
||||||
|
Mail::raw('Account Throttle: ' . $company->company_key, function ($message) use ($error_email, $company) {
|
||||||
|
$message->to($error_email)
|
||||||
|
->from(config('ninja.contact.email'))
|
||||||
|
->subject("Email throttle triggered for company " . $company->id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Cache::put("throttle_notified:{$key}", true, 60 * 24);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,8 @@ return [
|
|||||||
'key_length' => 64,
|
'key_length' => 64,
|
||||||
'date_format' => 'Y-m-d',
|
'date_format' => 'Y-m-d',
|
||||||
'date_time_format' => 'Y-m-d H:i',
|
'date_time_format' => 'Y-m-d H:i',
|
||||||
|
'daily_email_limit' => 300,
|
||||||
|
'error_email' => env('ERROR_EMAIL', ''),
|
||||||
|
|
||||||
'environment' => env('NINJA_ENVIRONMENT', 'selfhost'), // 'hosted', 'development', 'selfhost', 'reseller'
|
'environment' => env('NINJA_ENVIRONMENT', 'selfhost'), // 'hosted', 'development', 'selfhost', 'reseller'
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user