diff --git a/app/Lib/LDC/MailjetSms.php b/app/Lib/LDC/MailjetSms.php new file mode 100644 index 0000000..114159d --- /dev/null +++ b/app/Lib/LDC/MailjetSms.php @@ -0,0 +1,70 @@ +app = $app; + $this->loadDefaultOptions(); + } + + protected function loadDefaultOptions(): MailjetSms + { + $this->config = $this->app['config']->get('services.mailjet'); + return $this; + } + + protected function buildHttpClient(): Client + { + return new Client([ + 'allow_redirects' => false, + 'connect_timeout' => 3, + 'verify' => false, + 'headers' => [ + 'User-Agent' => 'LDCMailjetSms/1.0', + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'Authorization' => sprintf('Bearer %s', $this->config['sms_token']), + ], + 'timeout' => 5, + ]); + } + + public function send(string $message, string $to, $from = null): ResponseInterface + { + $from = $from ?? $this->config['sms_from']; + $client = $this->buildHttpClient(); + + try { + $response = $client->post($this->config['sms_endpoint'], [ + 'json' => [ + 'Text' => $message, + 'From' => $from, + 'To' => $to, + ] + ]); + } catch (\GuzzleHttp\Exception\RequestException $ex) { + Log::debug('error ' . $ex->getMessage()); + if (!is_null($ex->getResponse())){ + Log::debug((string) $ex->getResponse()->getBody()); + } + throw $ex; + } + + + + return $response; + } +}