Skip to content

Commit

Permalink
Package data
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Iop committed Mar 18, 2019
1 parent 4124711 commit 6e13e45
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vendor/
composer.lock
phpunit.xml
node_modules/
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WIP
30 changes: 30 additions & 0 deletions composer.json
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"
]
}
}
}
57 changes: 57 additions & 0 deletions src/InfobipServiceProvider.php
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
));
}

}
182 changes: 182 additions & 0 deletions src/Transport/InfobipTransport.php
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;
}

}

0 comments on commit 6e13e45

Please sign in to comment.