From 1b486eb78847a0c020e55a38f945086315e35aa6 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Wed, 4 Dec 2024 18:45:17 +0300 Subject: [PATCH] some --- .env.example | 7 +++ bootstrap/secure.php | 1 + kernel/Mailing.php | 50 +++++++++++++++++++ kernel/helpers/SMTP.php | 23 +++++---- .../ModuleShopClientController.php | 14 +++--- kernel/views/mailing/login_by_code.php | 12 +++++ 6 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 kernel/Mailing.php create mode 100644 kernel/views/mailing/login_by_code.php diff --git a/.env.example b/.env.example index 64e043c..319b8f0 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,5 @@ +APP_NAME="It Guild Micro Framework" + DB_HOST=localhost DB_USER=user DB_DRIVER=mysql @@ -10,6 +12,11 @@ DB_PREFIX='' VIEWS_PATH=/views VIEWS_CACHE_PATH=/views_cache +MAIL_SMTP_HOST=smtp.mail.ru +MAIL_SMTP_PORT=587 +MAIL_SMTP_USERNAME=username@mail.ru +MAIL_SMTP_PASSWORD=somepassword + MODULE_SHOP_URL='http://igfs.loc' MODULE_SHOP_TOKEN='your token' diff --git a/bootstrap/secure.php b/bootstrap/secure.php index a800dda..58f17bd 100644 --- a/bootstrap/secure.php +++ b/bootstrap/secure.php @@ -1,6 +1,7 @@ 'login_password', // login_password, email_code 'token_type' => 'JWT', // random_bytes, md5, crypt, hash, JWT 'token_expired_time' => "+30 days", // +1 day ]; diff --git a/kernel/Mailing.php b/kernel/Mailing.php new file mode 100644 index 0000000..05b341c --- /dev/null +++ b/kernel/Mailing.php @@ -0,0 +1,50 @@ +cgView = new CgView(); + $this->cgView->viewPath = KERNEL_DIR . "/views/mailing/"; + + $this->data = $data; + + $this->SMTP = new SMTP(); + + $this->init(); + } + + /** + * @throws Exception + */ + public function send_html(string $tpl, array $tplParams, array $mailParams): ?false + { + $mailParams['body'] = $this->cgView->fetch($tpl, $tplParams); + return $this->SMTP->send_html($mailParams); + } + + public function run() + { + } + + public static function create(array $data = []): static + { + return new static($data); + } + + protected function init() + { + } + +} \ No newline at end of file diff --git a/kernel/helpers/SMTP.php b/kernel/helpers/SMTP.php index 9abdf95..0f6d5e6 100644 --- a/kernel/helpers/SMTP.php +++ b/kernel/helpers/SMTP.php @@ -8,28 +8,31 @@ use PHPMailer\PHPMailer\PHPMailer; class SMTP { public PHPMailer $mail; - public function __construct(array $config = []) + public function __construct() { $this->mail = new PHPMailer(true); $this->mail->CharSet = 'UTF-8'; $this->mail->isSMTP(); $this->mail->SMTPAuth = true; $this->mail->SMTPDebug = 0; - $this->mail->Host = $config['host'] ?? ''; - $this->mail->Port = $config['port'] ?? 587; - $this->mail->Username = $config['username'] ?? ''; - $this->mail->Password = $config['password'] ?? ''; + $this->mail->Host = $_ENV['MAIL_SMTP_HOST']; + $this->mail->Port = $_ENV['MAIL_SMTP_PORT']; + $this->mail->Username = $_ENV['MAIL_SMTP_USERNAME']; + $this->mail->Password = $_ENV['MAIL_SMTP_PASSWORD']; } /** * @throws Exception */ - public function send(string $address): void + public function send_html(array $params) { - $this->mail->setFrom($this->mail->Username, $this->mail->Host); - $this->mail->addAddress($address); - $this->mail->Subject = 'Код подтверждения'; - $body = '

«Hello, world!»

'; + if (!isset($params['address'])){ + return false; + } + $this->mail->setFrom($this->mail->Username, $params['from_name'] ?? $this->mail->Host); + $this->mail->addAddress($params['address']); + $this->mail->Subject = $params['subject'] ?? 'Без темы'; + $body = $params['body'] ?? 'Нет информации'; $this->mail->msgHTML($body); $this->mail->send(); diff --git a/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php b/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php index 549afc6..c1a12e9 100644 --- a/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php +++ b/kernel/modules/module_shop_client/controllers/ModuleShopClientController.php @@ -11,6 +11,7 @@ use kernel\helpers\Debug; use kernel\helpers\Files; use kernel\helpers\RESTClient; use kernel\helpers\SMTP; +use kernel\Mailing; use kernel\modules\module_shop_client\services\ModuleShopClientService; use kernel\Request; use kernel\services\ModuleService; @@ -127,14 +128,13 @@ class ModuleShopClientController extends AdminController { $request = new Request(); $address = $request->post("email"); - $mail = new SMTP([ - 'host' => 'smtp.mail.ru', - 'port' => 587, - 'username' => 'chancellery@itguild.info', - 'password' => 'iBdGdxmJk1mnySJYtXc0' - ]); - $mail->send($address); + $mailing = new Mailing(); + $mailing->send_html("login_by_code.php", ['code' => mt_rand(100000, 999999)], [ + 'address' => $address, + 'subject' => "Код авторизации", + "from_name" => $_ENV['APP_NAME'] + ]); } } \ No newline at end of file diff --git a/kernel/views/mailing/login_by_code.php b/kernel/views/mailing/login_by_code.php new file mode 100644 index 0000000..a172e1d --- /dev/null +++ b/kernel/views/mailing/login_by_code.php @@ -0,0 +1,12 @@ + + +

+ Код подтверждения: +

+

+ Если вы не запрашивали код, проигнорируйте данное письмо. +

\ No newline at end of file