-
Notifications
You must be signed in to change notification settings - Fork 88
/
class.simple_mail.php
executable file
·714 lines (656 loc) · 15.5 KB
/
class.simple_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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
<?php
/**
* Simple Mail
*
* A simple PHP wrapper class for sending email using the mail() method.
*
* PHP version > 5.2
*
* LICENSE: This source file is subject to the MIT license, which is
* available through the world-wide-web at the following URI:
* http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt
*
* @category SimpleMail
* @package SimpleMail
* @author Eoghan O'Brien <[email protected]>
* @copyright 2009 - 2017 Eoghan O'Brien
* @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT
* @version 1.7.1
* @link http://github.com/eoghanobrien/php-simple-mail
*/
/**
* Simple Mail class.
*
* @category SimpleMail
* @package SimpleMail
* @author Eoghan O'Brien <[email protected]>
* @copyright 2009 - 2017 Eoghan O'Brien
* @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT
* @version 1.7.1
* @link http://github.com/eoghanobrien/php-simple-mail
*/
class SimpleMail
{
/**
* @var int $_wrap
*/
protected $_wrap = 78;
/**
* @var array $_to
*/
protected $_to = array();
/**
* @var string $_subject
*/
protected $_subject;
/**
* @var string $_message
*/
protected $_message;
/**
* @var array $_headers
*/
protected $_headers = array();
/**
* @var string $_parameters
*/
protected $_params;
/**
* @var array $_attachments
*/
protected $_attachments = array();
/**
* @var string $_uid
*/
protected $_uid;
/**
* Named constructor.
*
* @return static
*/
public static function make()
{
return new SimpleMail();
}
/**
* __construct
*
* Resets the class properties.
*/
public function __construct()
{
$this->reset();
}
/**
* reset
*
* Resets all properties to initial state.
*
* @return self
*/
public function reset()
{
$this->_to = array();
$this->_headers = array();
$this->_subject = null;
$this->_message = null;
$this->_wrap = 78;
$this->_params = null;
$this->_attachments = array();
$this->_uid = $this->getUniqueId();
return $this;
}
/**
* setTo
*
* @param string $email The email address to send to.
* @param string $name The name of the person to send to.
*
* @return self
*/
public function setTo($email, $name)
{
$this->_to[] = $this->formatHeader((string) $email, (string) $name);
return $this;
}
/**
* getTo
*
* Return an array of formatted To addresses.
*
* @return array
*/
public function getTo()
{
return $this->_to;
}
/**
* setFrom
*
* @param string $email The email to send as from.
* @param string $name The name to send as from.
*
* @return self
*/
public function setFrom($email, $name)
{
$this->addMailHeader('From', (string) $email, (string) $name);
return $this;
}
/**
* setCc
*
* @param array $pairs An array of name => email pairs.
*
* @return self
*/
public function setCc(array $pairs)
{
return $this->addMailHeaders('Cc', $pairs);
}
/**
* setBcc
*
* @param array $pairs An array of name => email pairs.
*
* @return self
*/
public function setBcc(array $pairs)
{
return $this->addMailHeaders('Bcc', $pairs);
}
/**
* setReplyTo
*
* @param string $email
* @param string $name
*
* @return self
*/
public function setReplyTo($email, $name = null)
{
return $this->addMailHeader('Reply-To', $email, $name);
}
/**
* setHtml
*
* @return self
*/
public function setHtml()
{
return $this->addGenericHeader(
'Content-Type', 'text/html; charset="utf-8"'
);
}
/**
* setSubject
*
* @param string $subject The email subject
*
* @return self
*/
public function setSubject($subject)
{
$this->_subject = $this->encodeUtf8(
$this->filterOther((string) $subject)
);
return $this;
}
/**
* getSubject function.
*
* @return string
*/
public function getSubject()
{
return $this->_subject;
}
/**
* setMessage
*
* @param string $message The message to send.
*
* @return self
*/
public function setMessage($message)
{
$this->_message = str_replace("\n.", "\n..", (string) $message);
return $this;
}
/**
* getMessage
*
* @return string
*/
public function getMessage()
{
return $this->_message;
}
/**
* addAttachment
*
* @param string $path The file path to the attachment.
* @param string $filename The filename of the attachment when emailed.
* @param null $data
*
* @return self
*/
public function addAttachment($path, $filename = null, $data = null)
{
$filename = empty($filename) ? basename($path) : $filename;
$filename = $this->encodeUtf8($this->filterOther((string) $filename));
$data = empty($data) ? $this->getAttachmentData($path) : $data;
$this->_attachments[] = array(
'path' => $path,
'file' => $filename,
'data' => chunk_split(base64_encode($data))
);
return $this;
}
/**
* getAttachmentData
*
* @param string $path The path to the attachment file.
*
* @return string
*/
public function getAttachmentData($path)
{
$filesize = filesize($path);
$handle = fopen($path, "r");
$attachment = fread($handle, $filesize);
fclose($handle);
return $attachment;
}
/**
* addMailHeader
*
* @param string $header The header to add.
* @param string $email The email to add.
* @param string $name The name to add.
*
* @return self
*/
public function addMailHeader($header, $email, $name = null)
{
$address = $this->formatHeader((string) $email, (string) $name);
$this->_headers[] = sprintf('%s: %s', (string) $header, $address);
return $this;
}
/**
* addMailHeaders
*
* @param string $header The header to add.
* @param array $pairs An array of name => email pairs.
*
* @return self
*/
public function addMailHeaders($header, array $pairs)
{
if (count($pairs) === 0) {
throw new InvalidArgumentException(
'You must pass at least one name => email pair.'
);
}
$addresses = array();
foreach ($pairs as $name => $email) {
$name = is_numeric($name) ? null : $name;
$addresses[] = $this->formatHeader($email, $name);
}
$this->addGenericHeader($header, implode(',', $addresses));
return $this;
}
/**
* addGenericHeader
*
* @param string $header The generic header to add.
* @param mixed $value The value of the header.
*
* @return self
*/
public function addGenericHeader($header, $value)
{
$this->_headers[] = sprintf(
'%s: %s',
(string) $header,
(string) $value
);
return $this;
}
/**
* getHeaders
*
* Return the headers registered so far as an array.
*
* @return array
*/
public function getHeaders()
{
return $this->_headers;
}
/**
* setAdditionalParameters
*
* Such as "[email protected]
*
* @param string $additionalParameters The addition mail parameter.
*
* @return self
*/
public function setParameters($additionalParameters)
{
$this->_params = (string) $additionalParameters;
return $this;
}
/**
* getAdditionalParameters
*
* @return string
*/
public function getParameters()
{
return $this->_params;
}
/**
* setWrap
*
* @param int $wrap The number of characters at which the message will wrap.
*
* @return self
*/
public function setWrap($wrap = 78)
{
$wrap = (int) $wrap;
if ($wrap < 1) {
$wrap = 78;
}
$this->_wrap = $wrap;
return $this;
}
/**
* getWrap
*
* @return int
*/
public function getWrap()
{
return $this->_wrap;
}
/**
* hasAttachments
*
* Checks if the email has any registered attachments.
*
* @return bool
*/
public function hasAttachments()
{
return !empty($this->_attachments);
}
/**
* assembleAttachment
*
* @return string
*/
public function assembleAttachmentHeaders()
{
$head = array();
$head[] = "MIME-Version: 1.0";
$head[] = "Content-Type: multipart/mixed; boundary=\"{$this->_uid}\"";
return join(PHP_EOL, $head);
}
/**
* assembleAttachmentBody
*
* @return string
*/
public function assembleAttachmentBody()
{
$body = array();
$body[] = "This is a multi-part message in MIME format.";
$body[] = "--{$this->_uid}";
$body[] = "Content-Type: text/html; charset=\"utf-8\"";
$body[] = "Content-Transfer-Encoding: quoted-printable";
$body[] = "";
$body[] = quoted_printable_encode($this->_message);
$body[] = "";
$body[] = "--{$this->_uid}";
foreach ($this->_attachments as $attachment) {
$body[] = $this->getAttachmentMimeTemplate($attachment);
}
return implode(PHP_EOL, $body) . '--';
}
/**
* getAttachmentMimeTemplate
*
* @param array $attachment An array containing 'file' and 'data' keys.
*
* @return string
*/
public function getAttachmentMimeTemplate($attachment)
{
$file = $attachment['file'];
$data = $attachment['data'];
$head = array();
$head[] = "Content-Type: application/octet-stream; name=\"{$file}\"";
$head[] = "Content-Transfer-Encoding: base64";
$head[] = "Content-Disposition: attachment; filename=\"{$file}\"";
$head[] = "";
$head[] = $data;
$head[] = "";
$head[] = "--{$this->_uid}";
return implode(PHP_EOL, $head);
}
/**
* send
*
* @return boolean
* @throws \RuntimeException on no 'To: ' address to send to.
*/
public function send()
{
$to = $this->getToForSend();
$headers = $this->getHeadersForSend();
if (empty($to)) {
throw new \RuntimeException(
'Unable to send, no To address has been set.'
);
}
if ($this->hasAttachments()) {
$message = $this->assembleAttachmentBody();
$headers .= PHP_EOL . $this->assembleAttachmentHeaders();
} else {
$message = $this->getWrapMessage();
}
return mail($to, $this->_subject, $message, $headers, $this->_params);
}
/**
* debug
*
* @return string
*/
public function debug()
{
return '<pre>' . print_r($this, true) . '</pre>';
}
/**
* magic __toString function
*
* @return string
*/
public function __toString()
{
return print_r($this, true);
}
/**
* formatHeader
*
* Formats a display address for emails according to RFC2822 e.g.
* Name <[email protected]>
*
* @param string $email The email address.
* @param string $name The display name.
*
* @return string
*/
public function formatHeader($email, $name = null)
{
$email = $this->filterEmail((string) $email);
if (empty($name)) {
return $email;
}
$name = $this->encodeUtf8($this->filterName((string) $name));
return sprintf('"%s" <%s>', $name, $email);
}
/**
* encodeUtf8
*
* @param string $value The value to encode.
*
* @return string
*/
public function encodeUtf8($value)
{
$value = trim($value);
if (preg_match('/(\s)/', $value)) {
return $this->encodeUtf8Words($value);
}
return $this->encodeUtf8Word($value);
}
/**
* encodeUtf8Word
*
* @param string $value The word to encode.
*
* @return string
*/
public function encodeUtf8Word($value)
{
return sprintf('=?UTF-8?B?%s?=', base64_encode($value));
}
/**
* encodeUtf8Words
*
* @param string $value The words to encode.
*
* @return string
*/
public function encodeUtf8Words($value)
{
$words = explode(' ', $value);
$encoded = array();
foreach ($words as $word) {
$encoded[] = $this->encodeUtf8Word($word);
}
return join($this->encodeUtf8Word(' '), $encoded);
}
/**
* filterEmail
*
* Removes any carriage return, line feed, tab, double quote, comma
* and angle bracket characters before sanitizing the email address.
*
* @param string $email The email to filter.
*
* @return string
*/
public function filterEmail($email)
{
$rule = array(
"\r" => '',
"\n" => '',
"\t" => '',
'"' => '',
',' => '',
'<' => '',
'>' => ''
);
$email = strtr($email, $rule);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
return $email;
}
/**
* filterName
*
* Removes any carriage return, line feed or tab characters. Replaces
* double quotes with single quotes and angle brackets with square
* brackets, before sanitizing the string and stripping out html tags.
*
* @param string $name The name to filter.
*
* @return string
*/
public function filterName($name)
{
$rule = array(
"\r" => '',
"\n" => '',
"\t" => '',
'"' => "'",
'<' => '[',
'>' => ']',
);
$filtered = filter_var(
$name,
FILTER_SANITIZE_STRING,
FILTER_FLAG_NO_ENCODE_QUOTES
);
return trim(strtr($filtered, $rule));
}
/**
* filterOther
*
* Removes ASCII control characters including any carriage return, line
* feed or tab characters.
*
* @param string $data The data to filter.
*
* @return string
*/
public function filterOther($data)
{
return filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
}
/**
* getHeadersForSend
*
* @return string
*/
public function getHeadersForSend()
{
if (empty($this->_headers)) {
return '';
}
return join(PHP_EOL, $this->_headers);
}
/**
* getToForSend
*
* @return string
*/
public function getToForSend()
{
if (empty($this->_to)) {
return '';
}
return join(', ', $this->_to);
}
/**
* getUniqueId
*
* @return string
*/
public function getUniqueId()
{
return md5(uniqid(time()));
}
/**
* getWrapMessage
*
* @return string
*/
public function getWrapMessage()
{
return wordwrap($this->_message, $this->_wrap);
}
}