From 6414501af4bcfbc018b2d526dc2dc79fc08bdb58 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 27 Feb 2023 18:35:18 -0700 Subject: [PATCH] Fixes for multiple emails + format tweaks --- CHANGELOG.md | 7 +++++++ classes/Email.php | 22 ++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74a0da0..a77b62f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v4.0.0-rc.4 +## 02/27/2023 + +1. [](#bugfix) + * Fixed for multiple recipients [#167](https://github.com/getgrav/grav-plugin-email/issues/167) + * Fix for simple array format with names which wasn't working + # v4.0.0-rc.3 ## 10/27/2022 diff --git a/classes/Email.php b/classes/Email.php index 94b8533..6ceedf8 100644 --- a/classes/Email.php +++ b/classes/Email.php @@ -135,6 +135,7 @@ public function buildMessage(array $params, array $vars = []): Message // Create message object. $message = new Message(); $headers = $message->getEmail()->getHeaders(); + $email = $message->getEmail(); // Extend parameters with defaults. $params += [ @@ -201,9 +202,9 @@ public function buildMessage(array $params, array $vars = []): Message case 'cc': case 'bcc': case 'reply_to': - $recipients = $this->processRecipients($key, $params); - foreach ($recipients as $address) { - $message->$key($address); + if ($recipients = $this->processRecipients($key, $params)) { + $key = $key === 'reply_to' ? 'replyTo' : $key; + $email->$key(...$recipients); } break; case 'tags': @@ -241,9 +242,13 @@ protected function processRecipients(string $type, array $params): array if (is_array($recipients) && Utils::isAssoc($recipients)) { $list[] = $this->createAddress($recipients); } else { - if (is_array($recipients[0])) { - foreach ($recipients as $recipient) { - $list[] = $this->createAddress($recipient); + if (is_array($recipients)) { + if (count($recipients) ===2 && $this->isValidEmail($recipients[0]) && is_string($recipients[1])) { + $list[] = $this->createAddress($recipients); + } else { + foreach ($recipients as $recipient) { + $list[] = $this->createAddress($recipient); + } } } else { if (is_string($recipients) && Utils::contains($recipients, ',')) { @@ -450,6 +455,11 @@ protected function jsonifyRecipients(array $recipients): string return json_encode($json); } + protected function isValidEmail($email): bool + { + return is_string($email) && filter_var($email, FILTER_VALIDATE_EMAIL) !== false; + } + /** * @return void * @deprecated 4.0 Switched from Swiftmailer to Symfony/Mailer - No longer supported