|
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\Mailer\MailTrap; |
|
19
|
|
|
use PHPMailer\PHPMailer\PHPMailer; |
|
20
|
|
|
use PHPMailer\PHPMailer\SMTP; |
|
21
|
|
|
use Exception; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* class SmtpAdapter |
|
25
|
|
|
* @package Quantum\Libraries\Mailer |
|
26
|
|
|
*/ |
|
27
|
|
|
class SmtpAdapter implements MailerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
use MailerAdapterTrait; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var PHPMailer |
|
34
|
|
|
*/ |
|
35
|
|
|
private $mailer; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Reply To addresses |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $replyToAddresses = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* CC addresses |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private $ccAddresses = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* BCC addresses |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
private $bccAddresses = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Email attachments |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
private $attachments = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Email attachments created from string |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
private $stringAttachments = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var SmtpAdapter|null |
|
69
|
|
|
*/ |
|
70
|
|
|
private static $instance = null; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* SmtpAdapter constructor |
|
74
|
|
|
* @param array $params |
|
75
|
|
|
*/ |
|
76
|
|
|
private function __construct(array $params) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->mailer = new PHPMailer(true); |
|
79
|
|
|
|
|
80
|
|
|
$this->mailer->CharSet = 'UTF-8'; |
|
81
|
|
|
|
|
82
|
|
|
$this->setupSmtp($params); |
|
83
|
|
|
|
|
84
|
|
|
if (config()->get('debug')) { |
|
85
|
|
|
$this->mailer->SMTPDebug = SMTP::DEBUG_SERVER; |
|
86
|
|
|
|
|
87
|
|
|
$this->mailer->Debugoutput = function ($message) { |
|
88
|
|
|
$this->updateDebugBar($message); |
|
89
|
|
|
}; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get Instance |
|
95
|
|
|
* @param array $params |
|
96
|
|
|
* @return SmtpAdapter|null |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function getInstance(array $params): ?SmtpAdapter |
|
99
|
|
|
{ |
|
100
|
|
|
if (self::$instance === null) { |
|
101
|
|
|
self::$instance = new self($params); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return self::$instance; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Sets "Reply-To" address |
|
109
|
|
|
* @param string $email |
|
110
|
|
|
* @param string|null $name |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setReplay(string $email, ?string $name = null): SmtpAdapter |
|
114
|
|
|
{ |
|
115
|
|
|
$this->replyToAddresses[] = [ |
|
116
|
|
|
'email' => $email, |
|
117
|
|
|
'name' => $name |
|
118
|
|
|
]; |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Gets "Reply-To" addresses |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getReplays(): array |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->replyToAddresses; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Sets "CC" address |
|
134
|
|
|
* @param string $email |
|
135
|
|
|
* @param string|null $name |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setCC(string $email, ?string $name = null): SmtpAdapter |
|
139
|
|
|
{ |
|
140
|
|
|
$this->ccAddresses[] = [ |
|
141
|
|
|
'email' => $email, |
|
142
|
|
|
'name' => $name |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Gets "CC" addresses |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getCCs(): array |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->ccAddresses; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Sets "BCC" address |
|
159
|
|
|
* @param string $email |
|
160
|
|
|
* @param string|null $name |
|
161
|
|
|
* @return $this |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setBCC(string $email, ?string $name = null): SmtpAdapter |
|
164
|
|
|
{ |
|
165
|
|
|
$this->bccAddresses[] = [ |
|
166
|
|
|
'email' => $email, |
|
167
|
|
|
'name' => $name |
|
168
|
|
|
]; |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get "BCC" addresses |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getBCCs(): array |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->bccAddresses; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Sets attachments from the path on the filesystem |
|
184
|
|
|
* @param string $attachment |
|
185
|
|
|
* @return $this |
|
186
|
|
|
*/ |
|
187
|
|
|
public function setAttachment(string $attachment): SmtpAdapter |
|
188
|
|
|
{ |
|
189
|
|
|
$this->attachments[] = $attachment;; |
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Gets the attachments |
|
195
|
|
|
* @return array |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getAttachments(): array |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->attachments; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Sets attachment from the string |
|
204
|
|
|
* @param string $content |
|
205
|
|
|
* @param string $filename |
|
206
|
|
|
* @return $this |
|
207
|
|
|
*/ |
|
208
|
|
|
public function setStringAttachment(string $content, string $filename): SmtpAdapter |
|
209
|
|
|
{ |
|
210
|
|
|
$this->stringAttachments[] = [ |
|
211
|
|
|
'content' => $content, |
|
212
|
|
|
'filename' => $filename |
|
213
|
|
|
]; |
|
214
|
|
|
|
|
215
|
|
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Gets the string attachments |
|
220
|
|
|
* @return array |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getStringAttachments(): array |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->stringAttachments; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Setups the SMTP |
|
229
|
|
|
* @param array $params |
|
230
|
|
|
*/ |
|
231
|
|
|
private function setupSmtp(array $params) |
|
232
|
|
|
{ |
|
233
|
|
|
$this->mailer->isSMTP(); |
|
234
|
|
|
$this->mailer->SMTPAuth = true; |
|
235
|
|
|
$this->mailer->Host = $params['host']; |
|
236
|
|
|
$this->mailer->SMTPSecure = $params['secure']; |
|
237
|
|
|
$this->mailer->Port = $params['port']; |
|
238
|
|
|
$this->mailer->Username = $params['username']; |
|
239
|
|
|
$this->mailer->Password = $params['password']; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Prepares the data |
|
244
|
|
|
* @throws Exception |
|
245
|
|
|
*/ |
|
246
|
|
|
private function prepare() |
|
247
|
|
|
{ |
|
248
|
|
|
$this->mailer->setFrom($this->from['email'], $this->from['name']); |
|
249
|
|
|
|
|
250
|
|
|
if ($this->subject) { |
|
251
|
|
|
$this->mailer->Subject = $this->subject; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
if ($this->message) { |
|
255
|
|
|
if ($this->templatePath) { |
|
256
|
|
|
$body = $this->createFromTemplate(); |
|
257
|
|
|
} else { |
|
258
|
|
|
$body = is_array($this->message) ? implode($this->message) : $this->message; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
$this->mailer->Body = $body; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$this->fillProperties('addAddress', $this->addresses); |
|
265
|
|
|
$this->fillProperties('addReplyTo', $this->replyToAddresses); |
|
266
|
|
|
$this->fillProperties('addCC', $this->ccAddresses); |
|
267
|
|
|
$this->fillProperties('addBCC', $this->bccAddresses); |
|
268
|
|
|
$this->fillProperties('addAttachment', $this->attachments); |
|
269
|
|
|
$this->fillProperties('addStringAttachment', $this->stringAttachments); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Fills the php mailer properties |
|
274
|
|
|
* @param string $method |
|
275
|
|
|
* @param array $fields |
|
276
|
|
|
*/ |
|
277
|
|
|
private function fillProperties(string $method, array $fields = []) |
|
278
|
|
|
{ |
|
279
|
|
|
if (!empty($fields)) { |
|
280
|
|
|
foreach ($fields as $field) { |
|
281
|
|
|
if (is_string($field)) { |
|
282
|
|
|
$this->mailer->$method($field); |
|
283
|
|
|
} else { |
|
284
|
|
|
$valOne = current($field); |
|
285
|
|
|
next($field); |
|
286
|
|
|
$valTwo = current($field); |
|
287
|
|
|
$this->mailer->$method($valOne, $valTwo); |
|
288
|
|
|
reset($field); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @return bool |
|
296
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
|
297
|
|
|
*/ |
|
298
|
|
|
private function sendEmail(): bool |
|
299
|
|
|
{ |
|
300
|
|
|
return $this->mailer->send(); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @return bool |
|
305
|
|
|
* @throws \PHPMailer\PHPMailer\Exception |
|
306
|
|
|
* @throws Exception |
|
307
|
|
|
*/ |
|
308
|
|
|
private function saveEmail(): bool |
|
309
|
|
|
{ |
|
310
|
|
|
$this->mailer->preSend(); |
|
311
|
|
|
return MailTrap::getInstance()->saveMessage($this->getMessageId(), $this->getMessageContent()); |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|