Skip to content

Commit 65c0030

Browse files
committed
InjectedByHeaderSubscriber
1 parent 109b07a commit 65c0030

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\Core\Domain\Messaging\EventSubscriber;
6+
7+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8+
use Symfony\Component\Mailer\Event\MessageEvent;
9+
use Symfony\Component\Mime\Email;
10+
11+
class InjectedByHeaderSubscriber implements EventSubscriberInterface
12+
{
13+
public static function getSubscribedEvents(): array
14+
{
15+
return [MessageEvent::class => 'onMessage'];
16+
}
17+
18+
public function onMessage(MessageEvent $event): void
19+
{
20+
// todo: add custom header only to messages sent individually not from campaigns
21+
// when the email is generated from a webpage (quite possible :-) add a "received line" to identify the origin
22+
$msg = $event->getMessage();
23+
if (!$msg instanceof Email)
24+
{
25+
return;
26+
}
27+
28+
// Only when triggered by HTTP request context (not CLI workers)
29+
if (PHP_SAPI === 'cli')
30+
{
31+
return;
32+
}
33+
34+
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
35+
$host = gethostname() ?: 'unknown-host';
36+
$time = date('r', $_SERVER['REQUEST_TIME'] ?? time());
37+
38+
$msg->getHeaders()->addTextHeader(
39+
'X-phpList-Injected-By',
40+
"from [$ip] by $host with HTTP; $time"
41+
);
42+
}
43+
}

0 commit comments

Comments
 (0)