-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rafael Iop
committed
Mar 18, 2019
1 parent
4124711
commit 6e13e45
Showing
5 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml | ||
node_modules/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
WIP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Send4\InfobipMail; | ||
|
||
use GuzzleHttp\Client as HttpClient; | ||
use Illuminate\Mail\TransportManager; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\ServiceProvider; | ||
use Send4\InfobipMail\Transport\InfobipTransport; | ||
|
||
class InfobipServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register the transport manager instance. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->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 | ||
)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
|
||
namespace Send4\InfobipMail\Transport; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use Illuminate\Mail\Transport\Transport; | ||
use Swift_Mime_SimpleMessage; | ||
use Swift_Attachment; | ||
use Swift_Image; | ||
|
||
class InfobipTransport extends Transport | ||
{ | ||
/** | ||
* Guzzle client instance. | ||
* | ||
* @var \GuzzleHttp\ClientInterface | ||
*/ | ||
protected $client; | ||
|
||
/** | ||
* The Infobip API key. | ||
* | ||
* https://dev.infobip.com/getting-started/security-and-authorization | ||
* | ||
* @var string | ||
*/ | ||
protected $key; | ||
|
||
/** | ||
* The Infobip base url. | ||
* | ||
* https://dev.infobip.com/getting-started/base-url | ||
* | ||
* @var string | ||
*/ | ||
protected $baseUrl; | ||
|
||
/** | ||
* Create a new Infobip transport instance. | ||
* | ||
* @param \GuzzleHttp\ClientInterface $client | ||
* @param string $key | ||
* @param string $baseUrl | ||
* @return void | ||
*/ | ||
public function __construct(ClientInterface $client, $key, $baseUrl) | ||
{ | ||
$this->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; | ||
} | ||
|
||
} |