From 6e13e45a42c30442e5d01fa25713bfbd31ff986f Mon Sep 17 00:00:00 2001 From: Rafael Iop Date: Sun, 17 Mar 2019 23:37:29 -0300 Subject: [PATCH] Package data --- .gitignore | 5 + README.md | 1 + composer.json | 30 +++++ src/InfobipServiceProvider.php | 57 +++++++++ src/Transport/InfobipTransport.php | 182 +++++++++++++++++++++++++++++ 5 files changed, 275 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/InfobipServiceProvider.php create mode 100644 src/Transport/InfobipTransport.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..179443b --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +vendor/ +composer.lock +phpunit.xml +node_modules/ +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6d3f665 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +WIP \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..712d47c --- /dev/null +++ b/composer.json @@ -0,0 +1,30 @@ +{ + "name": "send4/laravel-infobip-driver", + "description": "Adds Infobip mail to Laravel mail drivers.", + "keywords": ["laravel", "infobip"], + "type": "library", + "version": "1.0.0", + "require": { + "laravel/framework": "^5.6", + "guzzlehttp/guzzle": "^6.3" + }, + "license": "MIT", + "authors": [ + { + "name": "Send4", + "homepage": "https://www.send4.com.br/" + } + ], + "autoload": { + "psr-4": { + "Send4\\InfobipMail": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Send4\\InfobipMail\\InfobipServiceProvider" + ] + } + } +} diff --git a/src/InfobipServiceProvider.php b/src/InfobipServiceProvider.php new file mode 100644 index 0000000..c32a424 --- /dev/null +++ b/src/InfobipServiceProvider.php @@ -0,0 +1,57 @@ +app->afterResolving(TransportManager::class, function(TransportManager $manager) { + $this->createInfobipDriver($manager); + }); + } + + /** + * Create an instance of the Infobip Swift Transport driver. + * + * @param \Illuminate\Mail\TransportManager $manager + * @return \Send4\InfobipMail\Transport\InfobipTransport + */ + protected function createInfobipDriver(TransportManager $manager) + { + $manager->extend('infobip', function() { + $config = $this->app['config']->get('services.infobip', []); + + return new InfobipTransport( + $this->guzzle($config), + $config['api_key'], + $config['base_url'] + ); + }); + } + + /** + * Get a fresh Guzzle HTTP client instance. + * + * @param array $config + * @return \GuzzleHttp\Client + */ + protected function guzzle($config) + { + return new HttpClient(Arr::add( + $config['guzzle'] ?? [], 'connect_timeout', 60 + )); + } + +} \ No newline at end of file diff --git a/src/Transport/InfobipTransport.php b/src/Transport/InfobipTransport.php new file mode 100644 index 0000000..d7606d9 --- /dev/null +++ b/src/Transport/InfobipTransport.php @@ -0,0 +1,182 @@ +client = $client; + $this->key = $key; + $this->baseUrl = $baseUrl; + } + + /** + * {@inheritdoc} + */ + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) + { + $this->beforeSendPerformed($message); + + $response = $this->client->request( + 'POST', + "{$this->baseUrl}/email/1/send", + $this->payload($message) + ); + + $this->sendPerformed($message); + + return $this->numberOfRecipients($message); + } + + /** + * Get the HTTP payload for sending the Infobip message. + * + * @param \Swift_Mime_SimpleMessage $message + * @return array + */ + protected function payload(Swift_Mime_SimpleMessage $message) + { + $payload = [ + 'headers' => [ + 'Authorization' => 'App ' . $this->key, + 'Accept' => 'application/json' + ], + 'multipart' => [ + ['name' => 'from', 'contents' => $this->getFrom($message)], + ['name' => 'subject', 'contents' => $message->getSubject()], + ['name' => 'html', 'contents' => $message->getBody()], + ], + ]; + + // Add "to" attribute to multipart payload + foreach ($this->getTo($message) as $to) { + $payload['multipart'][] = ['name' => 'to', 'contents' => $to]; + } + + // Add "replyTo" attribute to multipart payload + if ($replyTo = $this->getReplyTo($message)) { + $payload['multipart'][] = ['name' => 'replyTo', 'contents' => $replyTo]; + } + + // Add "attachments" attribute to multipart payload + foreach ($this->getAttachments($message) as $attachment) { + $payload['multipart'][] = [ + 'name' => 'attachment', + 'contents' => $attachment['contents'], + 'filename' => $attachment['filename'], + ]; + } + + return $payload; + } + + /** + * Get all the addresses this message should be sent to. + * + * @param \Swift_Mime_SimpleMessage $message + * @return array + */ + protected function getTo(Swift_Mime_SimpleMessage $message) + { + $to = array_merge( + (array) $message->getTo(), + (array) $message->getCc(), + (array) $message->getBcc() + ); + + return array_keys($to); + } + + /** + * Get the "from" payload field for the API request. + * + * @param \Swift_Mime_SimpleMessage $message + * @return string + */ + protected function getFrom(Swift_Mime_SimpleMessage $message) + { + $from = $message->getSender() ?: $message->getFrom(); + + $address = array_keys($from)[0]; + $display = array_values($from)[0]; + + return $display ? $display." <{$address}>" : $address; + } + + /** + * Get the "replyTo" payload field for the API request. + * + * @param \Swift_Mime_SimpleMessage $message + * @return string + */ + protected function getReplyTo(Swift_Mime_SimpleMessage $message) + { + $replyTo = $message->getReplyTo(); + + return $replyTo ? array_keys($replyTo)[0] : null; + } + + /** + * @param Swift_Mime_SimpleMessage $message + * @return array + */ + private function getAttachments(Swift_Mime_SimpleMessage $message) + { + $attachments = []; + + foreach ($message->getChildren() as $attachment) { + if (!$attachment instanceof Swift_Attachment && !$attachment instanceof Swift_Image) { + continue; + } + + $attachments[] = [ + 'contents' => $attachment->getBody(), + 'filename' => $attachment->getFilename(), + 'type' => $attachment->getContentType(), + ]; + } + + return $attachments; + } + +}