From 5bb0dc3ea98244190c0737ad15472d48de8df9a4 Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Mon, 2 Oct 2023 07:21:23 +0530 Subject: [PATCH] Create SocketlabsAdapter.php --- .../Adapters/Socketlabs/SocketlabsAdapter.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/Utopia/Messaging/Adapters/Socketlabs/SocketlabsAdapter.php diff --git a/src/Utopia/Messaging/Adapters/Socketlabs/SocketlabsAdapter.php b/src/Utopia/Messaging/Adapters/Socketlabs/SocketlabsAdapter.php new file mode 100644 index 00000000..17e5a3a9 --- /dev/null +++ b/src/Utopia/Messaging/Adapters/Socketlabs/SocketlabsAdapter.php @@ -0,0 +1,53 @@ +// SocketlabsAdapter.php + +class SocketlabsAdapter implements MessagingAdapterInterface +{ + private $apiKey; + private $apiEndpoint; + + public function setConfig($config) + { + // Set the configuration values. + $this->apiKey = $config['apiKey']; + $this->apiEndpoint = $config['apiEndpoint']; + } + + public function sendMessage($message) + { + // Construct the email payload + $emailData = [ + 'subject' => $message->getSubject(), + 'htmlBody' => $message->getHtmlBody(), + 'textBody' => $message->getTextBody(), + 'from' => $message->getFrom(), + 'to' => $message->getTo(), + ]; + + // Convert data to JSON + $jsonPayload = json_encode($emailData); + + // Prepare the HTTP request + $ch = curl_init($this->apiEndpoint); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'Authorization: Bearer ' . $this->apiKey, + ]); + + // Execute the request + $response = curl_exec($ch); + $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + // Handle the response and return results + if ($statusCode === 200) { + // Successfully sent + return true; + } else { + // Failed to send, handle errors + return false; + } + } +}