Skip to content

Commit c92ca6f

Browse files
committed
initial commit
0 parents  commit c92ca6f

File tree

5 files changed

+237
-0
lines changed

5 files changed

+237
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.DS_store
2+
.idea/*

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
### Install
2+
3+
Require this package with composer using the following command:
4+
```bash
5+
composer require clarification/sparkpost-laravel-driver
6+
```
7+
8+
After updating composer, add the service provider to the `providers` array in `config/app.php`
9+
```php
10+
Clarification\MailDrivers\Sparkpost\SparkpostServiceProvider::class,
11+
```
12+
13+
You will also need to add the sparkpost API Key settings to the array in `config/services.php` and set up the environment key
14+
```php
15+
'sparkpost' => [
16+
'secret' => env('SPARKPOST_SECRET'),
17+
],
18+
```
19+
```bash
20+
SPARKPOST_SECRET=__Your_key_here__
21+
```
22+
23+
Finally you need to set your mail driver to `sparkpost`. You can do this by changing the driver in `config/mail.php`
24+
```php
25+
'driver' => env('MAIL_DRIVER', 'sparkpost'),
26+
```
27+
28+
Or by setting the environment variable `MAIL_DRIVER` in your .env file
29+
```bash
30+
MAIL_DRIVER=sparkpost
31+
```
32+
33+
If you need to pass any options to the guzzle client instance which is making the request to the Send Grid API, you can do so by setting the 'guzzle' options in `config/services.php`
34+
```php
35+
'sparkpost' => [
36+
'secret' => env('SPARKPOST_SECRET'),
37+
'guzzle' => [
38+
'verify' => true,
39+
'decode_content' => true,
40+
]
41+
],
42+
```
43+
44+
**This is only needed if you are using Laravel 5.1.* or older, the sparkpost driver is included in 5.2**
45+
If the Laravel Sparkpost driver is present (you are running >=5.2), The service provider will not load this packages Sparkpost driver.

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "clarification/sparkpost-laravel-driver",
3+
"type": "library",
4+
"description" : "Sparkpost mail driver for Laravel",
5+
"keywords": ["laravel", "mail", "driver", "sparkpost"],
6+
"license": "MIT",
7+
"homepage" : "http://clarification.io",
8+
"authors": [
9+
{
10+
"name" : "Robin Lidbetter",
11+
"role" : "Developer"
12+
}
13+
],
14+
"require": {
15+
"illuminate/container": ">=4.2",
16+
"swiftmailer/swiftmailer": "~5.1"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Clarification\\MailDrivers\\Sparkpost\\": "src"
21+
}
22+
}
23+
}

src/SparkpostServiceProvider.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Clarification\MailDrivers\Sparkpost;
4+
5+
use GuzzleHttp\Client;
6+
use Illuminate\Support\Arr;
7+
use Illuminate\Mail\TransportManager;
8+
use Illuminate\Support\ServiceProvider;
9+
use Clarification\MailDrivers\Sparkpost\Transport\SparkpostTransport;
10+
use Illuminate\Mail\Transport\SparkPostTransport as LaravelSparkPostTransport;
11+
12+
class SparkpostServiceProvider extends ServiceProvider
13+
{
14+
/**
15+
* After register is called on all service providers, then boot is called
16+
*/
17+
public function boot()
18+
{
19+
//
20+
}
21+
22+
/**
23+
* Register is called on all service providers first.
24+
*
25+
* We must register the extension before anything tries to use the mailing functionality.
26+
* None of the closures are executed until someone tries to send an email.
27+
*
28+
* This will register a closure which will be run when 'swift.transport' (the transport manager) is first resolved.
29+
* Then we extend the transport manager, by adding the spark post transport object as the 'sparkpost' driver.
30+
*/
31+
public function register()
32+
{
33+
// Don't need to register our driver if the current laravel install already has the spark post transport
34+
if(class_exists(LaravelSparkPostTransport::class, false)) {
35+
return;
36+
}
37+
38+
$this->app->extend('swift.transport', function(TransportManager $manager) {
39+
$manager->extend('sparkpost', function() {
40+
$config = $this->app['config']->get('services.sparkpost', []);
41+
$client = new Client(Arr::get($config, 'guzzle', []));
42+
return new SparkpostTransport($client, $config['secret']);
43+
});
44+
return $manager;
45+
});
46+
}
47+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Clarification\MailDrivers\Sparkpost\Transport;
4+
5+
use Swift_Mime_Message;
6+
use GuzzleHttp\ClientInterface;
7+
use Illuminate\Mail\Transport\Transport;
8+
9+
class SparkpostTransport extends Transport
10+
{
11+
/**
12+
* Guzzle client instance.
13+
*
14+
* @var \GuzzleHttp\ClientInterface
15+
*/
16+
protected $client;
17+
18+
/**
19+
* The SparkPost API key.
20+
*
21+
* @var string
22+
*/
23+
protected $key;
24+
25+
/**
26+
* Create a new SparkPost transport instance.
27+
*
28+
* @param \GuzzleHttp\ClientInterface $client
29+
* @param string $key
30+
*/
31+
public function __construct(ClientInterface $client, $key)
32+
{
33+
$this->client = $client;
34+
$this->key = $key;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
41+
{
42+
$this->beforeSendPerformed($message);
43+
$recipients = $this->getRecipients($message);
44+
$message->setBcc([]);
45+
$options = [
46+
'headers' => [
47+
'Authorization' => $this->key,
48+
],
49+
'json' => [
50+
'recipients' => $recipients,
51+
'content' => [
52+
'html' => $message->getBody(),
53+
'from' => $this->getFrom($message),
54+
'subject' => $message->getSubject(),
55+
],
56+
],
57+
];
58+
return $this->client->post('https://api.sparkpost.com/api/v1/transmissions', $options);
59+
}
60+
61+
/**
62+
* Get all the addresses this message should be sent to.
63+
*
64+
* Note that SparkPost still respects CC, BCC headers in raw message itself.
65+
*
66+
* @param \Swift_Mime_Message $message
67+
* @return array
68+
*/
69+
protected function getRecipients(Swift_Mime_Message $message)
70+
{
71+
$to = $bcc = [];
72+
if ($message->getTo()) {
73+
$to = array_merge($to, array_keys($message->getTo()));
74+
}
75+
if ($message->getCc()) {
76+
$to = array_merge($to, array_keys($message->getCc()));
77+
}
78+
if ($message->getBcc()) {
79+
$to = array_merge($bcc, array_keys($message->getBcc()));
80+
}
81+
$recipients = array_map(function ($address) {
82+
return ['address' => ['email' => $address, 'header_to' => $address]];
83+
}, $to);
84+
return $recipients;
85+
}
86+
87+
/**
88+
* Get the "from" contacts in the format required by SparkPost.
89+
*
90+
* @param Swift_Mime_Message $message
91+
* @return array
92+
*/
93+
protected function getFrom(Swift_Mime_Message $message)
94+
{
95+
return array_map(function ($email, $name) {
96+
return compact('name', 'email');
97+
}, array_keys($message->getFrom()), $message->getFrom())[0];
98+
}
99+
100+
/**
101+
* Get the API key being used by the transport.
102+
*
103+
* @return string
104+
*/
105+
public function getKey()
106+
{
107+
return $this->key;
108+
}
109+
110+
/**
111+
* Set the API key being used by the transport.
112+
*
113+
* @param string $key
114+
* @return string
115+
*/
116+
public function setKey($key)
117+
{
118+
return $this->key = $key;
119+
}
120+
}

0 commit comments

Comments
 (0)