From 98c5bf135d70eac8affacbabba6ae97b0c1a6a1e Mon Sep 17 00:00:00 2001 From: Matteo Date: Mon, 14 Nov 2022 16:58:00 +0100 Subject: [PATCH] Task per l'invio delle mail con procedura in cron --- modules/emails/src/EmailTask.php | 56 ++++++++++++++++++++++++++++++++ update/2_4_37.sql | 2 ++ 2 files changed, 58 insertions(+) create mode 100644 modules/emails/src/EmailTask.php diff --git a/modules/emails/src/EmailTask.php b/modules/emails/src/EmailTask.php new file mode 100644 index 000000000..0bb48e7bb --- /dev/null +++ b/modules/emails/src/EmailTask.php @@ -0,0 +1,56 @@ +. + */ + +namespace Modules\Emails; + +use Carbon\Carbon; +use Notifications\EmailNotification; +use PHPMailer\PHPMailer\Exception; +use Tasks\Manager; + +class EmailTask extends Manager +{ + + public function needsExecution() + { + $lista = database()->fetchArray("SELECT * FROM em_emails WHERE (sent_at IS NULL OR failed_at IS NOT NULL) AND attempt<".prepare(setting('Numero massimo di tentativi'))." ORDER BY created_at"); + $remaining = sizeof($lista); + + return !empty($remaining); + } + + public function execute() + { + $lista = database()->fetchArray("SELECT * FROM em_emails WHERE (sent_at IS NULL OR failed_at IS NOT NULL) AND attempt<".prepare(setting('Numero massimo di tentativi'))." ORDER BY created_at LIMIT 0,".setting('Numero email da inviare in contemporanea per account')); + + foreach ($lista as $mail) { + + $mail = Mail::find($mail['id']); + + try { + $email = EmailNotification::build($mail); + $email->send(); + } catch (Exception $e) { + echo $e; + } + } + + return $lista; + } +} \ No newline at end of file diff --git a/update/2_4_37.sql b/update/2_4_37.sql index b8cdd23a5..44f4ca790 100644 --- a/update/2_4_37.sql +++ b/update/2_4_37.sql @@ -72,3 +72,5 @@ INSERT INTO `co_tipidocumento` (`id`, `descrizione`, `dir`, `reversed`, `codice_ -- Fix vista Ddt in entrata UPDATE `zz_views` INNER JOIN `zz_modules` ON `zz_views`.`id_module` = `zz_modules`.`id` SET `zz_views`.`order_by` = 'CAST(IF(numero_esterno="", numero, numero_esterno) AS UNSIGNED)' WHERE `zz_modules`.`name` = 'Ddt di acquisto' AND `zz_views`.`name` = 'Numero' + +INSERT INTO `zz_tasks` (`id`, `name`, `class`, `expression`, `next_execution_at`, `last_executed_at`) VALUES (NULL, 'Invio automatico mail', 'Modules\\Emails\\EmailTask', '*/1 * * * *', NULL, NULL); \ No newline at end of file