|
1
|
|
|
<?php namespace nyx\notify\transports\mail\drivers; |
|
2
|
|
|
|
|
3
|
|
|
// Internal dependencies |
|
4
|
|
|
use nyx\notify\transports\mail; |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Mandrill 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 Mandrill implements mail\interfaces\Driver |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The traits of a Mandrill Mail Driver instance. |
|
19
|
|
|
*/ |
|
20
|
|
|
use traits\ExposesBcc; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string The API key used to authorize requests to Mandrill's API. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $key; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \GuzzleHttp\ClientInterface The underlying HTTP Client instance. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $client; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Creates a new Mandrill Mail Driver instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \GuzzleHttp\ClientInterface $client The HTTP Client to use. |
|
36
|
|
|
* @param string $key The API key to be used to authorize requests to Mandrill's API. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(\GuzzleHttp\ClientInterface $client, string $key) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->key = $key; |
|
41
|
|
|
$this->client = $client; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function send(\Swift_Mime_Message $message, &$failedRecipients = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$recipients = $this->getRecipients($message); |
|
50
|
|
|
|
|
51
|
|
|
// Unset the BCC recipients temporarily since we don't want to pass them along with the MIME. |
|
52
|
|
|
$this->hideBcc($message); |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
|
|
$this->client->request('POST', 'https://mandrillapp.com/api/1.0/messages/send-raw.json', [ |
|
56
|
|
|
'json' => [ |
|
57
|
|
|
'async' => true, |
|
58
|
|
|
'key' => $this->key, |
|
59
|
|
|
'to' => $recipients, |
|
60
|
|
|
'raw_message' => $message->toString() |
|
61
|
|
|
] |
|
62
|
|
|
]); |
|
63
|
|
|
} finally { |
|
64
|
|
|
// Always restore the BCC recipients. |
|
65
|
|
|
$this->restoreBcc($message); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return count($recipients); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns an array of all of the recipients of the message (to, cc and bcc) in a format understood by |
|
73
|
|
|
* Mandrill's "send-raw" endpoint for the "to" field. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \Swift_Mime_Message $message |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function getRecipients(\Swift_Mime_Message $message) : array |
|
79
|
|
|
{ |
|
80
|
|
|
// Note: We are using the in-memory copy of BCC recipients here since send() temporarily removed |
|
81
|
|
|
// them from the MIME message to hide them. |
|
82
|
|
|
return array_keys((array) $message->getTo() + (array) $message->getCc() + (array) (array) $this->bcc); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
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: