Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit 8edd260

Browse files
committed
feat(email): Allow adding plain text content
1 parent bead57e commit 8edd260

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20241208144321 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return 'Allow putting HTML content in an email';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql(<<<'SQL'
24+
ALTER TABLE email ADD text_content TEXT NOT NULL DEFAULT ''
25+
SQL);
26+
$this->addSql(<<<'SQL'
27+
ALTER TABLE email RENAME COLUMN content TO html_content
28+
SQL);
29+
}
30+
31+
public function down(Schema $schema): void
32+
{
33+
// this down() migration is auto-generated, please modify it to your needs
34+
$this->addSql(<<<'SQL'
35+
ALTER TABLE email RENAME COLUMN html_content TO content
36+
SQL);
37+
$this->addSql(<<<'SQL'
38+
ALTER TABLE email DROP text_content
39+
SQL);
40+
}
41+
}

src/Entity/Email.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ class Email
2525

2626
#[ORM\Column(type: Types::TEXT)]
2727
#[NotBlank]
28-
private string $content = '';
28+
private string $textContent = '';
29+
30+
#[ORM\Column(type: Types::TEXT)]
31+
#[NotBlank]
32+
private string $htmlContent = '';
2933

3034
/**
3135
* @var Collection<int, EmailDeliveryEvent>
@@ -63,14 +67,26 @@ public function setSubject(string $subject): static
6367
return $this;
6468
}
6569

66-
public function getContent(): string
70+
public function getTextContent(): string
71+
{
72+
return $this->textContent;
73+
}
74+
75+
public function setTextContent(string $textContent): static
76+
{
77+
$this->textContent = $textContent;
78+
79+
return $this;
80+
}
81+
82+
public function getHtmlContent(): string
6783
{
68-
return $this->content;
84+
return $this->htmlContent;
6985
}
7086

71-
public function setContent(string $content): static
87+
public function setHtmlContent(string $htmlContent): static
7288
{
73-
$this->content = $content;
89+
$this->htmlContent = $htmlContent;
7490

7591
return $this;
7692
}

src/EventSubscriber/EmailCreatedSubscriber.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,21 @@ public function onMessageEvent(MessageEvent $event): void
4545
return;
4646
}
4747

48-
$body = $message->getHtmlBody();
49-
if (!\is_string($body)) {
48+
$textBody = $message->getTextBody();
49+
if (!\is_string($textBody)) {
50+
$this->logger->warning('The message does not have an valid text body.', [
51+
'message' => $message,
52+
'body' => $textBody,
53+
]);
54+
55+
return;
56+
}
57+
58+
$htmlBody = $message->getHtmlBody();
59+
if (!\is_string($htmlBody)) {
5060
$this->logger->warning('The message does not have an valid HTML body.', [
5161
'message' => $message,
52-
'body' => $body,
62+
'body' => $htmlBody,
5363
]);
5464

5565
return;
@@ -68,7 +78,8 @@ public function onMessageEvent(MessageEvent $event): void
6878

6979
$email = (new EmailEntity())
7080
->setSubject($subject)
71-
->setContent($body)
81+
->setTextContent($textBody)
82+
->setHtmlContent($htmlBody)
7283
->setKind($kind);
7384
$this->entityManager->persist($email);
7485

tests/EventListener/EmailCreatedSubscriberTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function testTransactionalEmail(): void
2626

2727
$message = (new Email())
2828
->subject('subject')
29+
->text('body')
2930
->html('<div>body</div')
3031
->from('demo-dbplay@example.com')
3132
->to('test@example.com');
@@ -59,7 +60,8 @@ public function testTransactionalEmail(): void
5960
\assert($email instanceof EmailEntity);
6061

6162
self::assertEquals('subject', $email->getSubject());
62-
self::assertEquals('<div>body</div>', $email->getContent());
63+
self::assertEquals('body', $email->getTextContent());
64+
self::assertEquals('<div>body</div>', $email->getHtmlContent());
6365
self::assertEquals(EmailKind::Test, $email->getKind());
6466

6567
$emailInstance = $email;

0 commit comments

Comments
 (0)