Skip to content

Commit 6414501

Browse files
committed
Fixes for multiple emails + format tweaks
1 parent 6bdfd96 commit 6414501

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# v4.0.0-rc.4
2+
## 02/27/2023
3+
4+
1. [](#bugfix)
5+
* Fixed for multiple recipients [#167](https://github.com/getgrav/grav-plugin-email/issues/167)
6+
* Fix for simple array format with names which wasn't working
7+
18
# v4.0.0-rc.3
29
## 10/27/2022
310

classes/Email.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function buildMessage(array $params, array $vars = []): Message
135135
// Create message object.
136136
$message = new Message();
137137
$headers = $message->getEmail()->getHeaders();
138+
$email = $message->getEmail();
138139

139140
// Extend parameters with defaults.
140141
$params += [
@@ -201,9 +202,9 @@ public function buildMessage(array $params, array $vars = []): Message
201202
case 'cc':
202203
case 'bcc':
203204
case 'reply_to':
204-
$recipients = $this->processRecipients($key, $params);
205-
foreach ($recipients as $address) {
206-
$message->$key($address);
205+
if ($recipients = $this->processRecipients($key, $params)) {
206+
$key = $key === 'reply_to' ? 'replyTo' : $key;
207+
$email->$key(...$recipients);
207208
}
208209
break;
209210
case 'tags':
@@ -241,9 +242,13 @@ protected function processRecipients(string $type, array $params): array
241242
if (is_array($recipients) && Utils::isAssoc($recipients)) {
242243
$list[] = $this->createAddress($recipients);
243244
} else {
244-
if (is_array($recipients[0])) {
245-
foreach ($recipients as $recipient) {
246-
$list[] = $this->createAddress($recipient);
245+
if (is_array($recipients)) {
246+
if (count($recipients) ===2 && $this->isValidEmail($recipients[0]) && is_string($recipients[1])) {
247+
$list[] = $this->createAddress($recipients);
248+
} else {
249+
foreach ($recipients as $recipient) {
250+
$list[] = $this->createAddress($recipient);
251+
}
247252
}
248253
} else {
249254
if (is_string($recipients) && Utils::contains($recipients, ',')) {
@@ -450,6 +455,11 @@ protected function jsonifyRecipients(array $recipients): string
450455
return json_encode($json);
451456
}
452457

458+
protected function isValidEmail($email): bool
459+
{
460+
return is_string($email) && filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
461+
}
462+
453463
/**
454464
* @return void
455465
* @deprecated 4.0 Switched from Swiftmailer to Symfony/Mailer - No longer supported

0 commit comments

Comments
 (0)