diff --git a/.env b/.env index f5fc27a..8c3b590 100644 --- a/.env +++ b/.env @@ -20,6 +20,9 @@ MSG_91_TO= MSG_91_FROM= TEST_EMAIL= TEST_FROM_EMAIL= +TEST_CC_EMAIL= +TEST_BCC_EMAIL= +TEST_BCC_NAME= VONAGE_API_KEY= VONAGE_API_SECRET= VONAGE_TO= diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 032c6b6..26ffc2d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,6 +36,9 @@ jobs: MSG_91_FROM: ${{ secrets.MSG_91_FROM }} TEST_EMAIL: ${{ secrets.TEST_EMAIL }} TEST_FROM_EMAIL: ${{ secrets.TEST_FROM_EMAIL }} + TEST_CC_EMAIL: ${{ secrets.TEST_CC_EMAIL }} + TEST_BCC_EMAIL: ${{ secrets.TEST_BCC_EMAIL }} + TEST_BCC_NAME: ${{ secrets.TEST_BCC_NAME }} VONAGE_API_KEY: ${{ secrets.VONAGE_API_KEY }} VONAGE_API_SECRET: ${{ secrets.VONAGE_API_SECRET }} VONAGE_TO: ${{ secrets.VONAGE_TO }} diff --git a/Dockerfile b/Dockerfile index e0e8538..5bc1e5e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM composer:2.0 as composer +FROM composer:2.0 AS composer ARG TESTING=false ENV TESTING=$TESTING @@ -15,7 +15,7 @@ RUN composer install \ --no-scripts \ --prefer-dist -FROM php:8.2.14-cli-alpine3.19 +FROM php:8.3.11-cli-alpine3.20 WORKDIR /usr/local/src/ diff --git a/docker-compose.yml b/docker-compose.yml index fb87cc4..4433790 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3.9' - services: tests: build: @@ -31,6 +29,9 @@ services: - MSG_91_FROM - TEST_EMAIL - TEST_FROM_EMAIL + - TEST_CC_EMAIL + - TEST_BCC_EMAIL + - TEST_BCC_NAME - VONAGE_API_KEY - VONAGE_API_SECRET - VONAGE_TO diff --git a/src/Utopia/Messaging/Adapter/Email/Mailgun.php b/src/Utopia/Messaging/Adapter/Email/Mailgun.php index 756a51e..52c4d37 100644 --- a/src/Utopia/Messaging/Adapter/Email/Mailgun.php +++ b/src/Utopia/Messaging/Adapter/Email/Mailgun.php @@ -58,20 +58,28 @@ protected function process(EmailMessage $message): array if (!\is_null($message->getCC())) { foreach ($message->getCC() as $cc) { - if (!empty($cc['name'])) { - $body['cc'] = "{$body['cc']},{$cc['name']}<{$cc['email']}>"; - } else { - $body['cc'] = "{$body['cc']}, <{$cc['email']}>"; + if (!empty($cc['email'])) { + $ccString = !empty($cc['name']) + ? "{$cc['name']}<{$cc['email']}>" + : $cc['email']; + + $body['cc'] = !empty($body['cc']) + ? "{$body['cc']},{$ccString}" + : $ccString; } } } if (!\is_null($message->getBCC())) { foreach ($message->getBCC() as $bcc) { - if (!empty($bcc['name'])) { - $body['bcc'] = "{$body['bcc']},{$bcc['name']}<{$bcc['email']}>"; - } else { - $body['bcc'] = "{$body['bcc']}, <{$bcc['email']}>"; + if (!empty($bcc['email'])) { + $bccString = !empty($bcc['name']) + ? "{$bcc['name']}<{$bcc['email']}>" + : $bcc['email']; + + $body['bcc'] = !empty($body['bcc']) + ? "{$body['bcc']},{$bccString}" + : $bccString; } } } diff --git a/src/Utopia/Messaging/Adapter/Email/Mock.php b/src/Utopia/Messaging/Adapter/Email/Mock.php index 2da5b41..54ff4e9 100644 --- a/src/Utopia/Messaging/Adapter/Email/Mock.php +++ b/src/Utopia/Messaging/Adapter/Email/Mock.php @@ -50,6 +50,18 @@ protected function process(EmailMessage $message): array $mail->addAddress($to); } + if (!empty($message->getCC())) { + foreach ($message->getCC() as $cc) { + $mail->addCC($cc['email'], $cc['name'] ?? ''); + } + } + + if (!empty($message->getBCC())) { + foreach ($message->getBCC() as $bcc) { + $mail->addBCC($bcc['email'], $bcc['name'] ?? ''); + } + } + if (!$mail->send()) { foreach ($message->getTo() as $to) { $response->addResult($to, $mail->ErrorInfo); diff --git a/src/Utopia/Messaging/Adapter/Email/SMTP.php b/src/Utopia/Messaging/Adapter/Email/SMTP.php index 0f0f313..fc569a4 100644 --- a/src/Utopia/Messaging/Adapter/Email/SMTP.php +++ b/src/Utopia/Messaging/Adapter/Email/SMTP.php @@ -78,13 +78,13 @@ protected function process(EmailMessage $message): array if (!empty($message->getCC())) { foreach ($message->getCC() as $cc) { - $mail->addCC($cc['email'], $cc['name']); + $mail->addCC($cc['email'], $cc['name'] ?? ''); } } if (!empty($message->getBCC())) { foreach ($message->getBCC() as $bcc) { - $mail->addBCC($bcc['email'], $bcc['name']); + $mail->addBCC($bcc['email'], $bcc['name'] ?? ''); } } diff --git a/src/Utopia/Messaging/Messages/Email.php b/src/Utopia/Messaging/Messages/Email.php index 17740a7..c53b540 100644 --- a/src/Utopia/Messaging/Messages/Email.php +++ b/src/Utopia/Messaging/Messages/Email.php @@ -45,16 +45,16 @@ public function __construct( if (!\is_null($this->cc)) { foreach ($this->cc as $recipient) { - if (!isset($recipient['name']) || !isset($recipient['email'])) { - throw new \InvalidArgumentException('Each recipient in cc must have a name and email'); + if (!isset($recipient['email'])) { + throw new \InvalidArgumentException('Each CC recipient must have at least an email'); } } } if (!\is_null($this->bcc)) { foreach ($this->bcc as $recipient) { - if (!isset($recipient['name']) || !isset($recipient['email'])) { - throw new \InvalidArgumentException('Each recipient in bcc must have a name and email'); + if (!isset($recipient['email'])) { + throw new \InvalidArgumentException('Each BCC recipient must have at least an email'); } } } diff --git a/tests/Messaging/Adapter/Email/EmailTest.php b/tests/Messaging/Adapter/Email/EmailTest.php index 2aaa03e..0782fed 100644 --- a/tests/Messaging/Adapter/Email/EmailTest.php +++ b/tests/Messaging/Adapter/Email/EmailTest.php @@ -17,6 +17,8 @@ public function testSendEmail(): void $content = 'Test Content'; $fromName = 'Test Sender'; $fromEmail = 'sender@localhost.test'; + $cc = [['email' => 'tester2@localhost.test']]; + $bcc = [['name' => 'Tester3', 'email' => 'tester3@localhost.test']]; $message = new Email( to: [$to], @@ -24,6 +26,8 @@ public function testSendEmail(): void content: $content, fromName: $fromName, fromEmail: $fromEmail, + cc: $cc, + bcc: $bcc, ); $response = $sender->send($message); @@ -33,7 +37,10 @@ public function testSendEmail(): void $this->assertResponse($response); $this->assertEquals($to, $lastEmail['to'][0]['address']); $this->assertEquals($fromEmail, $lastEmail['from'][0]['address']); + $this->assertEquals($fromName, $lastEmail['from'][0]['name']); $this->assertEquals($subject, $lastEmail['subject']); $this->assertEquals($content, \trim($lastEmail['text'])); + $this->assertEquals($cc[0]['email'], $lastEmail['cc'][0]['address']); + $this->assertEquals($bcc[0]['email'], $lastEmail['envelope']['to'][2]['address']); } } diff --git a/tests/Messaging/Adapter/Email/MailgunTest.php b/tests/Messaging/Adapter/Email/MailgunTest.php index 561f372..dcba4f5 100644 --- a/tests/Messaging/Adapter/Email/MailgunTest.php +++ b/tests/Messaging/Adapter/Email/MailgunTest.php @@ -24,6 +24,8 @@ public function testSendEmail(): void $subject = 'Test Subject'; $content = 'Test Content'; $fromEmail = 'sender@'.$domain; + $cc = [['email' => \getenv('TEST_CC_EMAIL')]]; + $bcc = [['name' => \getenv('TEST_BCC_NAME'), 'email' => \getenv('TEST_BCC_EMAIL')]]; $message = new Email( to: [$to], @@ -31,6 +33,8 @@ public function testSendEmail(): void content: $content, fromName: 'Test Sender', fromEmail: $fromEmail, + cc: $cc, + bcc: $bcc, ); $response = $sender->send($message); diff --git a/tests/Messaging/Adapter/Email/SendgridTest.php b/tests/Messaging/Adapter/Email/SendgridTest.php index 7a0e965..6270cfb 100644 --- a/tests/Messaging/Adapter/Email/SendgridTest.php +++ b/tests/Messaging/Adapter/Email/SendgridTest.php @@ -18,6 +18,8 @@ public function testSendEmail(): void $subject = 'Test Subject'; $content = 'Test Content'; $fromEmail = \getenv('TEST_FROM_EMAIL'); + $cc = [['email' => \getenv('TEST_CC_EMAIL')]]; + $bcc = [['name' => \getenv('TEST_BCC_NAME'), 'email' => \getenv('TEST_BCC_EMAIL')]]; $message = new Email( to: [$to], @@ -25,6 +27,8 @@ public function testSendEmail(): void content: $content, fromName: 'Tester', fromEmail: $fromEmail, + cc: $cc, + bcc: $bcc, ); $response = $sender->send($message);