-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimplemail.php
More file actions
648 lines (564 loc) · 15.4 KB
/
simplemail.php
File metadata and controls
648 lines (564 loc) · 15.4 KB
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
<?php
/**
* LightPHP Framework
* LitePHP is a framework that has been designed to be lite waight, extensible and fast.
*
* @author Robert Pitt <robertpitt1988@gmail.com>
* @category core
* @copyright 2013 Robert Pitt
* @license GPL v3 - GNU Public License v3
* @version 1.0.0
*/
/**
* This class phpsimplemail
* @link(PHP Simple Mail, https://github.com/eoghanobrien/php-simple-mail)
*/
class Simplemail_Library
{
/**
* Control-Return-Line-Feed used to seperate data in the email.
*/
const CRLF = "\r\n";
/**
* @var int $_wrap
*/
protected $_wrap = 78;
/**
* @var string $_to
*/
protected $_to = array();
/**
* @var string $_bcc
*/
protected $_bcc = array();
/**
* @var string $_subject
*/
protected $_subject;
/**
* @var string $_message
*/
protected $_message;
/**
* @var array $_headers
*/
protected $_headers = array();
/**
* @var string $_parameters
*/
protected $_parameters;
/**
* @var array $_attachments
*/
protected $_attachments = array();
/**
* @var array $_attachmentsPath
*/
protected $_attachmentsPath = array();
/**
* @var array $_attachment_filename
*/
protected $_attachmentsFilename = array();
/**
* __construct.
*/
public function __construct()
{
$this->reset();
}
/**
* Resets all variables to initial state.
*
* @return Simplemail_Library
*/
public function reset()
{
$this->to = array();
$this->bcc = array();
$this->_headers = array();
$this->_subject = null;
$this->_message = null;
$this->_wrap = 78;
$this->_parameters = null;
$this->_attachments = array();
$this->_attachmentsPath = array();
$this->_attachmentsFilename = array();
//Return the context
return $this;
}
/**
* setTo.
*
* @param string $email
* @param string $name
*
* @throws InvalidArgumentException on non string value for $email
* @throws InvalidArgumentException on non string value for $name
*
* @return Simplemail_Library
*/
public function setTo($email, $name)
{
if (! is_string($email)) {
throw new InvalidArgumentException('$email - must be a string');
}
if (! is_string($name)) {
throw new InvalidArgumentException('$name - must be a string.');
}
$this->to[] = $this->formatHeader($email, $name);
return $this;
}
/**
* setTo.
*
* @param string $email
* @param string $name
*
* @throws InvalidArgumentException on non string value for $email
* @throws InvalidArgumentException on non string value for $name
*
* @return Simplemail_Library
*/
public function setBcc($email, $name)
{
if (! is_string($email)) {
throw new InvalidArgumentException('$email - must be a string');
}
if (! is_string($name)) {
throw new InvalidArgumentException('$name - must be a string.');
}
$this->bcc[] = $this->formatHeader($email, $name);
return $this;
}
/**
* Return an array of formatted To addresses.
*
* @return array
*/
public function getTo()
{
return $this->to;
}
/**
* setSubject function.
*
* @param string$subject
* @throws InvalidArgumentException on non string value for $subject
* @return Simplemail_Library
*/
public function setSubject($subject)
{
if (! is_string($subject)) {
throw new InvalidArgumentException('$subject - must be a string.');
}
$this->_subject = $this->filterOther($subject);
return $this;
}
/**
* getSubject function.
*
* @return string
*/
public function getSubject()
{
return $this->_subject;
}
/**
* setMessage function.
*
* @access public
* @param string $message
* @throws InvalidArgumentException on non string value for $message
* @return Simplemail_Library
*/
public function setMessage($message)
{
if (! is_string($message)) {
throw new InvalidArgumentException('$message - must be a string.');
}
$this->_message = str_replace("\n.", "\n..", $message);
return $this;
}
/**
* getMessage function.
*
* @return string
*/
public function getMessage()
{
return $this->_message;
}
/**
* addAttachment function.
*
* @todo Test this.
* @access public
* @param string $path
* @param string $filename
* @return Simplemail_Library
*/
public function addAttachment($path, $filename = null)
{
$filename = empty($filename) ? basename($path) : $filename;
$this->addAttachmentPath($path);
$this->addAttachmentFilename($filename);
$this->_attachments[] = $this->getAttachmentData($path);
return $this;
}
/**
* addAttachmentPath function.
*
* @todo Test this.
* @param string $path
* @return Simplemail_Library
*/
public function addAttachmentPath($path)
{
$this->_attachmentsPath[] = $path;
return $this;
}
/**
* addAttachmentFilename function.
*
* @todo Test this.
* @param string $filename
* @return Simplemail_Library
*/
public function addAttachmentFilename($filename)
{
$this->_attachmentsFilename[] = $filename;
return $this;
}
/**
* getAttachmentData function.
*
* @todo Test this.
* @param string $path
* @return string
*/
public function getAttachmentData($path)
{
$filesize = filesize($path);
$handle = fopen($path, "r");
$attachment = fread($handle, $filesize);
fclose($handle);
return chunk_split(base64_encode($attachment));
}
/**
* setFrom.
*
* @param string $email
* @param string $name
*
* @throws InvalidArgumentException on non string value for $email
* @throws InvalidArgumentException on non string value for $name
*
* @return Simplemail_Library
*/
public function setFrom($email, $name)
{
if ( ! is_string($email)) {
throw new InvalidArgumentException();
}
if ( ! is_string($name)) {
throw new InvalidArgumentException();
}
$this->addMailHeader('From', $email, $name);
return $this;
}
/**
* addMailHeader function.
*
* @param string $header
* @param string $email
* @param string $name
*
* @throws InvalidArgumentException on non string value for $header
* @throws InvalidArgumentException on non string value for $email
* @throws InvalidArgumentException on non string value for $name
*
* @return Simplemail_Library
*/
public function addMailHeader($header, $email = null, $name = null)
{
if ( ! is_string($header)) {
throw new InvalidArgumentException('$header must be a string.');
}
if ( ! is_string($email)) {
throw new InvalidArgumentException('$email must be a string.');
}
if ( ! is_string($name)) {
throw new InvalidArgumentException('$name must be a string.');
}
$address = $this->formatHeader($email, $name);
$this->_headers[] = sprintf('%s: %s', $header, $address);
return $this;
}
/**
* addGenericHeader function.
*
* @param string $header
* @param mixed $value
*
* @throws InvalidArgumentException on non string value for $header
* @throws InvalidArgumentException on non string value for $value
*
* @return Simplemail_Library
*/
public function addGenericHeader($header, $value)
{
if ( ! is_string($header)) {
throw new InvalidArgumentException('$header must be a string.');
}
if ( ! is_string($value) || ! is_string($value)) {
throw new InvalidArgumentException('$value must be a string.');
}
$this->_headers[] = "$header: $value";
return $this;
}
/**
* Return the headers registered so far as an array.
*
* @return array
*/
public function getHeaders()
{
return $this->_headers;
}
/**
* setAdditionalParameters function.
*
* Such as "-fyouremail@yourserver.com
*
* @param string $additionalParameters
* @throws InvalidArgumentException on non string $additionalParameters
* @return Simplemail_Library
*/
public function setParameters($additionalParameters)
{
if (! is_string($additionalParameters)) {
throw new InvalidArgumentException(
'$additionalParameters must be a string.'
);
}
$this->_parameters = $additionalParameters;
return $this;
}
/**
* getAdditionalParameters function
*
* @return string
*/
public function getParameters()
{
return $this->_parameters;
}
/**
* setWrap function.
*
* @param int $wrap
*
* @throws InvalidArgumentException on non int value
* @throws InvalidArgumentException on int less than 1 for $wrap
*
* @return Simplemail_Library
*/
public function setWrap($wrap = 78)
{
if (! is_int($wrap) || $wrap < 1) {
throw new InvalidArgumentException('Wrap must be an integer larger than 0');
}
$this->_wrap = $wrap;
return $this;
}
/**
* getWrap function.
*
* @return int
*/
public function getWrap()
{
return $this->_wrap;
}
/**
* Checks if the email has an registered attachments.
*
* @return bool
*/
public function hasAttachments()
{
return !!count($this->_attachments);
}
/**
* assembleAttachment function.
*
* @return string
*/
public function assembleAttachmentHeaders()
{
$uid = md5(uniqid(time()));
/**
* Construct the headers
* @var string
*/
$headers = sprintf('MIME-Version: 1.0%s', self::CRLF);
/**
* Add Bcc to the headers
*/
$bcc = (is_array($this->bcc) && !empty($this->bcc)) ? join(", ", $this->bcc) : false;
if($bcc)
{
$headers .= sprintf('Bcc: %s%s', $bcc, self::CRLF);
}
$headers .= sprintf('Content-Type: multipart/mixed; boundary="%s"%s%s',$uid,self::CRLF,self::CRLF);
$headers .= sprintf('This is a multi-part message in MIME format.%s',self::CRLF);
//Html Version
$headers .= sprintf('--%s%s', $uid, self::CRLF);
$headers .= sprintf('Content-type: text/html; charset="utf-8"%s', self::CRLF);
$headers .= sprintf('Content-Transfer-Encoding: 7bit%s%s', self::CRLF, self::CRLF);
$headers .= sprintf('%s%s%s', $this->_message, self::CRLF, self::CRLF);
//Attachemnts
$headers .= sprintf('--%s%s', $uid, self::CRLF);
foreach ($this->_attachmentsFilename as $key => $value) {
$headers .= sprintf('Content-Type: application/octet-stream; name="%s"%s',$value,self::CRLF);
$headers .= sprintf('Content-Transfer-Encoding: base64%s', self::CRLF);
$headers .= sprintf('Content-Disposition: attachment; filename="%s"%s%s',$value,self::CRLF,self::CRLF);
$headers .= sprintf('%s%s%s', $this->_attachments[$key], self::CRLF, self::CRLF);
$headers .= sprintf('--%s%s', $uid, self::CRLF);
}
return $headers;
}
/**
* send function.
*
* @throws RuntimeException on no To: address to send to
* @return boolean
*/
public function send()
{
$headers = (!empty($this->_headers)) ? join("\r\n", $this->_headers) : '';
$to = (is_array($this->to) && !empty($this->to)) ? join(", ", $this->to) : false;
if ($to === false)
{
throw new RuntimeException('Unable to send, no To address has been set.');
}
/**
* Assemble the html message and attachment
*/
$headers .= self::CRLF . $this->assembleAttachmentHeaders();
return mail($to, $this->_subject, "", $headers, $this->_parameters);
}
/**
* debug function.
*
* @return string
*/
public function debug()
{
return '<pre>'.print_r($this, 1).'</pre>';
}
/**
* magic __toString function.
*
* @return string
*/
public function __toString()
{
return print_r($this, 1);
}
/**
* formatHeader.
*
* Formats a display address for emails according to RFC2822 e.g.
* Name <address@domain.tld>
*
* @todo Test this.
* @param string $email
* @param string $name
* @return string
*/
public function formatHeader($email, $name = null)
{
$email = $this->filterEmail($email);
if (is_null($name)) {
return $email;
}
$name = $this->filterName($name);
return sprintf('%s <%s>', $name, $email);
}
/**
* filterEmail.
*
* Removes any carriage return, line feed, tab, double quote, comma
* and angle bracket characters before sanitizing the email address.
*
* @todo Test this.
* @param string $email
* @return string
*/
public function filterEmail($email)
{
$rule = array("\r" => '',
"\n" => '',
"\t" => '',
'"' => '',
',' => '',
'<' => '',
'>' => '',
);
$email = strtr($email, $rule);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
return $email;
}
/**
* Filter of name data.
*
* 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.
*
* @todo Test this.
* @param string $name
* @return string
*/
public function filterName($name)
{
$rule = array("\r" => '',
"\n" => '',
"\t" => '',
'"' => "'",
'<' => '[',
'>' => ']',
);
return trim(
strtr(
filter_var(
$name, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH
),
$rule
)
);
}
/**
* Filter of other data.
*
* Removes any carriage return, line feed or tab characters.
*
* @todo Test this.
* @param string $data
* @return string
*/
public function filterOther($data)
{
$rule = array("\r" => '',
"\n" => '',
"\t" => '',
);
return strtr(
filter_var(
$data, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH
),
$rule
);
}
}