1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.5 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Mailer\Adapters; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Mailer\Contracts\MailerInterface; |
18
|
|
|
use Quantum\Libraries\Mailer\Traits\MailerTrait; |
19
|
|
|
use Quantum\Libraries\HttpClient\HttpClient; |
20
|
|
|
use Exception; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* class MandrillAdapter |
24
|
|
|
* @package Quantum\Libraries\Mailer |
25
|
|
|
*/ |
26
|
|
|
class MandrillAdapter implements MailerInterface |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
use MailerTrait; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $name = 'Mandrill'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var HttpClient |
38
|
|
|
*/ |
39
|
|
|
private $httpClient; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $apiKey; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $apiUrl = 'https://mandrillapp.com/api/1.0/messages/send.json'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
private $data = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var MandrillAdapter|null |
58
|
|
|
*/ |
59
|
|
|
private static $instance = null; |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* MandrillAdapter constructor |
63
|
|
|
* @param array $params |
64
|
|
|
*/ |
65
|
|
|
public function __construct(array $params) |
66
|
|
|
{ |
67
|
|
|
$this->httpClient = new HttpClient(); |
68
|
|
|
|
69
|
|
|
$this->data['key'] = $params['api_key']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Prepares the data |
74
|
|
|
*/ |
75
|
|
|
private function prepare() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$message = []; |
78
|
|
|
|
79
|
|
|
$message['from_email'] = $this->from['email']; |
80
|
|
|
|
81
|
|
|
if ($this->from['name']) { |
82
|
|
|
$message['from_name'] = $this->from['name']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$message['to'] = $this->addresses; |
86
|
|
|
|
87
|
|
|
if ($this->subject) { |
88
|
|
|
$message['subject'] = $this->subject; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->message) { |
92
|
|
|
if ($this->templatePath) { |
93
|
|
|
$body = $this->createFromTemplate(); |
94
|
|
|
} else { |
95
|
|
|
$body = is_array($this->message) ? implode($this->message) : $this->message; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$message['html'] = trim(str_replace("\n", "", $body)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->data['message'] = $message; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
private function sendEmail(): bool |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
$this->httpClient |
111
|
|
|
->createRequest($this->apiUrl) |
112
|
|
|
->setMethod('POST') |
113
|
|
|
->setData(json_encode($this->data)) |
114
|
|
|
->start(); |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} catch (Exception $e) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |