-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMail.php
445 lines (372 loc) · 11.1 KB
/
Mail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
namespace Tamedevelopers\Support;
use PHPMailer\PHPMailer\SMTP;
use Tamedevelopers\Support\Str;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use Tamedevelopers\Support\Traits\MailTrait;
class Mail{
use MailTrait;
/**
* Constructor method to initialize the PHPMailer object
* @param string|array|null $emails
*
* @return void
*/
public function __construct($emails = null, ?array $options = [])
{
$this->mailer = new PHPMailer(true);
if(!empty($emails)){
$this->recipients['to'] = $this->convert($emails, 'email');
}
if(!empty($options)){
$this->options = $options;
}
// clone copy of self
if(!self::isMailInstance()){
self::$staticData = clone $this;
}
}
/**
* Handle the calls to non-existent instance methods.
* @param string $name
* @param mixed $args
*
* @return mixed
*/
public function __call($name, $args)
{
return self::nonExistMethod($name, $args, $this);
}
/**
* Set manual configuration
*
* @param array $options Mailer configuration options
* - host | port | username | password | encryption
* - from_email | from_name |
* - base\Base directory
*
* @return $this
*/
static public function config(?array $options = [])
{
if(!defined(self::$constantName)){
define(self::$constantName, $options);
}
return new static([], []);
}
/**
* Set the recipient(s) of the email.
*
* @param string|array $emails
* @return $this
*/
static public function to(...$emails)
{
if(func_num_args() === 1){
if(isset($emails[0]) && is_string($emails[0])){
$emails = explode(',', str_replace(["\r", "\n", " "], "", $emails[0]));
}
}
return new static(
$emails,
self::getConfig()
);
}
/**
* Add CC recipients.
*
* @param string|array $emails
* @return $this
*/
public function cc($emails)
{
$this->recipients['cc'] = $this->convert($emails, 'email');
return $this;
}
/**
* Add BCC recipients.
*
* @param string|array $emails
* @return $this
*/
public function bcc($emails)
{
$this->recipients['bcc'] = $this->convert($emails, 'email');
return $this;
}
/**
* Add Reply to recipients.
*
* @param string $emails
* @return $this
*/
public function __replyTo(...$emails)
{
[$address, $name] = [$emails[0], $emails[1] ?? null];
$this->recipients['reply_to'] = [$address, $name];
return $this;
}
/**
* Set the email subject.
*
* @param string $subject
* @return $this
*/
public function subject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* Set the email body.
*
* @param string $body
* @return $this
*/
public function body($body)
{
$this->body = $body;
return $this;
}
/**
* Set the email altbody.
*
* @param string $body
* @return $this
*/
public function __altBody($body)
{
$this->altbody = $body;
return $this;
}
/**
* Set the email body.
*
* @param string $driver
* - [optional] Default is smtp
*
* @return $this
*/
public function driver($driver = 'smtp')
{
$driver = Str::lower($driver);
$this->driver = match ($driver) {
'mail' => 'isMail',
'smtp' => 'isSMTP',
default => 'isSMTP'
};
return $this;
}
/**
* Add attachments to the email.
*
* @param string|array $attachments
* @return $this
*/
public function attach(...$attachments)
{
if(func_num_args() >= 1){
$attachments = ['path' => $attachments[0], 'as' => $attachments[1] ?? null];
}
$this->attachments = $this->formatAttachments($attachments);
foreach ($this->attachments as $path => $name) {
if(Tame()->exists($path)){
$this->mailer->addAttachment($path, $name);
}
}
return $this;
}
/**
* Delete attachments from server after mail has been sent
*
* @param bool $delete
* @return $this
*/
public function delete($delete)
{
$this->deleteAttachment = $delete;
return $this;
}
/**
* Flush Buffering from the server to avoid waiting for mail response before reload
*
* @param bool $flush
* @return $this
*/
public function flush($flush)
{
$this->flushBuffering = $flush;
return $this;
}
/**
* Debug error code for development purpose only
*
* @param int $debug
* @return $this
*/
public function debug($debug)
{
$this->debug = $debug;
return $this;
}
/**
* Proceed sending email
*
* @param callable $function
* @return mixed
*/
public function send(callable $function = null)
{
// configure smtp data
$this->configureSMTPData($this->options);
// create default options
$defaultOption = $this->getDefaultOption($this->options);
// setup mailer
$this->setupMailer($defaultOption);
foreach($this->recipients['to'] as $email){
// sending mail through auto crons flush
$this->ob_crons_flush(function() use ($defaultOption, $function, $email) {
try {
// Validate the recipient email
$verify = Tame()->emailValidator($email, true, true);
if (!$verify) {
throw new \Exception("Invalid email address: {$email}", 509); // Custom error code: 509
}
// email and name of receiver as name is optional
$this->mailer->addAddress($email);
// add cc
$this->addCC();
// add bcc
$this->addBCC();
// add reply to
$this->addReplyTo();
// Set email format to HTML
$this->mailer->isHTML(true);
// subject
$this->mailer->Subject = $this->subject;
$this->mailer->Body = $this->body;
// If support alternative message
if(!empty($this->altbody)){
$this->mailer->AltBody = $this->altbody;
}
// Connect
$this->mailer->SMTPConnect();
// send mail
$this->mailer->send();
// get message id
$mid = $this->mailer->getLastMessageID();
// clear added address
$this->mailer->clearAddresses();
// Close the SMTP session
$this->mailer->SMTPClose();
// if attachment delete is allowed
$this->deleteAttachment();
// $this->mail->ErrorInfo
if(is_callable($function)){
call_user_func($function, (object) [
'status' => 200,
'message' => 'Sent',
'mid' => $mid,
'to' => $email
]);
}
} catch (\Exception $e) {
if(is_callable($function)){
call_user_func($function, (object) [
'status' => $e->getCode(),
'message' => $e->getMessage(),
'mid' => null,
'to' => $email
]);
}
}
}, $defaultOption);
}
}
/**
* obFlush
*
* @return void
*/
public function obFlush()
{
@header("Connection: close");
@header("Content-length: " . ob_get_length());
@ob_end_flush();
@flush();
@session_write_close();
}
/**
* Flushes output buffer and sends data to client.
*
* @param callable|null $callback The function to execute after flushing the buffer.
* @param array|null $options The options to use during buffer flushing.
*
* @return void
*/
private function ob_crons_flush(callable $function, ?array $options = null)
{
// Disable output compression
if (!headers_sent()) {
@ini_set('zlib.output_compression', 'Off');
}
// Turn on implicit flushing
ob_implicit_flush(true);
// ignore user abort
ignore_user_abort(true);
// Disable script timeout
set_time_limit(0);
// turn on fast cgi
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}
// Set the HTTP response code ... only available in > PHP 5.4.0
http_response_code(200);
// Start output buffering
ob_start();
// execute code block
if(is_callable($function)){
$function(self::class);
}
$this->__obFlush($options);
// Disable implicit flushing
ob_implicit_flush(false);
}
/**
* Flushes output buffers and sends headers to enable server-side flushing.
*
* @param array $options An array of options to configure the flush behavior.
* Available options are:
* - 'flush' (bool) Whether to flush the email sending queue. Defaults to false.
* - 'debug' (int) Whether to enable debug mode. When debug mode is on, the email sending queue is not flushed. Defaults to false.
*
* @return void
*/
private function __obFlush(?array $options = [])
{
// Flush email sending queue
if ($options['flush'] && $options['debug'] === 0) {
if (!headers_sent()) {
@header('Surrogate-Control: BigPipe/1.0');
@header('X-Accel-Buffering: no');
@header("Content-Encoding: none");
@header("Connection: close");
@header("Content-Length: " . ob_get_length());
}
}
// Flush output buffers if active
if (ob_get_level() > 0) {
flush();
ob_flush();
ob_end_flush();
}
// Clean up output buffers if active
if (ob_get_level() > 0) {
ob_clean();
ob_end_clean();
}
// Set implicit flush to true to enable real-time output
ob_implicit_flush(true);
}
}