Skip to content

Commit a6ac04f

Browse files
authored
Merge pull request #114 from utopia-php/feat-recipient-value-object
feat: add name support to email 'to' field, matching cc/bcc format
2 parents de06865 + cf050ec commit a6ac04f

File tree

9 files changed

+265
-99
lines changed

9 files changed

+265
-99
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
}
2323
},
2424
"require": {
25-
"php": ">=8.0.0",
25+
"php": ">=8.1.0",
2626
"ext-curl": "*",
2727
"ext-openssl": "*",
2828
"phpmailer/phpmailer": "6.9.1",

composer.lock

Lines changed: 25 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,32 @@ protected function process(EmailMessage $message): array
5151

5252
$domain = $this->isEU ? $euDomain : $usDomain;
5353

54+
$recipients = $message->getTo();
55+
$toEmails = \array_map(fn ($to) => $to['email'], $recipients);
56+
5457
$body = [
55-
'to' => \implode(',', $message->getTo()),
56-
'from' => "{$message->getFromName()}<{$message->getFromEmail()}>",
58+
'to' => \implode(',', \array_map(
59+
fn ($to) => !empty($to['name'])
60+
? "{$to['name']} <{$to['email']}>"
61+
: $to['email'],
62+
$recipients
63+
)),
64+
'from' => "{$message->getFromName()} <{$message->getFromEmail()}>",
5765
'subject' => $message->getSubject(),
5866
'text' => $message->isHtml() ? null : $message->getContent(),
5967
'html' => $message->isHtml() ? $message->getContent() : null,
60-
'h:Reply-To: '."{$message->getReplyToName()}<{$message->getReplyToEmail()}>",
68+
'h:Reply-To: '."{$message->getReplyToName()} <{$message->getReplyToEmail()}>",
6169
];
6270

63-
if (\count($message->getTo()) > 1) {
64-
$body['recipient-variables'] = json_encode(array_fill_keys($message->getTo(), []));
71+
if (\count($recipients) > 1) {
72+
$body['recipient-variables'] = json_encode(array_fill_keys($toEmails, []));
6573
}
6674

6775
if (!\is_null($message->getCC())) {
6876
foreach ($message->getCC() as $cc) {
6977
if (!empty($cc['email'])) {
7078
$ccString = !empty($cc['name'])
71-
? "{$cc['name']}<{$cc['email']}>"
79+
? "{$cc['name']} <{$cc['email']}>"
7280
: $cc['email'];
7381

7482
$body['cc'] = !empty($body['cc'])
@@ -82,7 +90,7 @@ protected function process(EmailMessage $message): array
8290
foreach ($message->getBCC() as $bcc) {
8391
if (!empty($bcc['email'])) {
8492
$bccString = !empty($bcc['name'])
85-
? "{$bcc['name']}<{$bcc['email']}>"
93+
? "{$bcc['name']} <{$bcc['email']}>"
8694
: $bcc['email'];
8795

8896
$body['bcc'] = !empty($body['bcc'])
@@ -140,16 +148,16 @@ protected function process(EmailMessage $message): array
140148
if ($statusCode >= 200 && $statusCode < 300) {
141149
$response->setDeliveredTo(\count($message->getTo()));
142150
foreach ($message->getTo() as $to) {
143-
$response->addResult($to);
151+
$response->addResult($to['email']);
144152
}
145153
} elseif ($statusCode >= 400 && $statusCode < 500) {
146154
foreach ($message->getTo() as $to) {
147155
if (\is_string($result['response'])) {
148-
$response->addResult($to, $result['response']);
156+
$response->addResult($to['email'], $result['response']);
149157
} elseif (isset($result['response']['message'])) {
150-
$response->addResult($to, $result['response']['message']);
158+
$response->addResult($to['email'], $result['response']['message']);
151159
} else {
152-
$response->addResult($to, 'Unknown error');
160+
$response->addResult($to['email'], 'Unknown error');
153161
}
154162
}
155163
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected function process(EmailMessage $message): array
4747
$mail->isHTML($message->isHtml());
4848

4949
foreach ($message->getTo() as $to) {
50-
$mail->addAddress($to);
50+
$mail->addAddress($to['email'], $to['name'] ?? '');
5151
}
5252

5353
if (!empty($message->getCC())) {
@@ -64,12 +64,12 @@ protected function process(EmailMessage $message): array
6464

6565
if (!$mail->send()) {
6666
foreach ($message->getTo() as $to) {
67-
$response->addResult($to, $mail->ErrorInfo);
67+
$response->addResult($to['email'], $mail->ErrorInfo);
6868
}
6969
} else {
7070
$response->setDeliveredTo(\count($message->getTo()));
7171
foreach ($message->getTo() as $to) {
72-
$response->addResult($to);
72+
$response->addResult($to['email']);
7373
}
7474
}
7575

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

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,15 @@ protected function process(EmailMessage $message): array
7777

7878
$emails = [];
7979
foreach ($message->getTo() as $to) {
80+
$toFormatted = !empty($to['name'])
81+
? "{$to['name']} <{$to['email']}>"
82+
: $to['email'];
83+
8084
$email = [
8185
'from' => $message->getFromName()
8286
? "{$message->getFromName()} <{$message->getFromEmail()}>"
8387
: $message->getFromEmail(),
84-
'to' => [$to],
88+
'to' => [$toFormatted],
8589
'subject' => $message->getSubject(),
8690
];
8791

@@ -98,35 +102,27 @@ protected function process(EmailMessage $message): array
98102
}
99103

100104
if (! \is_null($message->getCC()) && ! empty($message->getCC())) {
101-
$ccList = [];
102-
foreach ($message->getCC() as $cc) {
103-
if (! empty($cc['email'])) {
104-
$ccList[] = ! empty($cc['name'])
105-
? "{$cc['name']} <{$cc['email']}>"
106-
: $cc['email'];
107-
}
108-
}
109-
if (! empty($ccList)) {
110-
$email['cc'] = $ccList;
111-
}
105+
$ccList = \array_map(
106+
fn ($cc) => ! empty($cc['name'])
107+
? "{$cc['name']} <{$cc['email']}>"
108+
: $cc['email'],
109+
$message->getCC()
110+
);
111+
$email['cc'] = $ccList;
112112
}
113113

114114
if (! empty($attachments)) {
115115
$email['attachments'] = $attachments;
116116
}
117117

118118
if (! \is_null($message->getBCC()) && ! empty($message->getBCC())) {
119-
$bccList = [];
120-
foreach ($message->getBCC() as $bcc) {
121-
if (! empty($bcc['email'])) {
122-
$bccList[] = ! empty($bcc['name'])
123-
? "{$bcc['name']} <{$bcc['email']}>"
124-
: $bcc['email'];
125-
}
126-
}
127-
if (! empty($bccList)) {
128-
$email['bcc'] = $bccList;
129-
}
119+
$bccList = \array_map(
120+
fn ($bcc) => ! empty($bcc['name'])
121+
? "{$bcc['name']} <{$bcc['email']}>"
122+
: $bcc['email'],
123+
$message->getBCC()
124+
);
125+
$email['bcc'] = $bccList;
130126
}
131127

132128
$emails[] = $email;
@@ -157,9 +153,9 @@ protected function process(EmailMessage $message): array
157153

158154
foreach ($message->getTo() as $index => $to) {
159155
if (isset($failedIndices[$index])) {
160-
$response->addResult($to, $failedIndices[$index]);
156+
$response->addResult($to['email'], $failedIndices[$index]);
161157
} else {
162-
$response->addResult($to);
158+
$response->addResult($to['email']);
163159
}
164160
}
165161

@@ -168,7 +164,7 @@ protected function process(EmailMessage $message): array
168164
} else {
169165
$response->setDeliveredTo(\count($message->getTo()));
170166
foreach ($message->getTo() as $to) {
171-
$response->addResult($to);
167+
$response->addResult($to['email']);
172168
}
173169
}
174170
} elseif ($statusCode >= 400 && $statusCode < 500) {
@@ -183,7 +179,7 @@ protected function process(EmailMessage $message): array
183179
}
184180

185181
foreach ($message->getTo() as $to) {
186-
$response->addResult($to, $errorMessage);
182+
$response->addResult($to['email'], $errorMessage);
187183
}
188184
} elseif ($statusCode >= 500) {
189185
$errorMessage = 'Server error';
@@ -195,7 +191,7 @@ protected function process(EmailMessage $message): array
195191
}
196192

197193
foreach ($message->getTo() as $to) {
198-
$response->addResult($to, $errorMessage);
194+
$response->addResult($to['email'], $errorMessage);
199195
}
200196
}
201197

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function process(EmailMessage $message): array
9797
$mail->AltBody = \trim($mail->AltBody);
9898

9999
foreach ($message->getTo() as $to) {
100-
$mail->addAddress($to);
100+
$mail->addAddress($to['email'], $to['name'] ?? '');
101101
}
102102

103103
if (!empty($message->getCC())) {
@@ -158,18 +158,18 @@ protected function process(EmailMessage $message): array
158158
? 'Unknown error'
159159
: $mail->ErrorInfo;
160160

161-
$response->addResult($to, $sent ? '' : $error);
161+
$response->addResult($to['email'], $sent ? '' : $error);
162162
}
163163

164-
foreach ($message->getCC() as $cc) {
164+
foreach ($message->getCC() ?? [] as $cc) {
165165
$error = empty($mail->ErrorInfo)
166166
? 'Unknown error'
167167
: $mail->ErrorInfo;
168168

169169
$response->addResult($cc['email'], $sent ? '' : $error);
170170
}
171171

172-
foreach ($message->getBCC() as $bcc) {
172+
foreach ($message->getBCC() ?? [] as $bcc) {
173173
$error = empty($mail->ErrorInfo)
174174
? 'Unknown error'
175175
: $mail->ErrorInfo;

0 commit comments

Comments
 (0)