Skip to content

Commit 85ba3c5

Browse files
authored
Merge pull request #12 from fhpimenta/support-laravel-6.0
support for Laravel 6.0
2 parents 99292e8 + 61c2545 commit 85ba3c5

File tree

3 files changed

+105
-21
lines changed

3 files changed

+105
-21
lines changed

composer.json

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
{
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"
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+
"guzzlehttp/guzzle": ">=5.0",
16+
"illuminate/container": ">=5.0",
17+
"swiftmailer/swiftmailer": "~5.1||~6.0"
18+
},
19+
"extra": {
20+
"laravel": {
21+
"providers": [
22+
"Clarification\\MailDrivers\\Sparkpost\\SparkpostServiceProvider"
23+
]
24+
}
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Clarification\\MailDrivers\\Sparkpost\\": "src"
29+
}
1230
}
13-
],
14-
"require": {
15-
"guzzlehttp/guzzle": ">=5.0",
16-
"illuminate/container": ">=5.0",
17-
"swiftmailer/swiftmailer": "~5.1||~6.0"
18-
},
19-
"autoload": {
20-
"psr-4": {
21-
"Clarification\\MailDrivers\\Sparkpost\\": "src"
22-
}
23-
}
2431
}

src/SparkpostServiceProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace Clarification\MailDrivers\Sparkpost;
44

55
use GuzzleHttp\Client;
6+
use Swift_Mime_Message;
67
use Illuminate\Mail\TransportManager;
78
use Illuminate\Support\ServiceProvider;
89
use Illuminate\Mail\Transport\Transport as AbstractTransport;
910
use Clarification\MailDrivers\Sparkpost\Transport\SparkPostTransport;
1011
use Clarification\MailDrivers\Sparkpost\Transport\SparkPostTransportFiveZero;
12+
use Clarification\MailDrivers\Sparkpost\Transport\SparkPostTransportFiveFive;
1113
use Illuminate\Mail\Transport\SparkPostTransport as LaravelSparkPostTransport;
1214

1315
class SparkpostServiceProvider extends ServiceProvider
@@ -46,6 +48,13 @@ public function register()
4648
$client = new Client($guzzleOptions);
4749

4850
if(class_exists(AbstractTransport::class)) {
51+
// version 6 of swiftmailer removed this class in favor of Swift_Mime_SimpleMessage
52+
// https://github.com/swiftmailer/swiftmailer/blob/5a90a13527e955f2d6e8fcc8876d087a89d37da3/CHANGES#L64-L78
53+
// larvel version 5.5 requires version 6 of swiftmailer and has updated method signatures
54+
if(!interface_exists(Swift_Mime_Message::class)) {
55+
return new SparkPostTransportFiveFive($client, $config['secret'], $sparkpostOptions);
56+
}
57+
4958
return new SparkPostTransport($client, $config['secret'], $sparkpostOptions);
5059
}
5160

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Clarification\MailDrivers\Sparkpost\Transport;
4+
5+
use Illuminate\Mail\Transport\Transport;
6+
use Swift_Mime_SimpleMessage;
7+
8+
class SparkPostTransportFiveFive extends Transport
9+
{
10+
use SparkPostTransportTrait;
11+
12+
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
13+
{
14+
$this->beforeSendPerformed($message);
15+
16+
$recipients = $this->getRecipients($message);
17+
18+
$message->setBcc([]);
19+
20+
$options = [
21+
'headers' => [
22+
'Authorization' => $this->key,
23+
],
24+
'json' => [
25+
'recipients' => $recipients,
26+
'content' => [
27+
'email_rfc822' => $message->toString(),
28+
],
29+
],
30+
];
31+
32+
if ($this->options) {
33+
$options['json']['options'] = $this->options;
34+
}
35+
36+
return $this->client->post('https://api.sparkpost.com/api/v1/transmissions', $options);
37+
}
38+
39+
/**
40+
* Get all the addresses this message should be sent to.
41+
*
42+
* Note that SparkPost still respects CC, BCC headers in raw message itself.
43+
*
44+
* @param Swift_Mime_SimpleMessage $message
45+
* @return array
46+
*/
47+
protected function getRecipients(Swift_Mime_SimpleMessage $message)
48+
{
49+
$to = [];
50+
if ($getTo = $message->getTo()) {
51+
$to = array_merge($to, array_keys($getTo));
52+
}
53+
54+
if ($getCc = $message->getCc()) {
55+
$to = array_merge($to, array_keys($getCc));
56+
}
57+
58+
if ($getBcc = $message->getBcc()) {
59+
$to = array_merge($to, array_keys($getBcc));
60+
}
61+
62+
$recipients = array_map(function ($address) {
63+
return compact('address');
64+
}, $to);
65+
66+
return $recipients;
67+
}
68+
}

0 commit comments

Comments
 (0)