Timeout server SMTP

Aggiunto timeout del server SMTP per gestire al meglio l'invio sequenziale delle email.
This commit is contained in:
Thomas Zilio 2019-08-29 14:48:14 +02:00
parent 62ebdbb999
commit d7fd612dc9
5 changed files with 36 additions and 17 deletions

View File

@ -88,14 +88,14 @@ function executeHook(hook, token) {
var timeout;
if (result.execute) {
timeout = 1;
startHook(hook);
} else {
timeout = 30;
}
setTimeout(function () {
startHook(hook);
}, timeout * 1000);
setTimeout(function () {
startHook(hook);
}, timeout * 1000);
}
},
});
}

View File

@ -2,6 +2,7 @@
namespace Modules\Emails;
use Carbon\Carbon;
use Hooks\Manager;
use Notifications\EmailNotification;
@ -29,20 +30,32 @@ class EmailHook extends Manager
public function execute()
{
$accounts = Account::all();
$diff = date('Y-m-d', strtotime('-4 hours'));
$failed = function ($query) use ($diff) {
$query->whereDate('failed_at', '<', $diff)
->orWhereNull('failed_at');
};
$accounts = Account::all();
$list = [];
foreach ($accounts as $account) {
$mail = Mail::whereNull('sent_at')
->where('id_account', $account->id)
->whereNull('failed_at')
->orWhereDate('failed_at', '<', $diff)
->orderBy('created_at')
->first();
$last_mail = $account->emails()->whereNotNull('sent_at')->orderBy('sent_at')->first();
if (!empty($mail)) {
$list[] = $mail;
// Controllo sul timeout dell'account
$date = new Carbon($last_mail->sent_at);
$now = new Carbon();
$diff = $date->diffInMilliseconds($now);
if (empty($last_mail) || $diff > $account->timeout) {
$mail = Mail::whereNull('sent_at')
->where('id_account', $account->id)
->where($failed)
->orderBy('created_at')
->first();
if (!empty($mail)) {
$list[] = $mail;
}
}
}

View File

@ -35,6 +35,7 @@ switch (post('op')) {
'from_address' => post('from_address'),
'encryption' => post('encryption'),
'pec' => post('pec'),
'timeout' => post('timeout'),
'ssl_no_verify' => post('ssl_no_verify'),
'predefined' => $predefined,
], ['id' => $id_record]);

View File

@ -63,8 +63,12 @@ include_once __DIR__.'/../../core.php';
{[ "type": "text", "label": "<?php echo tr('Username SMTP'); ?>", "name": "username", "value": "$username$" ]}
</div>
<div class="col-md-6">
{[ "type": "password", "label": "<?php echo tr('Password SMTP'); ?>", "class": "", "name": "password", "value": "$password$" ]}
<div class="col-md-3">
{[ "type": "password", "label": "<?php echo tr('Password SMTP'); ?>", "name": "password", "value": "$password$" ]}
</div>
<div class="col-md-3">
{[ "type": "number", "label": "<?php echo tr('Timeout coda di invio (millisecondi)'); ?>", "name": "timeout", "value": "$timeout$", "decimals": 0 ]}
</div>
</div>

View File

@ -419,7 +419,7 @@ INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`,
((SELECT `id` FROM `zz_modules` WHERE `name` = 'Newsletter'), 'Completato', 'IF(completed_at IS NULL, ''No'', ''Si'')', 4, 1, 0, 1, 1);
-- Modulo Stato email
INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Stato email', 'Stato email', 'stato_email', 'SELECT |select| FROM `em_emails` WHERE 1=1 AND (`em_emails`.`created_at` BETWEEN ''|period_start|'' AND ''|period_end|'' OR `em_emails`.`sent_at` IS NULL) HAVING 2=2', '', 'fa fa-spinner ', '2.4.11', '2.4.11', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '1');
INSERT INTO `zz_modules` (`id`, `name`, `title`, `directory`, `options`, `options2`, `icon`, `version`, `compatibility`, `order`, `parent`, `default`, `enabled`) VALUES (NULL, 'Stato email', 'Coda di invio', 'stato_email', 'SELECT |select| FROM `em_emails` WHERE 1=1 AND (`em_emails`.`created_at` BETWEEN ''|period_start|'' AND ''|period_end|'' OR `em_emails`.`sent_at` IS NULL) HAVING 2=2', '', 'fa fa-spinner ', '2.4.11', '2.4.11', '1', (SELECT `id` FROM `zz_modules` t WHERE t.`name` = 'Gestione email'), '1', '1');
INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`, `default`, `visible`, `format`) VALUES
((SELECT `id` FROM `zz_modules` WHERE `name` = 'Stato email'), 'id', 'id', 1, 0, 0, 1, 0, 0),
@ -432,3 +432,4 @@ INSERT INTO `zz_views` (`id_module`, `name`, `query`, `order`, `search`, `slow`,
ALTER TABLE `em_templates` CHANGE `id_smtp` `id_account` INT(11) NOT NULL;
ALTER TABLE `em_print_template` CHANGE `id_email` `id_template` INT(11) NOT NULL;
ALTER TABLE `em_accounts` ADD `timeout` INT(11) NOT NULL DEFAULT 1000;