|
1
|
|
|
<?php namespace nyx\notify\transports\mail\drivers; |
|
2
|
|
|
|
|
3
|
|
|
// Internal dependencies |
|
4
|
|
|
use nyx\notify\transports\mail; |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* SparkPost Mail Driver |
|
8
|
|
|
* |
|
9
|
|
|
* @package Nyx\Notify |
|
10
|
|
|
* @version 0.1.0 |
|
11
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
12
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
13
|
|
|
* @link https://github.com/unyx/nyx |
|
14
|
|
|
*/ |
|
15
|
|
|
class SparkPost implements mail\interfaces\Driver |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The traits of a SparkPost Mail Driver instance. |
|
19
|
|
|
*/ |
|
20
|
|
|
use traits\ExposesBcc; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string The API key used to authorize requests to SparkPost's API. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $key; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array An array of additional options that shall be passed along with each API request. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $options; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \GuzzleHttp\ClientInterface The underlying HTTP Client instance. |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $client; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Creates a new SparkPost Mail Driver instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \GuzzleHttp\ClientInterface $client The HTTP Client to use. |
|
41
|
|
|
* @param string $key The API key to be used to authorize requests to SparkPost's API. |
|
42
|
|
|
* @param array $options An array of additional options that shall be passed along with |
|
43
|
|
|
* each API request. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(\GuzzleHttp\ClientInterface $client, string $key, array $options = []) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->key = $key; |
|
48
|
|
|
$this->options = $options; |
|
49
|
|
|
$this->client = $client; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritDoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function send(\Swift_Mime_Message $message, &$failedRecipients = null) |
|
56
|
|
|
{ |
|
57
|
|
|
$recipients = $this->getRecipients($message); |
|
58
|
|
|
|
|
59
|
|
|
// Unset the BCC recipients temporarily since we don't want to pass them along with the MIME. |
|
60
|
|
|
$this->hideBcc($message); |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
$this->client->request('POST', 'https://api.sparkpost.com/api/v1/transmissions', [ |
|
64
|
|
|
'headers' => [ |
|
65
|
|
|
'Authorization' => $this->key, |
|
66
|
|
|
], |
|
67
|
|
|
'json' => [ |
|
68
|
|
|
'recipients' => $recipients, |
|
69
|
|
|
'options' => $this->options ?: ['start_time' => 'now'], |
|
70
|
|
|
'content' => [ |
|
71
|
|
|
'email_rfc822' => $message->toString(), |
|
72
|
|
|
] |
|
73
|
|
|
] |
|
74
|
|
|
]); |
|
75
|
|
|
} finally { |
|
76
|
|
|
// Always restore the BCC recipients. |
|
77
|
|
|
$this->restoreBcc($message); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return count($recipients); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns an array of all of the recipients of the message (to, cc and bcc) in a format understood by |
|
85
|
|
|
* SparkPost's "transmissions" endpoint for the "recipients" field. |
|
86
|
|
|
* |
|
87
|
|
|
* @param \Swift_Mime_Message $message |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getRecipients(\Swift_Mime_Message $message) : array |
|
91
|
|
|
{ |
|
92
|
|
|
$recipients = (array) $message->getTo() + (array) $message->getCc() + (array) $message->getBcc(); |
|
93
|
|
|
$result = []; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($recipients as $address => $name) { |
|
96
|
|
|
$result[] = [ |
|
97
|
|
|
'address' => $name |
|
98
|
|
|
? ['email' => $address, 'name' => $name] |
|
99
|
|
|
: $address |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $result; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: