Skip to content

Commit 69cb79a

Browse files
authored
MailSender takes host from nette/http (#566)
1 parent db0a128 commit 69cb79a

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"nette/tester": "^2.2",
2727
"latte/latte": "^2.5",
2828
"psr/log": "^1.0 || ^2.0 || ^3.0",
29-
"phpstan/phpstan": "^1.0"
29+
"phpstan/phpstan": "^1.0",
30+
"nette/http": "^3.0"
3031
},
3132
"conflict": {
3233
"nette/di": "<3.0"

src/Bridges/Nette/MailSender.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Nette;
1313
use Tracy;
1414

15-
1615
/**
1716
* Tracy logger bridge for Nette Mail.
1817
*/
@@ -25,17 +24,21 @@ class MailSender
2524
/** @var string|null sender of email notifications */
2625
private ?string $fromEmail = null;
2726

27+
/** @var string|null actual host on which notification occurred - visible in subject */
28+
private ?string $host = null;
29+
2830

29-
public function __construct(Nette\Mail\IMailer $mailer, ?string $fromEmail = null)
31+
public function __construct(Nette\Mail\IMailer $mailer, ?string $fromEmail = null, ?string $host = null)
3032
{
3133
$this->mailer = $mailer;
3234
$this->fromEmail = $fromEmail;
35+
$this->host = $host;
3336
}
3437

3538

3639
public function send(mixed $message, string $email): void
3740
{
38-
$host = preg_replace('#[^\w.-]+#', '', $_SERVER['SERVER_NAME'] ?? php_uname('n'));
41+
$host = preg_replace('#[^\w.-]+#', '', $this->host ?? $_SERVER['SERVER_NAME'] ?? php_uname('n'));
3942

4043
$mail = new Nette\Mail\Message;
4144
$mail->setHeader('X-Mailer', 'Tracy');

src/Bridges/Nette/TracyExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,14 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
116116
}
117117

118118
if ($this->config->netteMailer && $builder->getByType(Nette\Mail\IMailer::class)) {
119+
$senderParams = [];
120+
$senderParams['fromEmail'] = $this->config->fromEmail;
121+
if (class_exists(Nette\Http\Request::class)) {
122+
$senderParams['host'] = new Statement('($request = $this->getByType(?, false)) \? $request->getUrl()->getHost() : null', [Nette\Http\Request::class]);
123+
}
124+
119125
$initialize->addBody($builder->formatPhp('if ($logger instanceof Tracy\Logger) $logger->mailer = ?;', [
120-
[new Statement(Tracy\Bridges\Nette\MailSender::class, ['fromEmail' => $this->config->fromEmail]), 'send'],
126+
[new Statement(Tracy\Bridges\Nette\MailSender::class, $senderParams), 'send'],
121127
]));
122128
}
123129

0 commit comments

Comments
 (0)