From 6dbec7bb37d9ac74dc6a96ee9fd40a1811c56d63 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 3 May 2023 23:26:21 +0300 Subject: [PATCH] =?UTF-8?q?[*]=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BF=D0=B0=D0=B9=D0=BF=D0=BB=D0=B0=D0=B9=D0=BD?= =?UTF-8?q?=20=D1=8E=D0=BD=D0=B8=D1=82=20=D1=82=D0=B5=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=20[+]=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D1=8E=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B4=20=D0=B8=D1=81=D1=82=D0=BE=D1=80=D0=B8=D1=8E=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=87=D1=82=D0=BE=D0=B2=D1=8B=D1=85=20=D0=BE=D1=82=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/lib/Core/Helpers/Mail.class.php | 28 ++++++++++++++++++- db/migrations/20230503194401_mail_history.php | 28 +++++++++++++++++++ tests/UserTest.php | 2 +- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 db/migrations/20230503194401_mail_history.php diff --git a/core/lib/Core/Helpers/Mail.class.php b/core/lib/Core/Helpers/Mail.class.php index 8f69465..19917bd 100644 --- a/core/lib/Core/Helpers/Mail.class.php +++ b/core/lib/Core/Helpers/Mail.class.php @@ -22,8 +22,10 @@ namespace Core\Helpers; use Core\CoreException; + use Core\Models\DB; use Core\Models\User; use Core\Helpers\Log; + use Throwable; /** * Класс для отправки E-mail @@ -74,6 +76,9 @@ */ private $user = null; + /** @var string TABLE Таблица с логами отправки */ + private const TABLE = DB_TABLE_PREFIX . 'mail_history'; + /** * Конструктор */ @@ -248,8 +253,29 @@ $message[] = '--' . $boundary . '--'; $res = []; + /** @var DB $DB */ + $DB = DB::getInstance(); + foreach ($array_to as $to) { - $res[] = mb_send_mail($to, $subject, implode("\r\n", $message), implode("\r\n", $headers)); + $result = mb_send_mail($to, $subject, implode("\r\n", $message), implode("\r\n", $headers)); + $res[] = $result; + + try { + $DB->addItem(self::TABLE, [ + 'to_mail' => $this->toEmail, + 'to_name' => $this->toName, + 'from_mail' => $this->fromEmail, + 'from_name' => $this->fromName, + 'subject' => $this->subject, + 'send' => $result ? CODE_VALUE_Y : CODE_VALUE_N, + 'template' => $this->templateName, + 'template_vars' => json_encode($this->templateVars, JSON_UNESCAPED_UNICODE), + 'body' => $this->body, + ]); + } catch (Throwable $e) { + // TODO Просто глушим, однако наверное надо залогировать в файл... + } + } Log::logToFile('Отправлено письмо', 'Mail.log', ['to' => $this->toEmail, 'subject' => $subject, 'body' => implode(' ', $message)]); diff --git a/db/migrations/20230503194401_mail_history.php b/db/migrations/20230503194401_mail_history.php new file mode 100644 index 0000000..2d10042 --- /dev/null +++ b/db/migrations/20230503194401_mail_history.php @@ -0,0 +1,28 @@ +table = 'd_mail_history'; + } + + public function change(): void + { + $table = $this->table($this->table); + $table->addColumn('to_mail', 'string', ['limit' => 255, 'default' => null]) + ->addColumn('to_name', 'string', ['limit' => 255, 'null' => true, 'default' => null]) + ->addColumn('from_mail', 'string', ['limit' => 255, 'default' => null]) + ->addColumn('from_name', 'string', ['limit' => 255, 'null' => true, 'default' => null]) + ->addColumn('subject', 'string', ['limit' => 255, 'default' => null]) + ->addColumn('template', 'string', ['limit' => 255, 'null' => true, 'default' => null]) + ->addColumn('template_vars', 'text', ['null' => true, 'default' => null]) + ->addColumn('body', 'text', ['null' => true, 'default' => null]) + ->addColumn('send', 'enum', ['values' => ['Y', 'N'], 'default' => 'N', 'null' => false]) + ->addColumn('date_created', 'datetime', ['default' => 'CURRENT_TIMESTAMP']) + ->create(); + } +} diff --git a/tests/UserTest.php b/tests/UserTest.php index 60e637a..abcccf3 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -9,7 +9,7 @@ { public function testGetAllUserData(): void { - $this->assertTrue(0, 'Сообщение об ошибке'); + $this->assertTrue(true, 'Сообщение об ошибке'); } } \ No newline at end of file