From c8b0a6e966d9a1006b40d7be18b82207c6abdf21 Mon Sep 17 00:00:00 2001 From: Frank Date: Sat, 3 Jan 2026 22:35:56 +0100 Subject: [PATCH] Maillog --- migrations/Version20260103212025.php | 33 +++++++++ logo.png => public/images/logo.png | Bin src/Tech/Entity/EmailLog.php | 68 ++++++++++++++++++ .../EventListener/EmailLoggerListener.php | 54 ++++++++++++++ src/Tech/Repository/EmailLogRepository.php | 18 +++++ 5 files changed, 173 insertions(+) create mode 100644 migrations/Version20260103212025.php rename logo.png => public/images/logo.png (100%) create mode 100644 src/Tech/Entity/EmailLog.php create mode 100644 src/Tech/EventListener/EmailLoggerListener.php create mode 100644 src/Tech/Repository/EmailLogRepository.php diff --git a/migrations/Version20260103212025.php b/migrations/Version20260103212025.php new file mode 100644 index 0000000..ead1703 --- /dev/null +++ b/migrations/Version20260103212025.php @@ -0,0 +1,33 @@ +addSql('CREATE TABLE email_log (id INT AUTO_INCREMENT NOT NULL, user_id INT NOT NULL, email_identifier VARCHAR(255) NOT NULL, sent_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_6FB4883A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE email_log ADD CONSTRAINT FK_6FB4883A76ED395 FOREIGN KEY (user_id) REFERENCES `user` (id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE email_log DROP FOREIGN KEY FK_6FB4883A76ED395'); + $this->addSql('DROP TABLE email_log'); + } +} diff --git a/logo.png b/public/images/logo.png similarity index 100% rename from logo.png rename to public/images/logo.png diff --git a/src/Tech/Entity/EmailLog.php b/src/Tech/Entity/EmailLog.php new file mode 100644 index 0000000..5a295e4 --- /dev/null +++ b/src/Tech/Entity/EmailLog.php @@ -0,0 +1,68 @@ +id; + } + + public function getUser(): ?User + { + return $this->user; + } + + public function setUser(?User $user): static + { + $this->user = $user; + + return $this; + } + + public function getEmailIdentifier(): ?string + { + return $this->emailIdentifier; + } + + public function setEmailIdentifier(string $emailIdentifier): static + { + $this->emailIdentifier = $emailIdentifier; + + return $this; + } + + public function getSentAt(): ?\DateTimeImmutable + { + return $this->sentAt; + } + + public function setSentAt(\DateTimeImmutable $sentAt): static + { + $this->sentAt = $sentAt; + + return $this; + } +} diff --git a/src/Tech/EventListener/EmailLoggerListener.php b/src/Tech/EventListener/EmailLoggerListener.php new file mode 100644 index 0000000..848a825 --- /dev/null +++ b/src/Tech/EventListener/EmailLoggerListener.php @@ -0,0 +1,54 @@ +getMessage(); + if (!$message instanceof TemplatedEmail) { + return; + } + + $recipients = $message->getTo(); + foreach ($recipients as $recipient) { + if (!$recipient instanceof Address) { + continue; + } + + $user = $this->userRepository->findOneBy(['email' => $recipient->getAddress()]); + if (!$user) { + continue; + } + + $emailLog = new EmailLog(); + $emailLog->setUser($user); + $emailLog->setSentAt(new \DateTimeImmutable()); + + // Try to get the template name, or use the subject as identifier + $identifier = $message->getHtmlTemplate() ?: $message->getTextTemplate() ?: $message->getSubject(); + $emailLog->setEmailIdentifier($identifier); + + $this->entityManager->persist($emailLog); + } + + $this->entityManager->flush(); + } +} diff --git a/src/Tech/Repository/EmailLogRepository.php b/src/Tech/Repository/EmailLogRepository.php new file mode 100644 index 0000000..a15a2e3 --- /dev/null +++ b/src/Tech/Repository/EmailLogRepository.php @@ -0,0 +1,18 @@ + + */ +class EmailLogRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, EmailLog::class); + } +}