-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvEmail.php
2516 lines (2141 loc) · 62.9 KB
/
advEmail.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* advEmail.php
*
* Forked from geekMail to add extra features
* A standalone version of the CodeIgniter PHP Framework's Email library
*
* - Version 1.0
*
*
* This class is a standalone version of the Email library used in the CodeIgniter PHP Framework:
*
* - http://codeigniter.com/
*
*
* Most of the existing CodeIgniter documentation for the library should work, with the exception
* that the library won't be dynamically loaded from a CodeIgniter controller ("$this->load->..." and
* "$this->email->from..."):
*
* - http://codeigniter.com/user_guide/libraries/email.html
*
*
* Usage example:
* --------------
* <?php
*
* require 'advEmail.php';
*
* $advEmail = new advEmail();
*
* $advEmail->setMailType('html');
*
* $advEmail->from('noreply@mattclements.co.uk', 'Matt Clements');
*
* $advEmail->to('matt@mattclements.co.uk');
* //$advEmail->cc('matt@mattclements.co.uk');
* //$advEmail->bcc('matt@mattclements.co.uk');
*
* $advEmail->subject('Example subject');
*
* $advEmail->message('Example message.');
* $advEmail->set_alt_message('Text alternative email - manually set');
*
* $advEmail->attach('/home/matt/file1.txt');
* $advEmail->attach('/home/matt/file2.zip');
*
* if (!$advEmail->send())
* {
* $errors = $advEmail->getDebugger();
* print_r($errors);
* }
*
* ?>
*
*
* Adapted by: Matt Clements
* matt@mattclements.co.uk
* http://www.mattclements.co.uk
*
* GeekMail developed by: Willem van Zyl
* willem@geekology.co.za
* http://www.geekology.co.za/blog/
*
*
* ---
* As with CodeIgniter itself, this code is free and Open Source, but
* EllisLab, Inc. (the creators of CodeIgniter) require that this license
* agreement be included in all adaptations:
*
* http://codeigniter.com/user_guide/license.html
* ---
*/
class advEmail {
/**
* Used as the User-Agent and X-Mailer headers' value.
*
* @var string
*/
public $useragent = 'CodeIgniter';
/**
* Path to the Sendmail binary.
*
* @var string
*/
public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
/**
* Which method to use for sending e-mails.
*
* @var string 'mail', 'sendmail' or 'smtp'
*/
public $protocol = 'mail'; // mail/sendmail/smtp
/**
* STMP Server host
*
* @var string
*/
public $smtp_host = '';
/**
* SMTP Username
*
* @var string
*/
public $smtp_user = '';
/**
* SMTP Password
*
* @var string
*/
public $smtp_pass = '';
/**
* SMTP Server port
*
* @var int
*/
public $smtp_port = 25;
/**
* SMTP connection timeout in seconds
*
* @var int
*/
public $smtp_timeout = 5;
/**
* SMTP persistent connection
*
* @var bool
*/
public $smtp_keepalive = FALSE;
/**
* SMTP Encryption
*
* @var string empty, 'tls' or 'ssl'
*/
public $smtp_crypto = '';
/**
* Whether to apply word-wrapping to the message body.
*
* @var bool
*/
public $wordwrap = TRUE;
/**
* Number of characters to wrap at.
*
* @see CI_Email::$wordwrap
* @var int
*/
public $wrapchars = 76;
/**
* Message format.
*
* @var string 'text' or 'html'
*/
public $mailtype = 'text';
/**
* Character set (default: utf-8)
*
* @var string
*/
public $charset = 'utf-8';
/**
* Multipart message
*
* @var string 'mixed' (in the body) or 'related' (separate)
*/
public $multipart = 'mixed'; // "mixed" (in the body) or "related" (separate)
/**
* Alternative message (for HTML messages only)
*
* @var string
*/
public $alt_message = '';
/**
* Whether to validate e-mail addresses.
*
* @var bool
*/
public $validate = FALSE;
/**
* X-Priority header value.
*
* @var int 1-5
*/
public $priority = 3; // Default priority (1 - 5)
/**
* Newline character sequence.
* Use "\r\n" to comply with RFC 822.
*
* @link http://www.ietf.org/rfc/rfc822.txt
* @var string "\r\n" or "\n"
*/
public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
/**
* CRLF character sequence
*
* RFC 2045 specifies that for 'quoted-printable' encoding,
* "\r\n" must be used. However, it appears that some servers
* (even on the receiving end) don't handle it properly and
* switching to "\n", while improper, is the only solution
* that seems to work for all environments.
*
* @link http://www.ietf.org/rfc/rfc822.txt
* @var string
*/
public $crlf = "\n";
/**
* Whether to use Delivery Status Notification.
*
* @var bool
*/
public $dsn = FALSE;
/**
* Whether to send multipart alternatives.
* Yahoo! doesn't seem to like these.
*
* @var bool
*/
public $send_multipart = TRUE;
/**
* Whether to send messages to BCC recipients in batches.
*
* @var bool
*/
public $bcc_batch_mode = FALSE;
/**
* BCC Batch max number size.
*
* @see CI_Email::$bcc_batch_mode
* @var int
*/
public $bcc_batch_size = 200;
// --------------------------------------------------------------------
/**
* Whether PHP is running in safe mode. Initialized by the class constructor.
*
* @var bool
*/
protected $_safe_mode = FALSE;
/**
* Subject header
*
* @var string
*/
protected $_subject = '';
/**
* Message body
*
* @var string
*/
protected $_body = '';
/**
* Final message body to be sent.
*
* @var string
*/
protected $_finalbody = '';
/**
* multipart/alternative boundary
*
* @var string
*/
protected $_alt_boundary = '';
/**
* Attachment boundary
*
* @var string
*/
protected $_atc_boundary = '';
/**
* Final headers to send
*
* @var string
*/
protected $_header_str = '';
/**
* SMTP Connection socket placeholder
*
* @var resource
*/
protected $_smtp_connect = '';
/**
* Mail encoding
*
* @var string '8bit' or '7bit'
*/
protected $_encoding = '8bit';
/**
* Whether to perform SMTP authentication
*
* @var bool
*/
protected $_smtp_auth = FALSE;
/**
* Whether to send a Reply-To header
*
* @var bool
*/
protected $_replyto_flag = FALSE;
/**
* Debug messages
*
* @see CI_Email::print_debugger()
* @var string
*/
protected $_debug_msg = array();
/**
* Recipients
*
* @var string[]
*/
protected $_recipients = array();
/**
* CC Recipients
*
* @var string[]
*/
protected $_cc_array = array();
/**
* BCC Recipients
*
* @var string[]
*/
protected $_bcc_array = array();
/**
* Message headers
*
* @var string[]
*/
protected $_headers = array();
/**
* Attachment data
*
* @var array
*/
protected $_attachments = array();
/**
* Valid $protocol values
*
* @see CI_Email::$protocol
* @var string[]
*/
protected $_protocols = array('mail', 'sendmail', 'smtp');
/**
* Base charsets
*
* Character sets valid for 7-bit encoding,
* excluding language suffix.
*
* @var string[]
*/
protected $_base_charsets = array('us-ascii', 'iso-2022-');
/**
* Bit depths
*
* Valid mail encodings
*
* @see CI_Email::$_encoding
* @var string[]
*/
protected $_bit_depths = array('7bit', '8bit');
private $_log = array();
/**
* $priority translations
*
* Actual values to send with the X-Priority header
*
* @var string[]
*/
protected $_priorities = array(
1 => '1 (Highest)',
2 => '2 (High)',
3 => '3 (Normal)',
4 => '4 (Low)',
5 => '5 (Lowest)'
);
// --------------------------------------------------------------------
/**
* Constructor - Sets Email Preferences
*
* The constructor can be passed an array of config values
*
* @param array $config = array()
* @return void
*/
public function __construct(array $config = array())
{
//$this->charset = config_item('charset');
if (count($config) > 0)
{
$this->initialize($config);
}
else
{
$this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
}
$this->_safe_mode = ( ! $this->is_php('5.4') && ini_get('safe_mode'));
$this->charset = strtoupper($this->charset);
$this->_logMessage('debug', 'advEmail Class Initialized');
}
private function is_php($version)
{
static $_is_php;
$version = (string) $version;
if ( ! isset($_is_php[$version]))
{
$_is_php[$version] = version_compare(PHP_VERSION, $version, '>=');
}
return $_is_php[$version];
}
/**
* Logging methods to store and retrieve messages.
*/
private function _logMessage($type, $message)
{
//check that a valid log type was specified:
switch ($type)
{
case 'debug': break;
case 'error': break;
case 'info': break;
default: $type = 'debug';
}
//log the message:
$this->_log[] = array('date' => date('Y-m-d H:i:s'),
'type' => $type,
'message' => $message
);
}
public function getLog()
{
return $this->_log;
}
// --------------------------------------------------------------------
/**
* Destructor - Releases Resources
*
* @return void
*/
public function __destruct()
{
if (is_resource($this->_smtp_connect))
{
$this->_send_command('quit');
}
}
// --------------------------------------------------------------------
/**
* Initialize preferences
*
* @param array
* @return CI_Email
*/
public function initialize($config = array())
{
foreach ($config as $key => $val)
{
if (isset($this->$key))
{
$method = 'set_'.$key;
if (method_exists($this, $method))
{
$this->$method($val);
}
else
{
$this->$key = $val;
}
}
}
$this->clear();
$this->_smtp_auth = ! ($this->smtp_user === '' && $this->smtp_pass === '');
return $this;
}
// --------------------------------------------------------------------
/**
* Initialize the Email Data
*
* @param bool
* @return CI_Email
*/
public function clear($clear_attachments = FALSE)
{
$this->_subject = '';
$this->_body = '';
$this->_finalbody = '';
$this->_header_str = '';
$this->_replyto_flag = FALSE;
$this->_recipients = array();
$this->_cc_array = array();
$this->_bcc_array = array();
$this->_headers = array();
$this->_debug_msg = array();
$this->set_header('User-Agent', $this->useragent);
$this->set_header('Date', $this->_set_date());
if ($clear_attachments !== FALSE)
{
$this->_attachments = array();
}
return $this;
}
// --------------------------------------------------------------------
/**
* Set FROM
*
* @param string $from
* @param string $name
* @param string $return_path = NULL Return-Path
* @return CI_Email
*/
public function from($from, $name = '', $return_path = NULL)
{
if (preg_match('/\<(.*)\>/', $from, $match))
{
$from = $match[1];
}
if ($this->validate)
{
$this->validate_email($this->_str_to_array($from));
if ($return_path)
{
$this->validate_email($this->_str_to_array($return_path));
}
}
// prepare the display name
if ($name !== '')
{
// only use Q encoding if there are characters that would require it
if ( ! preg_match('/[\200-\377]/', $name))
{
// add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
$name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
}
else
{
$name = $this->_prep_q_encoding($name);
}
}
$this->set_header('From', $name.' <'.$from.'>');
isset($return_path) OR $return_path = $from;
$this->set_header('Return-Path', '<'.$return_path.'>');
return $this;
}
// --------------------------------------------------------------------
/**
* Set Reply-to
*
* @param string
* @param string
* @return CI_Email
*/
public function reply_to($replyto, $name = '')
{
if (preg_match('/\<(.*)\>/', $replyto, $match))
{
$replyto = $match[1];
}
if ($this->validate)
{
$this->validate_email($this->_str_to_array($replyto));
}
if ($name === '')
{
$name = $replyto;
}
if (strpos($name, '"') !== 0)
{
$name = '"'.$name.'"';
}
$this->set_header('Reply-To', $name.' <'.$replyto.'>');
$this->_replyto_flag = TRUE;
return $this;
}
// --------------------------------------------------------------------
/**
* Set Recipients
*
* @param string
* @return CI_Email
*/
public function to($to)
{
$to = $this->_str_to_array($to);
$to = $this->clean_email($to);
if ($this->validate)
{
$this->validate_email($to);
}
if ($this->_get_protocol() !== 'mail')
{
$this->set_header('To', implode(', ', $to));
}
$this->_recipients = $to;
return $this;
}
// --------------------------------------------------------------------
/**
* Set CC
*
* @param string
* @return CI_Email
*/
public function cc($cc)
{
$cc = $this->clean_email($this->_str_to_array($cc));
if ($this->validate)
{
$this->validate_email($cc);
}
$this->set_header('Cc', implode(', ', $cc));
if ($this->_get_protocol() === 'smtp')
{
$this->_cc_array = $cc;
}
return $this;
}
// --------------------------------------------------------------------
/**
* Set BCC
*
* @param string
* @param string
* @return CI_Email
*/
public function bcc($bcc, $limit = '')
{
if ($limit !== '' && is_numeric($limit))
{
$this->bcc_batch_mode = TRUE;
$this->bcc_batch_size = $limit;
}
$bcc = $this->clean_email($this->_str_to_array($bcc));
if ($this->validate)
{
$this->validate_email($bcc);
}
if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
{
$this->_bcc_array = $bcc;
}
else
{
$this->set_header('Bcc', implode(', ', $bcc));
}
return $this;
}
// --------------------------------------------------------------------
/**
* Set Email Subject
*
* @param string
* @return CI_Email
*/
public function subject($subject)
{
$subject = $this->_prep_q_encoding($subject);
$this->set_header('Subject', $subject);
return $this;
}
// --------------------------------------------------------------------
/**
* Set Body
*
* @param string
* @return CI_Email
*/
public function message($body)
{
$this->_body = rtrim(str_replace("\r", '', $body));
/* strip slashes only if magic quotes is ON
if we do it with magic quotes OFF, it strips real, user-inputted chars.
NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
it will probably not exist in future versions at all.
*/
if ( ! $this->is_php('5.4') && get_magic_quotes_gpc())
{
$this->_body = stripslashes($this->_body);
}
return $this;
}
// --------------------------------------------------------------------
/**
* Assign file attachments
*
* @param string $file Can be local path, URL or buffered content
* @param string $disposition = 'attachment'
* @param string $newname = NULL
* @param string $mime = ''
* @return CI_Email
*/
public function attach($file, $disposition = '', $newname = NULL, $mime = '')
{
if ($mime === '')
{
if (strpos($file, '://') === FALSE && ! file_exists($file))
{
$this->_setErrorMessage('email_attachment_missing');
return FALSE;
}
if ( ! $fp = @fopen($file, 'rb'))
{
$this->_setErrorMessage('email_attachment_unreadable');
return FALSE;
}
$file_content = stream_get_contents($fp);
$mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
fclose($fp);
}
else
{
$file_content =& $file; // buffered file
}
$this->_attachments[] = array(
'name' => array($file, $newname),
'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
'type' => $mime,
'content' => chunk_split(base64_encode($file_content))
);
return $this;
}
// --------------------------------------------------------------------
/**
* Set and return attachment Content-ID
*
* Useful for attached inline pictures
*
* @param string $filename
* @return string
*/
public function attachment_cid($filename)
{
if ($this->multipart !== 'related')
{
$this->multipart = 'related'; // Thunderbird need this for inline images
}
for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
{
if ($this->_attachments[$i]['name'][0] === $filename)
{
$this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
return $this->_attachments[$i]['cid'];
}
}
return FALSE;
}
// --------------------------------------------------------------------
/**
* Add a Header Item
*
* @param string
* @param string
* @return CI_Email
*/
public function set_header($header, $value)
{
$this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
return $this;
}
// --------------------------------------------------------------------
/**
* Convert a String to an Array
*
* @param string
* @return array
*/
protected function _str_to_array($email)
{
if ( ! is_array($email))
{
return (strpos($email, ',') !== FALSE)
? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
: (array) trim($email);
}
return $email;
}
// --------------------------------------------------------------------
/**
* Set Multipart Value
*
* @param string
* @return CI_Email
*/
public function set_alt_message($str)
{
$this->alt_message = (string) $str;
return $this;
}
// --------------------------------------------------------------------
/**
* Set Mailtype
*
* @param string
* @return CI_Email
*/
public function setMailType($type = 'text')
{
$this->mailtype = ($type === 'html') ? 'html' : 'text';
return $this;
}
// --------------------------------------------------------------------
/**
* Set Wordwrap
*
* @param bool
* @return CI_Email
*/
public function set_wordwrap($wordwrap = TRUE)
{
$this->wordwrap = (bool) $wordwrap;
return $this;
}
// --------------------------------------------------------------------
/**
* Set Protocol
*
* @param string
* @return CI_Email
*/
public function set_protocol($protocol = 'mail')
{
$this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
return $this;
}
// --------------------------------------------------------------------
/**
* Set Priority
*
* @param int
* @return CI_Email
*/
public function set_priority($n = 3)
{
$this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
return $this;
}
// --------------------------------------------------------------------
/**
* Set Newline Character
*
* @param string
* @return CI_Email
*/
public function set_newline($newline = "\n")
{
$this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
return $this;
}
// --------------------------------------------------------------------
/**
* Set CRLF
*
* @param string
* @return CI_Email
*/
public function set_crlf($crlf = "\n")
{
$this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
return $this;
}
// --------------------------------------------------------------------
/**
* Set Message Boundary
*
* @return void