"); self::cmd($sock, "RCPT TO:<{$to}>"); self::cmd($sock, "DATA"); $msg = "From: {$fromName} <{$from}>\r\n" . "To: {$to}\r\n" . "Subject: {$subject}\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: text/html; charset=UTF-8\r\n" . "\r\n" . $htmlBody . "\r\n.\r\n"; fwrite($sock, $msg); self::readLine($sock); self::cmd($sock, "QUIT"); fclose($sock); return true; } catch (\Throwable $e) { error_log("Mailer error: " . $e->getMessage()); return false; } } private static function cmd($sock, string $cmd): string { fwrite($sock, $cmd . "\r\n"); return self::readLine($sock); } private static function readLine($sock): string { $resp = ''; while ($line = fgets($sock, 512)) { $resp .= $line; if (isset($line[3]) && $line[3] === ' ') break; } return $resp; } public static function getConfig(): array { $defaults = [ 'smtp_host' => '', 'smtp_port' => '465', 'smtp_user' => '', 'smtp_pass' => '', 'smtp_from' => '', 'smtp_from_name' => 'PK10', 'smtp_encryption' => 'ssl', ]; try { $db = new Database(); $rows = $db->select('system_settings', ['setting_key', 'setting_value'], [ 'setting_key[~]' => 'smtp_%' ]); foreach ($rows as $r) $defaults[$r['setting_key']] = $r['setting_value']; } catch (\Throwable $e) {} return $defaults; } public static function test(string $to): array { $ok = self::send($to, 'SMTP Test', '
Time: ' . date('Y-m-d H:i:s') . '
'); return ['success' => $ok, 'message' => $ok ? 'Test email sent' : 'Failed to send, check SMTP settings']; } }