From b353c6e2cf5cee33f18ae8288a7e7daded1a21d0 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Tue, 5 Dec 2023 14:05:22 +0100 Subject: [PATCH 1/3] updates cc and bcc for email --- .../Messaging/Adapter/Email/Mailgun.php | 8 ++++---- .../Messaging/Adapter/Email/Sendgrid.php | 2 -- src/Utopia/Messaging/Adapter/SMS/Mock.php | 20 +++++++++++++++++-- src/Utopia/Messaging/Messages/Email.php | 8 ++++---- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Utopia/Messaging/Adapter/Email/Mailgun.php b/src/Utopia/Messaging/Adapter/Email/Mailgun.php index fda3e29c..77c119b7 100644 --- a/src/Utopia/Messaging/Adapter/Email/Mailgun.php +++ b/src/Utopia/Messaging/Adapter/Email/Mailgun.php @@ -54,14 +54,14 @@ protected function process(EmailMessage $message): string ]; if (! \is_null($message->getCC())) { - foreach ($message->getCC() as $cc) { - $body['cc'] = "{$body['cc']},{$cc['name']}<{$cc['email']}>"; + foreach ($message->getCC() as $ccEmail) { + $body['cc'] = "{$body['cc']}, <{$ccEmail}>"; } } if (! \is_null($message->getBCC())) { - foreach ($message->getBCC() as $bcc) { - $body['bcc'] = "{$body['bcc']},{$bcc['name']}<{$bcc['email']}>"; + foreach ($message->getBCC() as $bccEmail) { + $body['bcc'] = "{$body['bcc']},<{$bccEmail}>"; } } diff --git a/src/Utopia/Messaging/Adapter/Email/Sendgrid.php b/src/Utopia/Messaging/Adapter/Email/Sendgrid.php index db6ef4bc..e2d7b767 100644 --- a/src/Utopia/Messaging/Adapter/Email/Sendgrid.php +++ b/src/Utopia/Messaging/Adapter/Email/Sendgrid.php @@ -50,7 +50,6 @@ protected function process(EmailMessage $message): string if (! \is_null($message->getCC())) { foreach ($message->getCC() as $cc) { $personalizations[0]['cc'][] = [ - 'name' => $cc['name'], 'email' => $cc['email'], ]; } @@ -59,7 +58,6 @@ protected function process(EmailMessage $message): string if (! \is_null($message->getBCC())) { foreach ($message->getBCC() as $bcc) { $personalizations[0]['bcc'][] = [ - 'name' => $bcc['name'], 'email' => $bcc['email'], ]; } diff --git a/src/Utopia/Messaging/Adapter/SMS/Mock.php b/src/Utopia/Messaging/Adapter/SMS/Mock.php index e89f3428..c8b82656 100644 --- a/src/Utopia/Messaging/Adapter/SMS/Mock.php +++ b/src/Utopia/Messaging/Adapter/SMS/Mock.php @@ -4,6 +4,7 @@ use Utopia\Messaging\Adapter\SMS as SMSAdapter; use Utopia\Messaging\Messages\SMS as SMSMessage; +use Utopia\Messaging\Response; class Mock extends SMSAdapter { @@ -24,7 +25,7 @@ public function getName(): string public function getMaxMessagesPerRequest(): int { - return 1; + return 1000; } /** @@ -34,6 +35,10 @@ public function getMaxMessagesPerRequest(): int */ protected function process(SMSMessage $message): string { + $response = new Response($this->getType()); + + $response->setDeliveredTo(\count($message->getTo())); + $result = $this->request( method: 'POST', url: 'http://request-catcher:5000/mock-sms', @@ -49,6 +54,17 @@ protected function process(SMSMessage $message): string ]), ); - return \json_encode($result['response']); + if ($result['statusCode'] === 200) { + $response->setDeliveredTo(\count($message->getTo())); + foreach ($message->getTo() as $to) { + $response->addResultForRecipient($to); + } + } else { + foreach ($message->getTo() as $to) { + $response->addResultForRecipient($to, 'Unknown Error.'); + } + } + + return \json_encode($response->toArray()); } } diff --git a/src/Utopia/Messaging/Messages/Email.php b/src/Utopia/Messaging/Messages/Email.php index 268a4dde..2d4c17f2 100644 --- a/src/Utopia/Messaging/Messages/Email.php +++ b/src/Utopia/Messaging/Messages/Email.php @@ -12,8 +12,8 @@ class Email implements Message * @param string $content The content of the email. * @param string $fromName The name of the sender. * @param string $fromEmail The email address of the sender. - * @param array>|null $cc . The CC recipients of the email. Each recipient should be an array containing a "name" and an "email" key. - * @param array>|null $bcc . The BCC recipients of the email. Each recipient should be an array containing a "name" and an "email" key. + * @param array|null $cc . The CC recipients of the email. Each recipient should be an email. + * @param array|null $bcc . The BCC recipients of the email. Each recipient should be an email. * @param string|null $replyToName The name of the reply to. * @param string|null $replyToEmail The email address of the reply to. * @param array|null $attachments The attachments of the email. @@ -98,7 +98,7 @@ public function getReplyToEmail(): string } /** - * @return array>|null + * @return array|null */ public function getCC(): ?array { @@ -106,7 +106,7 @@ public function getCC(): ?array } /** - * @return array>|null + * @return array|null */ public function getBCC(): ?array { From 12384ad4d83293e04613d103e189b1bee58b9e93 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Tue, 5 Dec 2023 14:24:47 +0100 Subject: [PATCH 2/3] sends ccname and bccname as optional --- .../Messaging/Adapter/Email/Mailgun.php | 16 +++++++++--- .../Messaging/Adapter/Email/Sendgrid.php | 26 ++++++++++++++----- src/Utopia/Messaging/Messages/Email.php | 8 +++--- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/Utopia/Messaging/Adapter/Email/Mailgun.php b/src/Utopia/Messaging/Adapter/Email/Mailgun.php index 77c119b7..66fda1b6 100644 --- a/src/Utopia/Messaging/Adapter/Email/Mailgun.php +++ b/src/Utopia/Messaging/Adapter/Email/Mailgun.php @@ -54,14 +54,22 @@ protected function process(EmailMessage $message): string ]; if (! \is_null($message->getCC())) { - foreach ($message->getCC() as $ccEmail) { - $body['cc'] = "{$body['cc']}, <{$ccEmail}>"; + 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 (! \is_null($message->getBCC())) { - foreach ($message->getBCC() as $bccEmail) { - $body['bcc'] = "{$body['bcc']},<{$bccEmail}>"; + foreach ($message->getBCC() as $bcc) { + if (!empty($bcc['name'])) { + $body['bcc'] = "{$body['bcc']},{$bcc['name']}<{$bcc['email']}>"; + } else { + $body['bcc'] = "{$body['bcc']}, <{$bcc['email']}>"; + } } } diff --git a/src/Utopia/Messaging/Adapter/Email/Sendgrid.php b/src/Utopia/Messaging/Adapter/Email/Sendgrid.php index e2d7b767..14f3e308 100644 --- a/src/Utopia/Messaging/Adapter/Email/Sendgrid.php +++ b/src/Utopia/Messaging/Adapter/Email/Sendgrid.php @@ -49,17 +49,31 @@ protected function process(EmailMessage $message): string if (! \is_null($message->getCC())) { foreach ($message->getCC() as $cc) { - $personalizations[0]['cc'][] = [ - 'email' => $cc['email'], - ]; + if (!empty($cc['name'])) { + $personalizations[0]['cc'][] = [ + 'name' => $cc['name'], + 'email' => $cc['email'], + ]; + } else { + $personalizations[0]['cc'][] = [ + 'email' => $cc['email'], + ]; + } } } if (! \is_null($message->getBCC())) { foreach ($message->getBCC() as $bcc) { - $personalizations[0]['bcc'][] = [ - 'email' => $bcc['email'], - ]; + if (!empty($bcc['name'])) { + $personalizations[0]['bcc'][] = [ + 'name' => $bcc['name'], + 'email' => $bcc['email'], + ]; + } else { + $personalizations[0]['bcc'][] = [ + 'email' => $bcc['email'], + ]; + } } } diff --git a/src/Utopia/Messaging/Messages/Email.php b/src/Utopia/Messaging/Messages/Email.php index 2d4c17f2..268a4dde 100644 --- a/src/Utopia/Messaging/Messages/Email.php +++ b/src/Utopia/Messaging/Messages/Email.php @@ -12,8 +12,8 @@ class Email implements Message * @param string $content The content of the email. * @param string $fromName The name of the sender. * @param string $fromEmail The email address of the sender. - * @param array|null $cc . The CC recipients of the email. Each recipient should be an email. - * @param array|null $bcc . The BCC recipients of the email. Each recipient should be an email. + * @param array>|null $cc . The CC recipients of the email. Each recipient should be an array containing a "name" and an "email" key. + * @param array>|null $bcc . The BCC recipients of the email. Each recipient should be an array containing a "name" and an "email" key. * @param string|null $replyToName The name of the reply to. * @param string|null $replyToEmail The email address of the reply to. * @param array|null $attachments The attachments of the email. @@ -98,7 +98,7 @@ public function getReplyToEmail(): string } /** - * @return array|null + * @return array>|null */ public function getCC(): ?array { @@ -106,7 +106,7 @@ public function getCC(): ?array } /** - * @return array|null + * @return array>|null */ public function getBCC(): ?array { From 005045bc75eddcd2174c312c2bf940084e3a0fee Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Tue, 5 Dec 2023 14:25:11 +0100 Subject: [PATCH 3/3] lint fix --- src/Utopia/Messaging/Adapter/Email/Mailgun.php | 4 ++-- src/Utopia/Messaging/Adapter/Email/Sendgrid.php | 4 ++-- src/Utopia/Messaging/Adapter/SMS/Mock.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Utopia/Messaging/Adapter/Email/Mailgun.php b/src/Utopia/Messaging/Adapter/Email/Mailgun.php index 66fda1b6..f1894e09 100644 --- a/src/Utopia/Messaging/Adapter/Email/Mailgun.php +++ b/src/Utopia/Messaging/Adapter/Email/Mailgun.php @@ -55,7 +55,7 @@ protected function process(EmailMessage $message): string if (! \is_null($message->getCC())) { foreach ($message->getCC() as $cc) { - if (!empty($cc['name'])) { + if (! empty($cc['name'])) { $body['cc'] = "{$body['cc']},{$cc['name']}<{$cc['email']}>"; } else { $body['cc'] = "{$body['cc']}, <{$cc['email']}>"; @@ -65,7 +65,7 @@ protected function process(EmailMessage $message): string if (! \is_null($message->getBCC())) { foreach ($message->getBCC() as $bcc) { - if (!empty($bcc['name'])) { + if (! empty($bcc['name'])) { $body['bcc'] = "{$body['bcc']},{$bcc['name']}<{$bcc['email']}>"; } else { $body['bcc'] = "{$body['bcc']}, <{$bcc['email']}>"; diff --git a/src/Utopia/Messaging/Adapter/Email/Sendgrid.php b/src/Utopia/Messaging/Adapter/Email/Sendgrid.php index 14f3e308..00ad6ba0 100644 --- a/src/Utopia/Messaging/Adapter/Email/Sendgrid.php +++ b/src/Utopia/Messaging/Adapter/Email/Sendgrid.php @@ -49,7 +49,7 @@ protected function process(EmailMessage $message): string if (! \is_null($message->getCC())) { foreach ($message->getCC() as $cc) { - if (!empty($cc['name'])) { + if (! empty($cc['name'])) { $personalizations[0]['cc'][] = [ 'name' => $cc['name'], 'email' => $cc['email'], @@ -64,7 +64,7 @@ protected function process(EmailMessage $message): string if (! \is_null($message->getBCC())) { foreach ($message->getBCC() as $bcc) { - if (!empty($bcc['name'])) { + if (! empty($bcc['name'])) { $personalizations[0]['bcc'][] = [ 'name' => $bcc['name'], 'email' => $bcc['email'], diff --git a/src/Utopia/Messaging/Adapter/SMS/Mock.php b/src/Utopia/Messaging/Adapter/SMS/Mock.php index c8b82656..9eba18e2 100644 --- a/src/Utopia/Messaging/Adapter/SMS/Mock.php +++ b/src/Utopia/Messaging/Adapter/SMS/Mock.php @@ -38,7 +38,7 @@ protected function process(SMSMessage $message): string $response = new Response($this->getType()); $response->setDeliveredTo(\count($message->getTo())); - + $result = $this->request( method: 'POST', url: 'http://request-catcher:5000/mock-sms',