Skip to content

Commit

Permalink
Merge pull request #87 from utopia-php/fix-cc-bcc
Browse files Browse the repository at this point in the history
Remove name requirement for CC/BCC
  • Loading branch information
abnegate authored Oct 9, 2024
2 parents 6e466d3 + 65ce3f4 commit b9dfafb
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM composer:2.0 as composer
FROM composer:2.0 AS composer

ARG TESTING=false
ENV TESTING=$TESTING
Expand All @@ -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/

Expand Down
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.9'

services:
tests:
build:
Expand Down Expand Up @@ -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
Expand Down
24 changes: 16 additions & 8 deletions src/Utopia/Messaging/Adapter/Email/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Utopia/Messaging/Adapter/Email/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Utopia/Messaging/Adapter/Email/SMTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? '');
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Utopia/Messaging/Messages/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Messaging/Adapter/Email/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ public function testSendEmail(): void
$content = 'Test Content';
$fromName = 'Test Sender';
$fromEmail = '[email protected]';
$cc = [['email' => '[email protected]']];
$bcc = [['name' => 'Tester3', 'email' => '[email protected]']];

$message = new Email(
to: [$to],
subject: $subject,
content: $content,
fromName: $fromName,
fromEmail: $fromEmail,
cc: $cc,
bcc: $bcc,
);

$response = $sender->send($message);
Expand All @@ -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']);
}
}
4 changes: 4 additions & 0 deletions tests/Messaging/Adapter/Email/MailgunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ 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],
subject: $subject,
content: $content,
fromName: 'Test Sender',
fromEmail: $fromEmail,
cc: $cc,
bcc: $bcc,
);

$response = $sender->send($message);
Expand Down
4 changes: 4 additions & 0 deletions tests/Messaging/Adapter/Email/SendgridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ 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],
subject: $subject,
content: $content,
fromName: 'Tester',
fromEmail: $fromEmail,
cc: $cc,
bcc: $bcc,
);

$response = $sender->send($message);
Expand Down

0 comments on commit b9dfafb

Please sign in to comment.