debug und so
This commit is contained in:
@@ -5,13 +5,36 @@ namespace App;
|
||||
|
||||
final class Mailer
|
||||
{
|
||||
public function __construct(private App $app) {}
|
||||
private string $logFile;
|
||||
|
||||
public function __construct(private App $app)
|
||||
{
|
||||
$base = dirname(__DIR__, 2);
|
||||
$this->logFile = $base . '/debug/mailer_debug.log';
|
||||
}
|
||||
|
||||
private function log(string $msg, array $ctx = []): void
|
||||
{
|
||||
if (!defined('APP_DEBUG') || APP_DEBUG !== true) {
|
||||
return;
|
||||
}
|
||||
$line = '[' . date('Y-m-d H:i:s') . '] ' . $msg;
|
||||
if ($ctx) {
|
||||
$line .= ' ' . json_encode($ctx, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
$line .= "\n";
|
||||
$dir = dirname($this->logFile);
|
||||
if (!is_dir($dir)) {
|
||||
@mkdir($dir, 0775, true);
|
||||
}
|
||||
@file_put_contents($this->logFile, $line, FILE_APPEND);
|
||||
}
|
||||
|
||||
private function templates(): array
|
||||
{
|
||||
$env = $this->app->config()->env;
|
||||
$root = __DIR__ . '/../../config/emailtemplates.php';
|
||||
$envPath = __DIR__ . "/../../config/emailtemplates.php";
|
||||
$envPath = __DIR__ . "/../../config/{$env}/emailtemplates.php";
|
||||
$file = is_file($root) ? $root : $envPath;
|
||||
$emailtemplates = [];
|
||||
if (is_file($file)) {
|
||||
@@ -35,6 +58,7 @@ final class Mailer
|
||||
'placeholders' => $vars,
|
||||
];
|
||||
$payload['token'] = $apiToken;
|
||||
$this->log('template_api_request', ['template' => $id, 'placeholders' => array_keys($vars)]);
|
||||
$ctx = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
@@ -47,11 +71,15 @@ final class Mailer
|
||||
if ($resp !== false) {
|
||||
$decoded = json_decode($resp, true);
|
||||
if (is_array($decoded) && !empty($decoded['ok']) && !empty($decoded['html'])) {
|
||||
$this->log('template_api_success', ['template' => $id, 'subject' => $decoded['subject'] ?? null, 'html_len' => strlen((string)$decoded['html'])]);
|
||||
return [
|
||||
'subject' => $decoded['subject'] ?? 'Papakind',
|
||||
'subject' => $decoded['subject'] ?? 'Papa-Kind-Treff',
|
||||
'html' => $decoded['html'],
|
||||
];
|
||||
}
|
||||
$this->log('template_api_response_invalid', ['template' => $id, 'response' => $decoded]);
|
||||
} else {
|
||||
$this->log('template_api_unreachable', ['template' => $id]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +89,7 @@ final class Mailer
|
||||
foreach ($vars as $k => $v) {
|
||||
$body = str_replace(['{' . $k . '}', '{{' . $k . '}}'], (string)$v, $body);
|
||||
}
|
||||
$this->log('template_fallback_used', ['template' => $id]);
|
||||
return [
|
||||
'subject' => $subject,
|
||||
'html' => nl2br(htmlspecialchars($body, ENT_QUOTES)),
|
||||
@@ -81,6 +110,7 @@ final class Mailer
|
||||
$fromEmail = getenv('MAIL_FROM') ?: 'no-reply@' . $this->app->config()->primaryDomain;
|
||||
$fromName = getenv('MAIL_FROM_NAME') ?: 'Papa-Kind-Treff';
|
||||
|
||||
$this->log('mail_send_start', ['template' => $templateKey, 'to' => $to, 'transport' => $transport, 'subject' => $subject]);
|
||||
if ($transport === 'smtp') {
|
||||
$this->sendSmtp($to, $subject, $html, $fromEmail, $fromName);
|
||||
} else {
|
||||
@@ -95,7 +125,9 @@ final class Mailer
|
||||
$headers[] = 'From: ' . sprintf('"%s" <%s>', addslashes($fromName), $from);
|
||||
}
|
||||
$headers[] = 'Content-Type: text/html; charset=utf-8';
|
||||
if (!@mail($to, $subject, $html, implode("\r\n", $headers))) {
|
||||
$ok = @mail($to, $subject, $html, implode("\r\n", $headers));
|
||||
$this->log('mail_mail_transport', ['to' => $to, 'ok' => $ok]);
|
||||
if (!$ok) {
|
||||
throw new \RuntimeException('mail() transport failed');
|
||||
}
|
||||
}
|
||||
@@ -116,6 +148,7 @@ final class Mailer
|
||||
$proto = ($secure === 'ssl') ? 'ssl://' : '';
|
||||
$fp = @stream_socket_client($proto . $host . ':' . $port, $errno, $errstr, 15, STREAM_CLIENT_CONNECT);
|
||||
if (!$fp) {
|
||||
$this->log('mail_smtp_connect_failed', ['host' => $host, 'port' => $port, 'error' => $errstr]);
|
||||
$this->sendMailFn($to, $subject, $html, $from, $fromName);
|
||||
return;
|
||||
}
|
||||
@@ -170,5 +203,6 @@ final class Mailer
|
||||
$read();
|
||||
$write('QUIT');
|
||||
fclose($fp);
|
||||
$this->log('mail_smtp_sent', ['to' => $to, 'host' => $host, 'port' => $port, 'secure' => $secure]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user