Skip to content

Commit b9dfafb

Browse files
authored
Merge pull request #87 from utopia-php/fix-cc-bcc
Remove name requirement for CC/BCC
2 parents 6e466d3 + 65ce3f4 commit b9dfafb

File tree

11 files changed

+60
-18
lines changed

11 files changed

+60
-18
lines changed

.env

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ MSG_91_TO=
2020
MSG_91_FROM=
2121
TEST_EMAIL=
2222
TEST_FROM_EMAIL=
23+
TEST_CC_EMAIL=
24+
TEST_BCC_EMAIL=
25+
TEST_BCC_NAME=
2326
VONAGE_API_KEY=
2427
VONAGE_API_SECRET=
2528
VONAGE_TO=

.github/workflows/test.yml

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ jobs:
3636
MSG_91_FROM: ${{ secrets.MSG_91_FROM }}
3737
TEST_EMAIL: ${{ secrets.TEST_EMAIL }}
3838
TEST_FROM_EMAIL: ${{ secrets.TEST_FROM_EMAIL }}
39+
TEST_CC_EMAIL: ${{ secrets.TEST_CC_EMAIL }}
40+
TEST_BCC_EMAIL: ${{ secrets.TEST_BCC_EMAIL }}
41+
TEST_BCC_NAME: ${{ secrets.TEST_BCC_NAME }}
3942
VONAGE_API_KEY: ${{ secrets.VONAGE_API_KEY }}
4043
VONAGE_API_SECRET: ${{ secrets.VONAGE_API_SECRET }}
4144
VONAGE_TO: ${{ secrets.VONAGE_TO }}

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM composer:2.0 as composer
1+
FROM composer:2.0 AS composer
22

33
ARG TESTING=false
44
ENV TESTING=$TESTING
@@ -15,7 +15,7 @@ RUN composer install \
1515
--no-scripts \
1616
--prefer-dist
1717

18-
FROM php:8.2.14-cli-alpine3.19
18+
FROM php:8.3.11-cli-alpine3.20
1919

2020
WORKDIR /usr/local/src/
2121

docker-compose.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.9'
2-
31
services:
42
tests:
53
build:
@@ -31,6 +29,9 @@ services:
3129
- MSG_91_FROM
3230
- TEST_EMAIL
3331
- TEST_FROM_EMAIL
32+
- TEST_CC_EMAIL
33+
- TEST_BCC_EMAIL
34+
- TEST_BCC_NAME
3435
- VONAGE_API_KEY
3536
- VONAGE_API_SECRET
3637
- VONAGE_TO

src/Utopia/Messaging/Adapter/Email/Mailgun.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,28 @@ protected function process(EmailMessage $message): array
5858

