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

Commit 71171c0

Browse files
committed
feat(email): Send to test email
1 parent 1a24ea7 commit 71171c0

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

config/services.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
->set('app.redis_uri', env('REDIS_URI'))
1919
->set('app.openai_api_key', env('OPENAI_API_KEY'))
2020
->set('app.server-mail', env('SERVER_EMAIL'))
21+
->set('app.server-email-for-test', env('SERVER_EMAIL_FOR_TEST'))
2122
->set('app.mail.bcc-chunk', 10)
2223
->set('app.features.hint', true)
2324
->set('app.features.editable-profile', true)
@@ -52,6 +53,7 @@
5253

5354
$services->set(EmailService::class)
5455
->arg('$serverMail', param('app.server-mail'))
56+
->arg('$serverMailForTest', param('app.server-email-for-test'))
5557
->arg('$chunkLimit', param('app.mail.bcc-chunk'))
5658
;
5759

src/Service/EmailService.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,26 @@
1212
final readonly class EmailService
1313
{
1414
private Address $fromAddress;
15+
private Address $testAddress;
1516

1617
/**
1718
* @param int<1, max> $chunkLimit
1819
*/
1920
public function __construct(
2021
private MailerInterface $mailer,
2122
private string $serverMail,
23+
private string $serverMailForTest,
2224
private int $chunkLimit,
2325
) {
2426
$this->fromAddress = new Address(
2527
address: $this->serverMail,
2628
name: '資料庫練功房'
2729
);
30+
31+
$this->testAddress = new Address(
32+
address: $this->serverMailForTest,
33+
name: '資料庫練功房 - 測試信箱'
34+
);
2835
}
2936

3037
/**
@@ -58,4 +65,15 @@ public function send(EmailDto $emailDto): void
5865
$this->mailer->send($email);
5966
}
6067
}
68+
69+
/**
70+
* Send an email with the given {@link EmailDto} to the test email address.
71+
*/
72+
public function sendToTest(EmailDto $emailDto): void
73+
{
74+
$emailDtoCloned = clone $emailDto;
75+
$emailDtoCloned->setToAddress($this->testAddress);
76+
$email = $emailDtoCloned->toEmail()->from($this->fromAddress);
77+
$this->mailer->send($email);
78+
}
6179
}

tests/Service/EmailServiceTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testSendWithoutBcc(): void
3333
})
3434
;
3535

36-
$emailService = new EmailService($mailer, 'test@example.com', 10);
36+
$emailService = new EmailService($mailer, 'test@example.com', '', 10);
3737
$emailDto = (new EmailDto())
3838
->setSubject('Test')
3939
->setToAddress('test2@gmail.com')
@@ -60,7 +60,7 @@ public function testSendWithBcc(): void
6060
})
6161
;
6262

63-
$emailService = new EmailService($mailer, 'test@example.com', 10);
63+
$emailService = new EmailService($mailer, 'test@example.com', '', 10);
6464
$emailDto = (new EmailDto())
6565
->setSubject('Test')
6666
->setToAddress('test2@gmail.com')
@@ -107,7 +107,7 @@ public function testSendWithChunkedBcc(): void
107107
})
108108
;
109109

110-
$emailService = new EmailService($mailer, 'test@example.com', 10);
110+
$emailService = new EmailService($mailer, 'test@example.com', '', 10);
111111
$emailDto = (new EmailDto())
112112
->setSubject('Test')
113113
->setToAddress('test@example.com')
@@ -122,4 +122,30 @@ public function testSendWithChunkedBcc(): void
122122

123123
$emailService->send($emailDto);
124124
}
125+
126+
public function testSendToTestEmail(): void
127+
{
128+
$mailer = $this->createMock(MailerInterface::class);
129+
$mailer->expects(self::once())
130+
->method('send')
131+
->willReturnCallback(static function (Email $email): void {
132+
self::assertSame('Test', $email->getSubject());
133+
self::assertSame('test@example.com', $email->getFrom()[0]->getAddress());
134+
self::assertSame('test+target@example.com', $email->getTo()[0]->getAddress());
135+
self::assertSame('Test TEXT', $email->getTextBody());
136+
self::assertSame('Test HTML', $email->getHtmlBody());
137+
})
138+
;
139+
140+
$emailService = new EmailService($mailer, 'test@example.com', 'test+target@example.com', 10);
141+
142+
$emailDto = (new EmailDto())
143+
->setSubject('Test')
144+
->setKind(EmailKind::Test)
145+
->setText('Test TEXT')
146+
->setHtml('Test HTML')
147+
;
148+
149+
$emailService->sendToTest($emailDto);
150+
}
125151
}

0 commit comments

Comments
 (0)