diff --git a/src/Utopia/Messaging/Adapter.php b/src/Utopia/Messaging/Adapter.php index 38ae9496..60a909ef 100644 --- a/src/Utopia/Messaging/Adapter.php +++ b/src/Utopia/Messaging/Adapter.php @@ -55,26 +55,25 @@ protected function request( string $method, string $url, array $headers = [], - mixed $body = null, + ?string $body = null, ): string { - $headers[] = 'Content-length: '.\strlen($body); - $ch = \curl_init(); + if (! \is_null($body)) { + $headers[] = 'Content-Length: '.\strlen($body); + \curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + } + \curl_setopt($ch, CURLOPT_URL, $url); \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); \curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); \curl_setopt($ch, CURLOPT_USERAGENT, "Appwrite {$this->getName()} Message Sender"); - if (! is_null($body)) { - \curl_setopt($ch, CURLOPT_POSTFIELDS, $body); - } - $response = \curl_exec($ch); if (\curl_errno($ch)) { - throw new \Exception('Error:'.\curl_error($ch)); + throw new \Exception('Error: '.\curl_error($ch)); } if (\curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) { throw new \Exception($response); diff --git a/src/Utopia/Messaging/Adapters/Email/Mailgun.php b/src/Utopia/Messaging/Adapters/Email/Mailgun.php index 872d6b09..ba64c906 100644 --- a/src/Utopia/Messaging/Adapters/Email/Mailgun.php +++ b/src/Utopia/Messaging/Adapters/Email/Mailgun.php @@ -40,13 +40,13 @@ protected function process(Email $message): string headers: [ 'Authorization: Basic '.base64_encode('api:'.$this->apiKey), ], - body: [ + body: \http_build_query([ 'from' => $message->getFrom(), 'to' => \implode(',', $message->getTo()), 'subject' => $message->getSubject(), 'text' => $message->isHtml() ? null : $message->getContent(), 'html' => $message->isHtml() ? $message->getContent() : null, - ], + ]), ); } } diff --git a/src/Utopia/Messaging/Adapters/SMS/Telesign.php b/src/Utopia/Messaging/Adapters/SMS/Telesign.php index 96b60042..d19d5876 100644 --- a/src/Utopia/Messaging/Adapters/SMS/Telesign.php +++ b/src/Utopia/Messaging/Adapters/SMS/Telesign.php @@ -48,10 +48,10 @@ protected function process(SMS $message): string headers: [ 'Authorization: Basic '.base64_encode("{$this->username}:{$this->password}"), ], - body: [ + body: \http_build_query([ 'template' => $message->getContent(), 'recipients' => \implode(',', $to), - ], + ]), ); } } diff --git a/src/Utopia/Messaging/Adapters/SMS/TextMagic.php b/src/Utopia/Messaging/Adapters/SMS/TextMagic.php index e941f3a5..68fb4e37 100644 --- a/src/Utopia/Messaging/Adapters/SMS/TextMagic.php +++ b/src/Utopia/Messaging/Adapters/SMS/TextMagic.php @@ -49,11 +49,11 @@ protected function process(SMS $message): string "X-TM-Username: {$this->username}", "X-TM-Key: {$this->apiKey}", ], - body: [ + body: \http_build_query([ 'text' => $message->getContent(), 'from' => \ltrim($message->getFrom(), '+'), 'phones' => \implode(',', $to), - ], + ]), ); } } diff --git a/src/Utopia/Messaging/Adapters/SMS/Vonage.php b/src/Utopia/Messaging/Adapters/SMS/Vonage.php index 2c945374..2643a6b0 100644 --- a/src/Utopia/Messaging/Adapters/SMS/Vonage.php +++ b/src/Utopia/Messaging/Adapters/SMS/Vonage.php @@ -45,13 +45,13 @@ protected function process(SMS $message): string return $this->request( method: 'POST', url: 'https://rest.nexmo.com/sms/json', - body: [ + body: \http_build_query([ 'text' => $message->getContent(), 'from' => $message->getFrom(), 'to' => \implode(',', $to), 'api_key' => $this->apiKey, 'api_secret' => $this->apiSecret, - ] + ]), ); } }