5959
if (!\is_null($message->getCC())) {
6060
foreach ($message->getCC() as $cc) {
61-
if (!empty($cc['name'])) {
62-
$body['cc'] = "{$body['cc']},{$cc['name']}<{$cc['email']}>";
63-
} else {
64-
$body['cc'] = "{$body['cc']}, <{$cc['email']}>";
61+
if (!empty($cc['email'])) {
62+
$ccString = !empty($cc['name'])
63+
? "{$cc['name']}<{$cc['email']}>"
64+
: $cc['email'];
65+
66+
$body['cc'] = !empty($body['cc'])
67+
? "{$body['cc']},{$ccString}"
68+
: $ccString;
6569
}
6670
}
6771
}
6872

6973
if (!\is_null($message->getBCC())) {
7074
foreach ($message->getBCC() as $bcc) {
71-
if (!empty($bcc['name'])) {
72-
$body['bcc'] = "{$body['bcc']},{$bcc['name']}<{$bcc['email']}>";
73-
} else {
74-
$body['bcc'] = "{$body['bcc']}, <{$bcc['email']}>";
75+
if (!empty($bcc['email'])) {
76+
$bccString = !empty($bcc['name'])
77+
? "{$bcc['name']}<{$bcc['email']}>"
78+
: $bcc['email'];
79+
80+
$body['bcc'] = !empty($body['bcc'])
81+
? "{$body['bcc']},{$bccString}"
82+
: $bccString;
7583
}
7684
}
7785
}

src/Utopia/Messaging/Adapter/Email/Mock.php

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ protected function process(EmailMessage $message): array
5050
$mail->addAddress($to);
5151
}
5252

53+
if (!empty($message->getCC())) {
54+
foreach ($message->getCC() as $cc) {
55+
$mail->addCC($cc['email'], $cc['name'] ?? '');
56+
}
57+
}
58+
59+
if (!empty($message->getBCC())) {
60+
foreach ($message->getBCC() as $bcc) {
61+
$mail->addBCC($bcc['email'], $bcc['name'] ?? '');
62+
}
63+
}
64+
5365
if (!$mail->send()) {
5466
foreach ($message->getTo() as $to) {
5567
$response->addResult($to, $mail->ErrorInfo);

src/Utopia/Messaging/Adapter/Email/SMTP.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ protected function process(EmailMessage $message): array
7878

7979
if (!empty($message->getCC())) {
8080
foreach ($message->getCC() as $cc) {
81-
$mail->addCC($cc['email'], $cc['name']);
81+
$mail->addCC($cc['email'], $cc['name'] ?? '');
8282
}
8383
}
8484

8585
if (!empty($message->getBCC())) {
8686
foreach ($message->getBCC() as $bcc) {
87-
$mail->addBCC($bcc['email'], $bcc['name']);
87+
$mail->addBCC($bcc['email'], $bcc['name'] ?? '');
8888
}
8989
}
9090

src/Utopia/Messaging/Messages/Email.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ public function __construct(
4545

4646
if (!\is_null($this->cc)) {
4747
foreach ($this->cc as $recipient) {
48-
if (!isset($recipient['name']) || !isset($recipient['email'])) {
49-
throw new \InvalidArgumentException('Each recipient in cc must have a name and email');
48+
if (!isset($recipient['email'])) {
49+
throw new \InvalidArgumentException('Each CC recipient must have at least an email');
5050
}
5151
}
5252
}
5353

5454
if (!\is_null($this->bcc)) {
5555
foreach ($this->bcc as $recipient) {
56-
if (!isset($recipient['name']) || !isset($recipient['email'])) {
57-
throw new \InvalidArgumentException('Each recipient in bcc must have a name and email');
56+
if (!isset($recipient['email'])) {
57+
throw new \InvalidArgumentException('Each BCC recipient must have at least an email');
5858
}
5959
}
6060
}

tests/Messaging/Adapter/Email/EmailTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ public function testSendEmail(): void
1717
$content = 'Test Content';
1818
$fromName = 'Test Sender';
1919
$fromEmail = '[email protected]';
20+
$cc = [['email' => '[email protected]']];
21+
$bcc = [['name' => 'Tester3', 'email' => '[email protected]']];
2022

2123
$message = new Email(
2224
to: [$to],
2325
subject: $subject,
2426
content: $content,
2527
fromName: $fromName,
2628
fromEmail: $fromEmail,
29+
cc: $cc,
30+
bcc: $bcc,
2731
);
2832

2933
$response = $sender->send($message);
@@ -33,7 +37,10 @@ public function testSendEmail(): void
3337
$this->assertResponse($response);
3438
$this->assertEquals($to, $lastEmail['to'][0]['address']);
3539
$this->assertEquals($fromEmail, $lastEmail['from'][0]['address']);
40+
$this->assertEquals($fromName, $lastEmail['from'][0]['name']);
3641
$this->assertEquals($subject, $lastEmail['subject']);
3742
$this->assertEquals($content, \trim($lastEmail['text']));
43+
$this->assertEquals($cc[0]['email'], $lastEmail['cc'][0]['address']);
44+
$this->assertEquals($bcc[0]['email'], $lastEmail['envelope']['to'][2]['address']);
3845
}
3946
}

tests/Messaging/Adapter/Email/MailgunTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ public function testSendEmail(): void
2424
$subject = 'Test Subject';
2525
$content = 'Test Content';
2626
$fromEmail = 'sender@'.$domain;
27+
$cc = [['email' => \getenv('TEST_CC_EMAIL')]];
28+
$bcc = [['name' => \getenv('TEST_BCC_NAME'), 'email' => \getenv('TEST_BCC_EMAIL')]];
2729

2830
$message = new Email(
2931
to: [$to],
3032
subject: $subject,
3133
content: $content,
3234
fromName: 'Test Sender',
3335
fromEmail: $fromEmail,
36+
cc: $cc,
37+
bcc: $bcc,
3438
);
3539

3640
$response = $sender->send($message);

tests/Messaging/Adapter/Email/SendgridTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ public function testSendEmail(): void
1818
$subject = 'Test Subject';
1919
$content = 'Test Content';
2020
$fromEmail = \getenv('TEST_FROM_EMAIL');
21+
$cc = [['email' => \getenv('TEST_CC_EMAIL')]];
22+
$bcc = [['name' => \getenv('TEST_BCC_NAME'), 'email' => \getenv('TEST_BCC_EMAIL')]];
2123

2224
$message = new Email(
2325
to: [$to],
2426
subject: $subject,
2527
content: $content,
2628
fromName: 'Tester',
2729
fromEmail: $fromEmail,
30+
cc: $cc,
31+
bcc: $bcc,
2832
);
2933

3034
$response = $sender->send($message);

0 commit comments

Comments
 (0)