File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
src/Domain/Messaging/EventSubscriber Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments