feat: disable mailjet open and click tracking

This commit is contained in:
fromme committed 2026-11-10 17:13:06 +01:00
1 parent 8898320e21
commit 2ef831c5c4
2 files changed
+72

No files matched your search

+13
View File
@@ -19,6 +19,9 @@ class Mailer
* reminders: Mailjet (MAILING_MAILER_DSN, mailer.yaml), rather than the webhoster's relay that
* carries every other mail, which would risk the hosting account at a few hundred mails. Pass
* it as the 'transport' option.
*
* Mail sent over it is never tracked, whatever the Mailjet account is set to: no open pixel,
* no links rewritten through Mailjet's redirect domain. Staff mail is not a campaign.
*/
public const string MAILJET_TRANSPORT = 'mailing';
@@ -83,6 +86,16 @@ class Mailer
$email->getHeaders()->addTextHeader('X-Transport', $config['transport']);
}
// Mailjet tracks per its account settings unless a message says otherwise. These headers
// only work over SMTP (mailjet+smtp, the DSN we use) - the Send API refuses them - and
// Symfony's SMTP transport passes them through untouched.
if (self::MAILJET_TRANSPORT === $config['transport']) {
$email->getHeaders()
->addTextHeader('X-Mailjet-TrackOpen', '0')
->addTextHeader('X-Mailjet-TrackClick', '0')
;
}
if (null !== $config['bus_transport']) {
$email->getHeaders()->addTextHeader('X-Bus-Transport', $config['bus_transport']);
}
+59
View File
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Tests\Email;
use App\Email\Mailer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* What a mail carries for the transport it goes out over, built through the real template stack.
*/
class MailerTest extends KernelTestCase
{
/**
* Mailjet would otherwise track opens and rewrite links if the account is set to, which staff
* mail - mailings and reminders alike - must not be.
*/
public function testMailOverMailjetIsNotTracked(): void
{
$headers = $this->create(Mailer::MAILJET_TRANSPORT)->getHeaders();
$this->assertSame(Mailer::MAILJET_TRANSPORT, $headers->get('X-Transport')?->getBodyAsString());
$this->assertSame('0', $headers->get('X-Mailjet-TrackOpen')?->getBodyAsString());
$this->assertSame('0', $headers->get('X-Mailjet-TrackClick')?->getBodyAsString());
}
/**
* The webhoster's relay would pass Mailjet headers on to the recipient as noise.
*/
public function testMailOverTheRelayCarriesNoMailjetHeaders(): void
{
$headers = $this->create(null)->getHeaders();
$this->assertFalse($headers->has('X-Transport'));
$this->assertFalse($headers->has('X-Mailjet-TrackOpen'));
$this->assertFalse($headers->has('X-Mailjet-TrackClick'));
}
/**
* Mailer::create() takes the options as createAndSendEmail() has resolved them.
*/
private function create(?string $transport): TemplatedEmail
{
self::bootKernel();
return self::getContainer()->get(Mailer::class)->create(['feedbacks' => []], [
'from' => '[email protected]',
'to' => '[email protected]',
'subject' => 'Betreff',
'subject_parameters' => [],
'template' => 'email/reminder_pending_feedback.html.twig',
'attachments' => [],
'transport' => $transport,
'bus_transport' => null,
]);
}
}