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.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Mailer\Adapters; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Mailer\MailerInterface; |
18
|
|
|
use Quantum\Libraries\Curl\HttpClient; |
19
|
|
|
use Quantum\Libraries\Mailer\MailTrap; |
20
|
|
|
use Exception; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* class MandrillAdapter |
24
|
|
|
* @package Quantum\Libraries\Mailer |
25
|
|
|
*/ |
26
|
|
|
class MandrillAdapter implements MailerInterface |
27
|
|
|
{ |
28
|
|
|
use MailerAdapterTrait; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $name = 'Mandrill'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var HttpClient |
37
|
|
|
*/ |
38
|
|
|
private $httpClient; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $apiKey; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $apiUrl = 'https://mandrillapp.com/api/1.0/messages/send.json'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $data = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var MandrillAdapter|null |
57
|
|
|
*/ |
58
|
|
|
private static $instance = null; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* MandrillAdapter constructor |
62
|
|
|
* @param array $params |
63
|
|
|
*/ |
64
|
|
|
private function __construct(array $params) |
65
|
|
|
{ |
66
|
|
|
$this->httpClient = new HttpClient(); |
67
|
|
|
|
68
|
|
|
$this->data['key'] = $params['api_key']; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get Instance |
73
|
|
|
* @param array $params |
74
|
|
|
* @return MandrillAdapter |
75
|
|
|
*/ |
76
|
|
|
public static function getInstance(array $params): MandrillAdapter |
77
|
|
|
{ |
78
|
|
|
if (self::$instance === null) { |
79
|
|
|
self::$instance = new self($params); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return self::$instance; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Prepares the data |
87
|
|
|
*/ |
88
|
|
|
private function prepare() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$message = []; |
91
|
|
|
|
92
|
|
|
$message['from_email'] = $this->from['email']; |
93
|
|
|
|
94
|
|
|
if ($this->from['name']) { |
95
|
|
|
$message['from_name'] = $this->from['name']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$message['to'] = $this->addresses; |
99
|
|
|
|
100
|
|
|
if ($this->subject) { |
101
|
|
|
$message['subject'] = $this->subject; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($this->message) { |
105
|
|
|
if ($this->templatePath) { |
106
|
|
|
$body = $this->createFromTemplate(); |
107
|
|
|
} else { |
108
|
|
|
$body = is_array($this->message) ? implode($this->message) : $this->message; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$message['html'] = trim(str_replace("\n", "", $body)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->data['message'] = $message; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
private function sendEmail(): bool |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
$this->httpClient |
124
|
|
|
->createRequest($this->apiUrl) |
125
|
|
|
->setMethod('POST') |
126
|
|
|
->setData(json_encode($this->data)) |
127
|
|
|
->start(); |
128
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} catch (Exception $e) { |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return bool |
137
|
|
|
* @throws Exception |
138
|
|
|
*/ |
139
|
|
|
private function saveEmail(): bool |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
return MailTrap::getInstance()->saveMessage($this->getMessageId(), $this->getMessageContent()); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|