Files
wms-app/app/assets/utils/classes/SmtpManager.php
T
2026-05-20 10:17:02 +07:00

180 lines
6.1 KiB
PHP

<?php
require_once __DIR__ . '/../module/mailer.php';
class SmtpManager
{
private PDO $pdo;
private int $companyId;
private string $method;
private string $pinkey;
private string $iv;
public function __construct(PDO $pdo, int $company_id, string $method, string $pinkey, string $iv)
{
$this->pdo = $pdo;
$this->companyId = $company_id;
$this->method = $method;
$this->pinkey = $pinkey;
$this->iv = $iv;
}
public function get(): ?array
{
$sth = $this->pdo->prepare(
"SELECT
server,
port,
username,
from_name,
from_email,
encryption
FROM company_smtp
WHERE company_id = :company_id
LIMIT 1"
);
$sth->execute([':company_id' => $this->companyId]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
return $row ?: null;
}
public function save(array $data): void
{
$server = trim((string)($data['smtp_host'] ?? ''));
$port = trim((string)($data['smtp_port'] ?? '587'));
$username = trim((string)($data['smtp_username'] ?? ''));
$raw_pass = (string)($data['smtp_password'] ?? '');
$from_name = trim((string)($data['smtp_from_name'] ?? ''));
$from_email = trim((string)($data['smtp_from_email'] ?? ''));
$encryption = trim((string)($data['smtp_encryption'] ?? 'tls'));
if ($server === '' || $username === '') {
throw new Exception('SMTP host and username are required.');
}
if (!in_array($port, ['25', '465', '587'], true)) $port = '587';
if (!in_array($encryption, ['tls', 'ssl', 'none'], true)) $encryption = 'tls';
$sth = $this->pdo->prepare(
"SELECT smtp_id
FROM company_smtp
WHERE company_id = :company_id
LIMIT 1"
);
$sth->execute([':company_id' => $this->companyId]);
$existing_id = $sth->fetchColumn();
if ($existing_id) {
$this->update((int)$existing_id, $server, $port, $username, $raw_pass, $from_name, $from_email, $encryption);
return;
}
if ($raw_pass === '') {
throw new Exception('Password is required for a new SMTP configuration.');
}
$this->pdo->prepare(
"INSERT INTO company_smtp
(company_id, server, port, username, password,
from_name, from_email, encryption, updated_at)
VALUES
(:company_id, :server, :port, :username, :password,
:from_name, :from_email, :encryption, NOW())"
)->execute([
':company_id' => $this->companyId,
':server' => $server,
':port' => $port,
':username' => $username,
':password' => $this->encryptPassword($raw_pass),
':from_name' => $from_name,
':from_email' => $from_email,
':encryption' => $encryption,
]);
}
public function sendTest(string $to): void
{
$to = trim($to);
if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid recipient email address.');
}
$sth = $this->pdo->prepare(
"SELECT from_name
FROM company_smtp
WHERE company_id = :company_id
LIMIT 1"
);
$sth->execute([':company_id' => $this->companyId]);
$row = $sth->fetch(PDO::FETCH_ASSOC);
if (!$row) {
throw new Exception('No SMTP configuration found. Please save your settings first.');
}
$mailer = new mailer(['pdo1' => $this->pdo]);
$mailer->send_email([
'company_id' => $this->companyId,
'smtp' => [],
'to' => $to,
'subject' => 'SMTP Test - WMS',
'message' => "This is a test email from your WMS SMTP configuration.\n\nIf you received this, your SMTP settings are working correctly.",
'channel_name' => $row['from_name'] ?: 'WMS System',
'key' => $this->pinkey,
]);
}
private function update(int $smtp_id, string $server, string $port, string $username, string $raw_pass, string $from_name, string $from_email, string $encryption): void
{
if ($raw_pass !== '') {
$this->pdo->prepare(
"UPDATE company_smtp SET
server = :server,
port = :port,
username = :username,
password = :password,
from_name = :from_name,
from_email = :from_email,
encryption = :encryption,
updated_at = NOW()
WHERE smtp_id = :smtp_id"
)->execute([
':server' => $server,
':port' => $port,
':username' => $username,
':password' => $this->encryptPassword($raw_pass),
':from_name' => $from_name,
':from_email' => $from_email,
':encryption' => $encryption,
':smtp_id' => $smtp_id,
]);
return;
}
$this->pdo->prepare(
"UPDATE company_smtp SET
server = :server,
port = :port,
username = :username,
from_name = :from_name,
from_email = :from_email,
encryption = :encryption,
updated_at = NOW()
WHERE smtp_id = :smtp_id"
)->execute([
':server' => $server,
':port' => $port,
':username' => $username,
':from_name' => $from_name,
':from_email' => $from_email,
':encryption' => $encryption,
':smtp_id' => $smtp_id,
]);
}
private function encryptPassword(string $plain): string
{
return openssl_encrypt($plain, $this->method, $this->pinkey, 0, $this->iv);
}
}
?>