diff --git a/composer.json b/composer.json index bdfd6859b..1f22e39b0 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "nette/tester": "^2.2", "latte/latte": "^2.5", "psr/log": "^1.0 || ^2.0 || ^3.0", - "phpstan/phpstan": "^1.0" + "phpstan/phpstan": "^1.0", + "nette/http": "^3.0" }, "conflict": { "nette/di": "<3.0" diff --git a/src/Bridges/Nette/MailSender.php b/src/Bridges/Nette/MailSender.php index bac96ae50..49b452a95 100644 --- a/src/Bridges/Nette/MailSender.php +++ b/src/Bridges/Nette/MailSender.php @@ -12,7 +12,6 @@ use Nette; use Tracy; - /** * Tracy logger bridge for Nette Mail. */ @@ -25,17 +24,21 @@ class MailSender /** @var string|null sender of email notifications */ private ?string $fromEmail = null; + /** @var string|null actual host on which notification occurred - visible in subject */ + private ?string $host = null; + - public function __construct(Nette\Mail\IMailer $mailer, ?string $fromEmail = null) + public function __construct(Nette\Mail\IMailer $mailer, ?string $fromEmail = null, ?string $host = null) { $this->mailer = $mailer; $this->fromEmail = $fromEmail; + $this->host = $host; } public function send(mixed $message, string $email): void { - $host = preg_replace('#[^\w.-]+#', '', $_SERVER['SERVER_NAME'] ?? php_uname('n')); + $host = preg_replace('#[^\w.-]+#', '', $this->host ?? $_SERVER['SERVER_NAME'] ?? php_uname('n')); $mail = new Nette\Mail\Message; $mail->setHeader('X-Mailer', 'Tracy'); diff --git a/src/Bridges/Nette/TracyExtension.php b/src/Bridges/Nette/TracyExtension.php index 9d4df697c..6649e0257 100644 --- a/src/Bridges/Nette/TracyExtension.php +++ b/src/Bridges/Nette/TracyExtension.php @@ -116,8 +116,14 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class) } if ($this->config->netteMailer && $builder->getByType(Nette\Mail\IMailer::class)) { + $senderParams = []; + $senderParams['fromEmail'] = $this->config->fromEmail; + if (class_exists(Nette\Http\Request::class)) { + $senderParams['host'] = new Statement('($request = $this->getByType(?, false)) \? $request->getUrl()->getHost() : null', [Nette\Http\Request::class]); + } + $initialize->addBody($builder->formatPhp('if ($logger instanceof Tracy\Logger) $logger->mailer = ?;', [ - [new Statement(Tracy\Bridges\Nette\MailSender::class, ['fromEmail' => $this->config->fromEmail]), 'send'], + [new Statement(Tracy\Bridges\Nette\MailSender::class, $senderParams), 'send'], ])); }