blob: 158c82af29efe4284ceccfd0d582581816dcdb0f [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter Email Class
20 *
21 * Permits email to be sent using Mail, Sendmail, or SMTP.
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Libraries
26 * @author Rick Ellis
27 * @link http://www.codeigniter.com/user_guide/libraries/email.html
28 */
29class CI_Email {
30
31 var $useragent = "Code Igniter";
32 var $mailpath = "/usr/sbin/sendmail"; // Sendmail path
33 var $protocol = "mail"; // mail/sendmail/smtp
34 var $smtp_host = ""; // SMTP Server. Example: mail.earthlink.net
35 var $smtp_user = ""; // SMTP Username
36 var $smtp_pass = ""; // SMTP Password
37 var $smtp_port = "25"; // SMTP Port
38 var $smtp_timeout = 5; // SMTP Timeout in seconds
39 var $wordwrap = TRUE; // true/false Turns word-wrap on/off
40 var $wrapchars = "76"; // Number of characters to wrap at.
41 var $mailtype = "text"; // text/html Defines email formatting
42 var $charset = "utf-8"; // Default char set: iso-8859-1 or us-ascii
43 var $multipart = "mixed"; // "mixed" (in the body) or "related" (separate)
44 var $alt_message = ''; // Alternative message for HTML emails
45 var $validate = FALSE; // true/false. Enables email validation
46 var $priority = "3"; // Default priority (1 - 5)
47 var $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
48 var $bcc_batch_mode = FALSE; // true/false Turns on/off Bcc batch feature
49 var $bcc_batch_size = 200; // If bcc_batch_mode = true, sets max number of Bccs in each batch
50 var $_subject = "";
51 var $_body = "";
52 var $_finalbody = "";
53 var $_alt_boundary = "";
54 var $_atc_boundary = "";
55 var $_header_str = "";
56 var $_smtp_connect = "";
57 var $_encoding = "8bit";
58 var $_safe_mode = FALSE;
59 var $_IP = FALSE;
60 var $_smtp_auth = FALSE;
61 var $_replyto_flag = FALSE;
62 var $_debug_msg = array();
63 var $_recipients = array();
64 var $_cc_array = array();
65 var $_bcc_array = array();
66 var $_headers = array();
67 var $_attach_name = array();
68 var $_attach_type = array();
69 var $_attach_disp = array();
70 var $_protocols = array('mail', 'sendmail', 'smtp');
71 var $_base_charsets = array('iso-8859-1', 'us-ascii');
72 var $_bit_depths = array('7bit', '8bit');
73 var $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
74
75
76 /**
77 * Constructor - Sets Email Preferences
78 *
79 * The constructor can be passed an array of config values
80 */
81 function CI_Email($config = array())
82 {
83 if (count($config) > 0)
84 {
85 $this->initialize($config);
86 }
87
88 log_message('debug', "Email Class Initialized");
89 }
admine334c472006-10-21 19:44:22 +000090
adminb0dd10f2006-08-25 17:25:49 +000091 // --------------------------------------------------------------------
92
93 /**
94 * Initialize preferences
95 *
96 * @access public
97 * @param array
98 * @return void
99 */
100 function initialize($config = array())
101 {
102 $this->clear();
103 foreach ($config as $key => $val)
104 {
105 if (isset($this->$key))
106 {
107 $method = 'set_'.$key;
108
109 if (method_exists($this, $method))
110 {
111 $this->$method($val);
112 }
113 else
114 {
115 $this->$key = $val;
116 }
117 }
118 }
admine334c472006-10-21 19:44:22 +0000119 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000120 $this->_safe_mode = (@ini_get("safe_mode") == 0) ? FALSE : TRUE;
121 }
adminb0dd10f2006-08-25 17:25:49 +0000122
123 // --------------------------------------------------------------------
124
125 /**
126 * Initialize the Email Data
127 *
128 * @access public
129 * @return void
130 */
admind16d6c22006-09-21 00:45:12 +0000131 function clear($clear_attachments = FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000132 {
133 $this->_subject = "";
134 $this->_body = "";
135 $this->_finalbody = "";
136 $this->_header_str = "";
137 $this->_replyto_flag = FALSE;
138 $this->_recipients = array();
139 $this->_headers = array();
140 $this->_debug_msg = array();
141
142 $this->_set_header('User-Agent', $this->useragent);
143 $this->_set_header('Date', $this->_set_date());
admind16d6c22006-09-21 00:45:12 +0000144
admine334c472006-10-21 19:44:22 +0000145 if ($clear_attachments !== FALSE)
146 {
147 $this->_attach_name = array();
148 $this->_attach_type = array();
149 $this->_attach_disp = array();
150 }
adminb0dd10f2006-08-25 17:25:49 +0000151 }
adminb0dd10f2006-08-25 17:25:49 +0000152
153 // --------------------------------------------------------------------
154
155 /**
156 * Set FROM
157 *
158 * @access public
159 * @param string
160 * @param string
161 * @return void
162 */
163 function from($from, $name = '')
164 {
165 if (preg_match( '/\<(.*)\>/', $from, $match))
166 $from = $match['1'];
167
168 if ($this->validate)
169 $this->validate_email($this->_str_to_array($from));
170
171 if ($name != '' && substr($name, 0, 1) != '"')
172 {
173 $name = '"'.$name.'"';
174 }
175
176 $this->_set_header('From', $name.' <'.$from.'>');
177 $this->_set_header('Return-Path', '<'.$from.'>');
178 }
adminb0dd10f2006-08-25 17:25:49 +0000179
180 // --------------------------------------------------------------------
181
182 /**
183 * Set Reply-to
184 *
185 * @access public
186 * @param string
187 * @param string
188 * @return void
189 */
190 function reply_to($replyto, $name = '')
191 {
192 if (preg_match( '/\<(.*)\>/', $replyto, $match))
193 $replyto = $match['1'];
194
195 if ($this->validate)
196 $this->validate_email($this->_str_to_array($replyto));
197
198 if ($name == '')
199 {
200 $name = $replyto;
201 }
202
203 if (substr($name, 0, 1) != '"')
204 {
205 $name = '"'.$name.'"';
206 }
207
208 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
209 $this->_replyto_flag = TRUE;
210 }
adminb0dd10f2006-08-25 17:25:49 +0000211
212 // --------------------------------------------------------------------
213
214 /**
215 * Set Recipients
216 *
217 * @access public
218 * @param string
219 * @return void
220 */
221 function to($to)
222 {
223 $to = $this->_str_to_array($to);
224 $to = $this->clean_email($to);
225
226 if ($this->validate)
227 $this->validate_email($to);
228
229 if ($this->_get_protocol() != 'mail')
230 $this->_set_header('To', implode(", ", $to));
231
232 switch ($this->_get_protocol())
233 {
234 case 'smtp' : $this->_recipients = $to;
235 break;
236 case 'sendmail' : $this->_recipients = implode(", ", $to);
237 break;
238 case 'mail' : $this->_recipients = implode(", ", $to);
239 break;
240 }
241 }
adminb0dd10f2006-08-25 17:25:49 +0000242
243 // --------------------------------------------------------------------
244
245 /**
246 * Set CC
247 *
248 * @access public
249 * @param string
250 * @return void
251 */
252 function cc($cc)
253 {
254 $cc = $this->_str_to_array($cc);
255 $cc = $this->clean_email($cc);
256
257 if ($this->validate)
258 $this->validate_email($cc);
259
260 $this->_set_header('Cc', implode(", ", $cc));
261
262 if ($this->_get_protocol() == "smtp")
263 $this->_cc_array = $cc;
264 }
adminb0dd10f2006-08-25 17:25:49 +0000265
266 // --------------------------------------------------------------------
267
268 /**
269 * Set BCC
270 *
271 * @access public
272 * @param string
273 * @param string
274 * @return void
275 */
276 function bcc($bcc, $limit = '')
277 {
admin1cf89aa2006-09-03 18:24:39 +0000278 if ($limit != '' && is_numeric($limit))
adminb0dd10f2006-08-25 17:25:49 +0000279 {
280 $this->bcc_batch_mode = true;
281 $this->bcc_batch_size = $limit;
282 }
283
284 $bcc = $this->_str_to_array($bcc);
285 $bcc = $this->clean_email($bcc);
286
287 if ($this->validate)
288 $this->validate_email($bcc);
289
290 if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
291 $this->_bcc_array = $bcc;
292 else
293 $this->_set_header('Bcc', implode(", ", $bcc));
294 }
adminb0dd10f2006-08-25 17:25:49 +0000295
296 // --------------------------------------------------------------------
297
298 /**
299 * Set Email Subject
300 *
301 * @access public
302 * @param string
303 * @return void
304 */
305 function subject($subject)
306 {
307 $subject = preg_replace("/(\r\n)|(\r)|(\n)/", "", $subject);
308 $subject = preg_replace("/(\t)/", " ", $subject);
309
310 $this->_set_header('Subject', trim($subject));
311 }
adminb0dd10f2006-08-25 17:25:49 +0000312
313 // --------------------------------------------------------------------
314
315 /**
316 * Set Body
317 *
318 * @access public
319 * @param string
320 * @return void
321 */
322 function message($body)
323 {
admin3f643e62006-10-27 06:25:31 +0000324 $this->_body = stripslashes(rtrim(str_replace("\r", "", $body)));
adminb0dd10f2006-08-25 17:25:49 +0000325 }
adminb0dd10f2006-08-25 17:25:49 +0000326
327 // --------------------------------------------------------------------
328
329 /**
330 * Assign file attachments
331 *
332 * @access public
333 * @param string
334 * @return string
335 */
336 function attach($filename, $disposition = 'attachment')
337 {
338 $this->_attach_name[] = $filename;
339 $this->_attach_type[] = $this->_mime_types(next(explode('.', basename($filename))));
admine334c472006-10-21 19:44:22 +0000340 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
adminb0dd10f2006-08-25 17:25:49 +0000341 }
admine334c472006-10-21 19:44:22 +0000342
adminb0dd10f2006-08-25 17:25:49 +0000343 // --------------------------------------------------------------------
344
345 /**
346 * Add a Header Item
347 *
348 * @access public
349 * @param string
350 * @param string
351 * @return void
352 */
353 function _set_header($header, $value)
354 {
355 $this->_headers[$header] = $value;
356 }
adminb0dd10f2006-08-25 17:25:49 +0000357
358 // --------------------------------------------------------------------
359
360 /**
361 * Convert a String to an Array
362 *
363 * @access public
364 * @param string
365 * @return array
366 */
367 function _str_to_array($email)
368 {
369 if ( ! is_array($email))
370 {
371 if (ereg(',$', $email))
372 $email = substr($email, 0, -1);
373
374 if (ereg('^,', $email))
375 $email = substr($email, 1);
376
377 if (ereg(',', $email))
378 {
379 $x = explode(',', $email);
380 $email = array();
381
382 for ($i = 0; $i < count($x); $i ++)
383 $email[] = trim($x[$i]);
384 }
385 else
386 {
387 $email = trim($email);
388 settype($email, "array");
389 }
390 }
391 return $email;
392 }
adminb0dd10f2006-08-25 17:25:49 +0000393
394 // --------------------------------------------------------------------
395
396 /**
397 * Set Multipart Value
398 *
399 * @access public
400 * @param string
401 * @return void
402 */
403 function set_alt_message($str = '')
404 {
405 $this->alt_message = ($str == '') ? '' : $str;
406 }
adminb0dd10f2006-08-25 17:25:49 +0000407
408 // --------------------------------------------------------------------
409
410 /**
411 * Set Mailtype
412 *
413 * @access public
414 * @param string
415 * @return void
416 */
417 function set_mailtype($type = 'text')
418 {
419 $this->mailtype = ($type == 'html') ? 'html' : 'text';
420 }
adminb0dd10f2006-08-25 17:25:49 +0000421
422 // --------------------------------------------------------------------
423
424 /**
425 * Set Wordwrap
426 *
427 * @access public
428 * @param string
429 * @return void
430 */
431 function set_wordwrap($wordwrap = TRUE)
432 {
433 $this->wordwrap = ($wordwrap === FALSE) ? FALSE : TRUE;
434 }
adminb0dd10f2006-08-25 17:25:49 +0000435
436 // --------------------------------------------------------------------
437
438 /**
admin23db0dd2006-10-21 19:16:50 +0000439 * Set Protocol
adminb0dd10f2006-08-25 17:25:49 +0000440 *
441 * @access public
442 * @param string
443 * @return void
444 */
445 function set_protocol($protocol = 'mail')
admine334c472006-10-21 19:44:22 +0000446 {
adminee54c112006-09-28 17:13:38 +0000447 $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
adminb0dd10f2006-08-25 17:25:49 +0000448 }
adminb0dd10f2006-08-25 17:25:49 +0000449
450 // --------------------------------------------------------------------
451
452 /**
453 * Set Priority
454 *
455 * @access public
456 * @param integer
457 * @return void
458 */
459 function set_priority($n = 3)
460 {
admin1cf89aa2006-09-03 18:24:39 +0000461 if ( ! is_numeric($n))
adminb0dd10f2006-08-25 17:25:49 +0000462 {
463 $this->priority = 3;
464 return;
465 }
466
467 if ($n < 1 OR $n > 5)
468 {
469 $this->priority = 3;
470 return;
471 }
472
473 $this->priority = $n;
474 }
adminb0dd10f2006-08-25 17:25:49 +0000475
476 // --------------------------------------------------------------------
477
478 /**
479 * Set Newline Character
480 *
481 * @access public
482 * @param string
483 * @return void
484 */
485 function set_newline($newline = "\n")
486 {
487 if ($newline != "\n" OR $newline != "\r\n" OR $newline != "\r")
488 {
489 $this->newline = "\n";
490 return;
491 }
492
493 $this->newline = $newline;
494 }
adminb0dd10f2006-08-25 17:25:49 +0000495
496 // --------------------------------------------------------------------
497
498 /**
admin23db0dd2006-10-21 19:16:50 +0000499 * Set Message Boundary
adminb0dd10f2006-08-25 17:25:49 +0000500 *
501 * @access private
502 * @return void
503 */
504 function _set_boundaries()
505 {
admin23db0dd2006-10-21 19:16:50 +0000506 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
adminb0dd10f2006-08-25 17:25:49 +0000507 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
508 }
adminb0dd10f2006-08-25 17:25:49 +0000509
510 // --------------------------------------------------------------------
511
512 /**
513 * Get the Message ID
514 *
515 * @access private
516 * @return string
517 */
518 function _get_message_id()
519 {
520 $from = $this->_headers['Return-Path'];
521 $from = str_replace(">", "", $from);
522 $from = str_replace("<", "", $from);
523
admine334c472006-10-21 19:44:22 +0000524 return "<".uniqid('').strstr($from, '@').">";
adminb0dd10f2006-08-25 17:25:49 +0000525 }
adminb0dd10f2006-08-25 17:25:49 +0000526
527 // --------------------------------------------------------------------
528
529 /**
530 * Get Mail Protocol
531 *
532 * @access private
533 * @param bool
534 * @return string
535 */
536 function _get_protocol($return = true)
537 {
538 $this->protocol = strtolower($this->protocol);
adminee54c112006-09-28 17:13:38 +0000539 $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
adminb0dd10f2006-08-25 17:25:49 +0000540
admine334c472006-10-21 19:44:22 +0000541 if ($return == true)
adminb0dd10f2006-08-25 17:25:49 +0000542 return $this->protocol;
543 }
adminb0dd10f2006-08-25 17:25:49 +0000544
545 // --------------------------------------------------------------------
546
547 /**
548 * Get Mail Encoding
549 *
550 * @access private
551 * @param bool
552 * @return string
553 */
554 function _get_encoding($return = true)
555 {
556 $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '7bit' : $this->_encoding;
557
admine334c472006-10-21 19:44:22 +0000558 if ( ! in_array($this->charset, $this->_base_charsets, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000559 $this->_encoding = "8bit";
560
admine334c472006-10-21 19:44:22 +0000561 if ($return == true)
adminb0dd10f2006-08-25 17:25:49 +0000562 return $this->_encoding;
563 }
admine334c472006-10-21 19:44:22 +0000564
adminb0dd10f2006-08-25 17:25:49 +0000565 // --------------------------------------------------------------------
566
567 /**
568 * Get content type (text/html/attachment)
569 *
570 * @access private
571 * @return string
572 */
573 function _get_content_type()
574 {
575 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
576 return 'html';
577
578 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
579 return 'html-attach';
580
581 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
582 return 'plain-attach';
583
584 else return 'plain';
585 }
adminb0dd10f2006-08-25 17:25:49 +0000586
587 // --------------------------------------------------------------------
588
589 /**
590 * Set RFC 822 Date
591 *
592 * @access public
593 * @return string
594 */
595 function _set_date()
596 {
597 $timezone = date("Z");
598 $operator = (substr($timezone, 0, 1) == '-') ? '-' : '+';
599 $timezone = abs($timezone);
600 $timezone = ($timezone/3600) * 100 + ($timezone % 3600) /60;
601
602 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
603 }
adminb0dd10f2006-08-25 17:25:49 +0000604
605 // --------------------------------------------------------------------
606
607 /**
608 * Mime message
609 *
610 * @access private
611 * @return string
612 */
613 function _get_mime_message()
614 {
615 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
616 }
adminb0dd10f2006-08-25 17:25:49 +0000617
618 // --------------------------------------------------------------------
619
620 /**
621 * Validate Email Address
622 *
623 * @access public
624 * @param string
625 * @return bool
626 */
627 function validate_email($email)
628 {
629 if ( ! is_array($email))
630 {
631 $this->_set_error_message('email_must_be_array');
632 return FALSE;
633 }
634
635 foreach ($email as $val)
636 {
admine334c472006-10-21 19:44:22 +0000637 if ( ! $this->valid_email($val))
adminb0dd10f2006-08-25 17:25:49 +0000638 {
639 $this->_set_error_message('email_invalid_address', $val);
640 return FALSE;
641 }
642 }
643 }
adminb0dd10f2006-08-25 17:25:49 +0000644
645 // --------------------------------------------------------------------
646
647 /**
648 * Email Validation
649 *
650 * @access public
651 * @param string
652 * @return bool
653 */
654 function valid_email($address)
655 {
656 if ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address))
657 return FALSE;
admine334c472006-10-21 19:44:22 +0000658 else
adminb0dd10f2006-08-25 17:25:49 +0000659 return TRUE;
660 }
adminb0dd10f2006-08-25 17:25:49 +0000661
662 // --------------------------------------------------------------------
663
664 /**
665 * Clean Extended Email Address: Joe Smith <joe@smith.com>
666 *
667 * @access public
668 * @param string
669 * @return string
670 */
671 function clean_email($email)
672 {
673 if ( ! is_array($email))
674 {
675 if (preg_match('/\<(.*)\>/', $email, $match))
admine334c472006-10-21 19:44:22 +0000676 return $match['1'];
677 else
678 return $email;
adminb0dd10f2006-08-25 17:25:49 +0000679 }
680
681 $clean_email = array();
682
admine334c472006-10-21 19:44:22 +0000683 for ($i=0; $i < count($email); $i++)
adminb0dd10f2006-08-25 17:25:49 +0000684 {
685 if (preg_match( '/\<(.*)\>/', $email[$i], $match))
admine334c472006-10-21 19:44:22 +0000686 $clean_email[] = $match['1'];
687 else
688 $clean_email[] = $email[$i];
adminb0dd10f2006-08-25 17:25:49 +0000689 }
690
691 return $clean_email;
692 }
adminb0dd10f2006-08-25 17:25:49 +0000693
694 // --------------------------------------------------------------------
695
696 /**
697 * Build alternative plain text message
698 *
699 * This function provides the raw message for use
700 * in plain-text headers of HTML-formatted emails.
admine334c472006-10-21 19:44:22 +0000701 * If the user hasn't specified his own alternative message
adminb0dd10f2006-08-25 17:25:49 +0000702 * it creates one by stripping the HTML
703 *
704 * @access private
705 * @return string
706 */
707 function _get_alt_message()
708 {
admind16d6c22006-09-21 00:45:12 +0000709 if ($this->alt_message != "")
710 {
711 return $this->word_wrap($this->alt_message, '76');
712 }
713
adminb0dd10f2006-08-25 17:25:49 +0000714 if (eregi( '\<body(.*)\</body\>', $this->_body, $match))
715 {
716 $body = $match['1'];
717 $body = substr($body, strpos($body, ">") + 1);
718 }
719 else
720 {
721 $body = $this->_body;
722 }
723
724 $body = trim(strip_tags($body));
725 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
726 $body = str_replace("\t", "", $body);
727
728 for ($i = 20; $i >= 3; $i--)
729 {
730 $n = "";
731
732 for ($x = 1; $x <= $i; $x ++)
733 $n .= "\n";
734
735 $body = str_replace($n, "\n\n", $body);
736 }
737
738 return $this->word_wrap($body, '76');
739 }
adminb0dd10f2006-08-25 17:25:49 +0000740
741 // --------------------------------------------------------------------
742
743 /**
744 * Word Wrap
745 *
746 * @access public
747 * @param string
748 * @param integer
749 * @return string
750 */
751 function word_wrap($str, $chars = '')
752 {
753 if ($chars == '')
adminb0dd10f2006-08-25 17:25:49 +0000754 {
admin3f643e62006-10-27 06:25:31 +0000755 $chars = ($this->wrapchars == "") ? "76" : $this->wrapchars;
756 }
757
758 $str = preg_replace("| +|", " ", $str);
759
760 $str = preg_replace("|(\[url.+\])|", "{unwrap}\\1{/unwrap}", $str);
761
762 $output = "";
763 foreach (split("\n", $str) as $current_line)
764 {
765 if (strlen($current_line) > $chars)
adminb0dd10f2006-08-25 17:25:49 +0000766 {
767 $line = "";
admin3f643e62006-10-27 06:25:31 +0000768
769 foreach (split(" ", $current_line) as $words)
adminb0dd10f2006-08-25 17:25:49 +0000770 {
admin3f643e62006-10-27 06:25:31 +0000771 while((strlen($words)) > $chars)
adminb0dd10f2006-08-25 17:25:49 +0000772 {
admin3f643e62006-10-27 06:25:31 +0000773 if (stristr($words, '{unwrap}') !== FALSE OR stristr($words, '{/unwrap}') !== FALSE)
adminb0dd10f2006-08-25 17:25:49 +0000774 {
775 break;
776 }
admin3f643e62006-10-27 06:25:31 +0000777
778 $output .= substr($words, 0, $chars-1);
779 $words = substr($words, $chars-1);
780
781 $output .= $this->newline;
adminb0dd10f2006-08-25 17:25:49 +0000782 }
783
admin3f643e62006-10-27 06:25:31 +0000784 if ((strlen($line) + strlen($words)) > $chars)
adminb0dd10f2006-08-25 17:25:49 +0000785 {
admin3f643e62006-10-27 06:25:31 +0000786 $output .= $line.$this->newline;
adminb0dd10f2006-08-25 17:25:49 +0000787
admin3f643e62006-10-27 06:25:31 +0000788 $line = $words." ";
789 }
790 else
adminb0dd10f2006-08-25 17:25:49 +0000791 {
admin3f643e62006-10-27 06:25:31 +0000792 $line .= $words." ";
adminb0dd10f2006-08-25 17:25:49 +0000793 }
794 }
795
admin3f643e62006-10-27 06:25:31 +0000796 $output .= $line.$this->newline;
797 }
798 else
adminb0dd10f2006-08-25 17:25:49 +0000799 {
admin3f643e62006-10-27 06:25:31 +0000800 $output .= $current_line.$this->newline;
adminb0dd10f2006-08-25 17:25:49 +0000801 }
802 }
803
804 return $output;
805 }
adminb0dd10f2006-08-25 17:25:49 +0000806
807 // --------------------------------------------------------------------
808
809 /**
810 * Build final headers
811 *
812 * @access public
813 * @param string
814 * @return string
815 */
816 function _build_headers()
817 {
818 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
819 $this->_set_header('X-Mailer', $this->useragent);
820 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
821 $this->_set_header('Message-ID', $this->_get_message_id());
822 $this->_set_header('Mime-Version', '1.0');
823 }
adminb0dd10f2006-08-25 17:25:49 +0000824
825 // --------------------------------------------------------------------
826
827 /**
828 * Write Headers as a string
829 *
830 * @access public
831 * @return void
832 */
833 function _write_headers()
834 {
835 if ($this->protocol == 'mail')
836 {
837 $this->_subject = $this->_headers['Subject'];
838 unset($this->_headers['Subject']);
839 }
840
841 reset($this->_headers);
842 $this->_header_str = "";
843
admine334c472006-10-21 19:44:22 +0000844 foreach($this->_headers as $key => $val)
adminb0dd10f2006-08-25 17:25:49 +0000845 {
846 $val = trim($val);
847
848 if ($val != "")
849 {
850 $this->_header_str .= $key.": ".$val.$this->newline;
851 }
852 }
853
854 if ($this->_get_protocol() == 'mail')
855 $this->_header_str = substr($this->_header_str, 0, -1);
856 }
adminb0dd10f2006-08-25 17:25:49 +0000857
858 // --------------------------------------------------------------------
859
860 /**
861 * Build Final Body and attachments
862 *
863 * @access public
864 * @return void
865 */
866 function _build_message()
867 {
admin3f643e62006-10-27 06:25:31 +0000868 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
869 {
870 $this->_body = $this->word_wrap($this->_body);
871 }
872
adminb0dd10f2006-08-25 17:25:49 +0000873 $this->_set_boundaries();
874 $this->_write_headers();
875
876 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
877
878 switch ($this->_get_content_type())
879 {
880 case 'plain' :
881
882 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
883 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
884
885 if ($this->_get_protocol() == 'mail')
886 {
887 $this->_header_str .= $hdr;
888 $this->_finalbody = $this->_body;
889
890 return;
891 }
892
893 $hdr .= $this->newline . $this->newline . $this->_body;
894
895 $this->_finalbody = $hdr;
896 return;
897
898 break;
899 case 'html' :
900
901 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline;
902 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
903 $hdr .= "--" . $this->_alt_boundary . $this->newline;
904
905 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
906 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
907 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
908
909 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
910 $hdr .= "Content-Transfer-Encoding: quoted/printable";
911
912 if ($this->_get_protocol() == 'mail')
913 {
914 $this->_header_str .= $hdr;
915 $this->_finalbody = $this->_body . $this->newline . $this->newline . "--" . $this->_alt_boundary . "--";
916
917 return;
918 }
919
920 $hdr .= $this->newline . $this->newline;
921 $hdr .= $this->_body . $this->newline . $this->newline . "--" . $this->_alt_boundary . "--";
922
923 $this->_finalbody = $hdr;
924 return;
925
926 break;
927 case 'plain-attach' :
928
929 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
930 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
931 $hdr .= "--" . $this->_atc_boundary . $this->newline;
932
933 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
934 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
935
936 if ($this->_get_protocol() == 'mail')
937 {
938 $this->_header_str .= $hdr;
939
940 $body = $this->_body . $this->newline . $this->newline;
941 }
942
943 $hdr .= $this->newline . $this->newline;
944 $hdr .= $this->_body . $this->newline . $this->newline;
945
946 break;
947 case 'html-attach' :
948
949 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
950 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
951 $hdr .= "--" . $this->_atc_boundary . $this->newline;
952
953 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
954 $hdr .= "--" . $this->_alt_boundary . $this->newline;
955
956 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
957 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
958 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
959
960 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
961 $hdr .= "Content-Transfer-Encoding: quoted/printable";
962
963 if ($this->_get_protocol() == 'mail')
964 {
965 $this->_header_str .= $hdr;
966
admine334c472006-10-21 19:44:22 +0000967 $body = $this->_body . $this->newline . $this->newline;
adminb0dd10f2006-08-25 17:25:49 +0000968 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
969 }
970
971 $hdr .= $this->newline . $this->newline;
972 $hdr .= $this->_body . $this->newline . $this->newline;
973 $hdr .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
974
975 break;
976 }
977
978 $attachment = array();
979
980 $z = 0;
981
982 for ($i=0; $i < count($this->_attach_name); $i++)
983 {
984 $filename = $this->_attach_name[$i];
985 $basename = basename($filename);
986 $ctype = $this->_attach_type[$i];
987
988 if ( ! file_exists($filename))
989 {
admine334c472006-10-21 19:44:22 +0000990 $this->_set_error_message('email_attachment_missing', $filename);
adminb0dd10f2006-08-25 17:25:49 +0000991 return FALSE;
992 }
993
994 $h = "--".$this->_atc_boundary.$this->newline;
995 $h .= "Content-type: ".$ctype."; ";
996 $h .= "name=\"".$basename."\"".$this->newline;
997 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
998 $h .= "Content-Transfer-Encoding: base64".$this->newline;
999
1000 $attachment[$z++] = $h;
1001 $file = filesize($filename) +1;
1002
1003 if ( ! $fp = fopen($filename, 'r'))
1004 {
admine334c472006-10-21 19:44:22 +00001005 $this->_set_error_message('email_attachment_unredable', $filename);
adminb0dd10f2006-08-25 17:25:49 +00001006 return FALSE;
1007 }
1008
1009 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1010 fclose($fp);
1011 }
1012
1013 if ($this->_get_protocol() == 'mail')
1014 {
1015 $this->_finalbody = $body . implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
1016
1017 return;
1018 }
1019
1020 $this->_finalbody = $hdr.implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
1021
1022 return;
1023 }
adminb0dd10f2006-08-25 17:25:49 +00001024
1025 // --------------------------------------------------------------------
1026
1027 /**
1028 * Send Email
1029 *
1030 * @access public
1031 * @return bool
1032 */
1033 function send()
1034 {
1035 if ($this->_replyto_flag == FALSE)
1036 {
1037 $this->reply_to($this->_headers['From']);
1038 }
1039
1040 if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
1041 ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
1042 ( ! isset($this->_headers['Cc'])))
1043 {
1044 $this->_set_error_message('email_no_recipients');
1045 return FALSE;
1046 }
1047
1048 $this->_build_headers();
1049
1050 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
1051 {
1052 if (count($this->_bcc_array) > $this->bcc_batch_size)
1053 return $this->batch_bcc_send();
1054 }
1055
1056 $this->_build_message();
1057
1058 if ( ! $this->_spool_email())
1059 return FALSE;
1060 else
1061 return TRUE;
1062 }
adminb0dd10f2006-08-25 17:25:49 +00001063
1064 // --------------------------------------------------------------------
1065
1066 /**
admin23db0dd2006-10-21 19:16:50 +00001067 * Batch Bcc Send. Sends groups of BCCs in batches
adminb0dd10f2006-08-25 17:25:49 +00001068 *
1069 * @access public
1070 * @return bool
1071 */
1072 function batch_bcc_send()
1073 {
1074 $float = $this->bcc_batch_size -1;
1075
1076 $flag = 0;
1077 $set = "";
1078
1079 $chunk = array();
1080
1081 for ($i = 0; $i < count($this->_bcc_array); $i++)
1082 {
1083 if (isset($this->_bcc_array[$i]))
1084 $set .= ", ".$this->_bcc_array[$i];
1085
1086 if ($i == $float)
1087 {
1088 $chunk[] = substr($set, 1);
1089 $float = $float + $this->bcc_batch_size;
1090 $set = "";
1091 }
1092
1093 if ($i == count($this->_bcc_array)-1)
1094 $chunk[] = substr($set, 1);
1095 }
1096
1097 for ($i = 0; $i < count($chunk); $i++)
1098 {
1099 unset($this->_headers['Bcc']);
1100 unset($bcc);
1101
1102 $bcc = $this->_str_to_array($chunk[$i]);
1103 $bcc = $this->clean_email($bcc);
1104
1105 if ($this->protocol != 'smtp')
1106 $this->_set_header('Bcc', implode(", ", $bcc));
1107 else
1108 $this->_bcc_array = $bcc;
1109
1110 $this->_build_message();
1111 $this->_spool_email();
1112 }
1113 }
adminb0dd10f2006-08-25 17:25:49 +00001114
1115 // --------------------------------------------------------------------
1116
1117 /**
1118 * Unwrap special elements
1119 *
1120 * @access private
1121 * @return void
1122 */
admine334c472006-10-21 19:44:22 +00001123 function _unwrap_specials()
1124 {
1125 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1126 }
adminb0dd10f2006-08-25 17:25:49 +00001127
1128 // --------------------------------------------------------------------
1129
1130 /**
1131 * Strip line-breaks via callback
1132 *
1133 * @access private
1134 * @return string
1135 */
admine334c472006-10-21 19:44:22 +00001136 function _remove_nl_callback($matches)
1137 {
1138 return preg_replace("/(\r\n)|(\r)|(\n)/", "", $matches['1']);
1139 }
adminb0dd10f2006-08-25 17:25:49 +00001140
1141 // --------------------------------------------------------------------
1142
1143 /**
1144 * Spool mail to the mail server
1145 *
1146 * @access private
admin23db0dd2006-10-21 19:16:50 +00001147 * @return bool
adminb0dd10f2006-08-25 17:25:49 +00001148 */
1149 function _spool_email()
1150 {
admine334c472006-10-21 19:44:22 +00001151 $this->_unwrap_specials();
adminb0dd10f2006-08-25 17:25:49 +00001152
1153 switch ($this->_get_protocol())
1154 {
1155 case 'mail' :
1156
1157 if ( ! $this->_send_with_mail())
1158 {
1159 $this->_set_error_message('email_send_failure_phpmail');
1160 return FALSE;
1161 }
1162 break;
admine334c472006-10-21 19:44:22 +00001163 case 'sendmail' :
adminb0dd10f2006-08-25 17:25:49 +00001164
1165 if ( ! $this->_send_with_sendmail())
1166 {
1167 $this->_set_error_message('email_send_failure_sendmail');
1168 return FALSE;
1169 }
1170 break;
admine334c472006-10-21 19:44:22 +00001171 case 'smtp' :
adminb0dd10f2006-08-25 17:25:49 +00001172
1173 if ( ! $this->_send_with_smtp())
1174 {
1175 $this->_set_error_message('email_send_failure_smtp');
1176 return FALSE;
1177 }
1178 break;
1179
1180 }
1181
1182 $this->_set_error_message('email_sent', $this->_get_protocol());
1183 return true;
1184 }
adminb0dd10f2006-08-25 17:25:49 +00001185
1186 // --------------------------------------------------------------------
1187
1188 /**
1189 * Send using mail()
1190 *
1191 * @access private
1192 * @return bool
1193 */
1194 function _send_with_mail()
1195 {
1196 if ($this->_safe_mode == TRUE)
1197 {
1198 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
1199 return FALSE;
1200 else
1201 return TRUE;
1202 }
1203 else
1204 {
1205 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f".$this->clean_email($this->_headers['From'])))
1206 return FALSE;
1207 else
1208 return TRUE;
1209 }
1210 }
adminb0dd10f2006-08-25 17:25:49 +00001211
1212 // --------------------------------------------------------------------
1213
1214 /**
1215 * Send using Sendmail
1216 *
1217 * @access private
1218 * @return bool
1219 */
1220 function _send_with_sendmail()
1221 {
1222 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1223
1224 if ( ! is_resource($fp))
1225 {
1226 $this->_set_error_message('email_no_socket');
1227 return FALSE;
1228 }
1229
1230 fputs($fp, $this->_header_str);
1231 fputs($fp, $this->_finalbody);
1232 pclose($fp) >> 8 & 0xFF;
1233
1234 return TRUE;
1235 }
adminb0dd10f2006-08-25 17:25:49 +00001236
1237 // --------------------------------------------------------------------
1238
1239 /**
1240 * Send using SMTP
1241 *
1242 * @access private
1243 * @return bool
1244 */
1245 function _send_with_smtp()
1246 {
admine334c472006-10-21 19:44:22 +00001247 if ($this->smtp_host == '')
1248 {
adminb0dd10f2006-08-25 17:25:49 +00001249 $this->_set_error_message('email_no_hostname');
1250 return FALSE;
1251 }
1252
1253 $this->_smtp_connect();
1254 $this->_smtp_authenticate();
1255
1256 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1257
1258 foreach($this->_recipients as $val)
1259 $this->_send_command('to', $val);
1260
1261 if (count($this->_cc_array) > 0)
1262 {
1263 foreach($this->_cc_array as $val)
1264 {
1265 if ($val != "")
1266 $this->_send_command('to', $val);
1267 }
1268 }
1269
1270 if (count($this->_bcc_array) > 0)
1271 {
1272 foreach($this->_bcc_array as $val)
1273 {
1274 if ($val != "")
1275 $this->_send_command('to', $val);
1276 }
1277 }
1278
1279 $this->_send_command('data');
1280
1281 $this->_send_data($this->_header_str . $this->_finalbody);
1282
1283 $this->_send_data('.');
1284
1285 $reply = $this->_get_smtp_data();
1286
1287 $this->_set_error_message($reply);
1288
1289 if (substr($reply, 0, 3) != '250')
1290 {
1291 $this->_set_error_message('email_smtp_error', $reply);
1292 return FALSE;
1293 }
1294
1295 $this->_send_command('quit');
1296 return true;
1297 }
adminb0dd10f2006-08-25 17:25:49 +00001298
1299 // --------------------------------------------------------------------
1300
1301 /**
1302 * SMTP Connect
1303 *
1304 * @access public
1305 * @param string
1306 * @return string
1307 */
1308 function _smtp_connect()
1309 {
1310
admine334c472006-10-21 19:44:22 +00001311 $this->_smtp_connect = fsockopen($this->smtp_host,
adminb0dd10f2006-08-25 17:25:49 +00001312 $this->smtp_port,
admine334c472006-10-21 19:44:22 +00001313 $errno,
1314 $errstr,
adminb0dd10f2006-08-25 17:25:49 +00001315 $this->smtp_timeout);
1316
1317 if( ! is_resource($this->_smtp_connect))
1318 {
1319 $this->_set_error_message('email_smtp_error', $errno." ".$errstr);
1320 return FALSE;
1321 }
1322
1323 $this->_set_error_message($this->_get_smtp_data());
1324 return $this->_send_command('hello');
1325 }
adminb0dd10f2006-08-25 17:25:49 +00001326
1327 // --------------------------------------------------------------------
1328
1329 /**
1330 * Send SMTP command
1331 *
1332 * @access private
1333 * @param string
1334 * @param string
1335 * @return string
1336 */
1337 function _send_command($cmd, $data = '')
1338 {
1339 switch ($cmd)
1340 {
1341 case 'hello' :
1342
1343 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1344 $this->_send_data('EHLO '.$this->_get_hostname());
1345 else
1346 $this->_send_data('HELO '.$this->_get_hostname());
1347
1348 $resp = 250;
1349 break;
1350 case 'from' :
1351
1352 $this->_send_data('MAIL FROM:<'.$data.'>');
1353
1354 $resp = 250;
1355 break;
1356 case 'to' :
1357
1358 $this->_send_data('RCPT TO:<'.$data.'>');
1359
1360 $resp = 250;
1361 break;
1362 case 'data' :
1363
1364 $this->_send_data('DATA');
1365
1366 $resp = 354;
1367 break;
1368 case 'quit' :
1369
1370 $this->_send_data('QUIT');
1371
1372 $resp = 221;
1373 break;
1374 }
1375
1376 $reply = $this->_get_smtp_data();
1377
1378 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1379
1380 if (substr($reply, 0, 3) != $resp)
1381 {
1382 $this->_set_error_message('email_smtp_error', $reply);
1383 return FALSE;
1384 }
1385
1386 if ($cmd == 'quit')
1387 fclose($this->_smtp_connect);
1388
1389 return true;
1390 }
adminb0dd10f2006-08-25 17:25:49 +00001391
1392 // --------------------------------------------------------------------
1393
1394 /**
1395 * SMTP Authenticate
1396 *
1397 * @access private
1398 * @return bool
1399 */
1400 function _smtp_authenticate()
1401 {
1402 if ( ! $this->_smtp_auth)
1403 return true;
1404
1405 if ($this->smtp_user == "" AND $this->smtp_pass == "")
1406 {
1407 $this->_set_error_message('email_no_smtp_unpw');
1408 return FALSE;
1409 }
1410
1411 $this->_send_data('AUTH LOGIN');
1412
1413 $reply = $this->_get_smtp_data();
1414
1415 if (substr($reply, 0, 3) != '334')
1416 {
1417 $this->_set_error_message('email_filed_smtp_login', $reply);
1418 return FALSE;
1419 }
1420
1421 $this->_send_data(base64_encode($this->smtp_user));
1422
1423 $reply = $this->_get_smtp_data();
1424
1425 if (substr($reply, 0, 3) != '334')
1426 {
1427 $this->_set_error_message('email_smtp_auth_un', $reply);
1428 return FALSE;
1429 }
1430
1431 $this->_send_data(base64_encode($this->smtp_pass));
1432
1433 $reply = $this->_get_smtp_data();
1434
1435 if (substr($reply, 0, 3) != '235')
1436 {
1437 $this->_set_error_message('email_smtp_auth_pw', $reply);
1438 return FALSE;
1439 }
1440
1441 return true;
1442 }
adminb0dd10f2006-08-25 17:25:49 +00001443
1444 // --------------------------------------------------------------------
1445
1446 /**
1447 * Send SMTP data
1448 *
1449 * @access private
1450 * @return bool
1451 */
1452 function _send_data($data)
1453 {
1454 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1455 {
1456 $this->_set_error_message('email_smtp_data_failure', $data);
1457 return FALSE;
1458 }
1459 else
1460 return true;
1461 }
adminb0dd10f2006-08-25 17:25:49 +00001462
1463 // --------------------------------------------------------------------
1464
1465 /**
1466 * Get SMTP data
1467 *
1468 * @access private
1469 * @return string
1470 */
1471 function _get_smtp_data()
1472 {
admine334c472006-10-21 19:44:22 +00001473 $data = "";
1474
1475 while ($str = fgets($this->_smtp_connect, 512))
1476 {
adminb0dd10f2006-08-25 17:25:49 +00001477 $data .= $str;
1478
1479 if (substr($str, 3, 1) == " ")
1480 break;
admine334c472006-10-21 19:44:22 +00001481 }
1482
1483 return $data;
adminb0dd10f2006-08-25 17:25:49 +00001484 }
adminb0dd10f2006-08-25 17:25:49 +00001485
1486 // --------------------------------------------------------------------
1487
1488 /**
1489 * Get Hostname
1490 *
1491 * @access private
1492 * @return string
1493 */
1494 function _get_hostname()
1495 {
derek243f5cc2006-10-03 00:25:03 +00001496 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
adminb0dd10f2006-08-25 17:25:49 +00001497 }
adminb0dd10f2006-08-25 17:25:49 +00001498
1499 // --------------------------------------------------------------------
1500
1501 /**
1502 * Get IP
1503 *
1504 * @access private
1505 * @return string
1506 */
1507 function _get_ip()
1508 {
1509 if ($this->_IP !== FALSE)
1510 {
1511 return $this->_IP;
1512 }
1513
1514 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1515 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1516 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1517
1518 if ($cip && $rip) $this->_IP = $cip;
1519 elseif ($rip) $this->_IP = $rip;
1520 elseif ($cip) $this->_IP = $cip;
1521 elseif ($fip) $this->_IP = $fip;
1522
1523 if (strstr($this->_IP, ','))
1524 {
1525 $x = explode(',', $this->_IP);
1526 $this->_IP = end($x);
1527 }
1528
1529 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
1530 $this->_IP = '0.0.0.0';
1531
1532 unset($cip);
1533 unset($rip);
1534 unset($fip);
1535
1536 return $this->_IP;
1537 }
adminb0dd10f2006-08-25 17:25:49 +00001538
1539 // --------------------------------------------------------------------
1540
1541 /**
admin23db0dd2006-10-21 19:16:50 +00001542 * Get Debug Message
adminb0dd10f2006-08-25 17:25:49 +00001543 *
1544 * @access public
1545 * @return string
1546 */
1547 function print_debugger()
1548 {
1549 $msg = '';
1550
1551 if (count($this->_debug_msg) > 0)
1552 {
1553 foreach ($this->_debug_msg as $val)
1554 {
1555 $msg .= $val;
1556 }
1557 }
1558
1559 $msg .= "<pre>".$this->_header_str."\n".$this->_subject."\n".$this->_finalbody.'</pre>';
1560 return $msg;
1561 }
adminb0dd10f2006-08-25 17:25:49 +00001562
1563 // --------------------------------------------------------------------
1564
1565 /**
1566 * Set Message
1567 *
1568 * @access public
1569 * @param string
1570 * @return string
1571 */
1572 function _set_error_message($msg, $val = '')
1573 {
admin88a8ad12006-10-07 03:16:32 +00001574 $CI =& get_instance();
1575 $CI->lang->load('email');
adminb0dd10f2006-08-25 17:25:49 +00001576
admin88a8ad12006-10-07 03:16:32 +00001577 if (FALSE === ($line = $CI->lang->line($msg)))
adminb0dd10f2006-08-25 17:25:49 +00001578 {
1579 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1580 }
1581 else
1582 {
1583 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1584 }
1585 }
adminb0dd10f2006-08-25 17:25:49 +00001586
1587 // --------------------------------------------------------------------
1588
1589 /**
1590 * Mime Types
1591 *
1592 * @access private
1593 * @param string
1594 * @return string
1595 */
1596 function _mime_types($ext = "")
1597 {
1598 $mimes = array( 'hqx' => 'application/mac-binhex40',
1599 'cpt' => 'application/mac-compactpro',
1600 'doc' => 'application/msword',
1601 'bin' => 'application/macbinary',
1602 'dms' => 'application/octet-stream',
1603 'lha' => 'application/octet-stream',
1604 'lzh' => 'application/octet-stream',
1605 'exe' => 'application/octet-stream',
1606 'class' => 'application/octet-stream',
1607 'psd' => 'application/octet-stream',
1608 'so' => 'application/octet-stream',
1609 'sea' => 'application/octet-stream',
1610 'dll' => 'application/octet-stream',
1611 'oda' => 'application/oda',
1612 'pdf' => 'application/pdf',
1613 'ai' => 'application/postscript',
1614 'eps' => 'application/postscript',
1615 'ps' => 'application/postscript',
1616 'smi' => 'application/smil',
1617 'smil' => 'application/smil',
1618 'mif' => 'application/vnd.mif',
1619 'xls' => 'application/vnd.ms-excel',
1620 'ppt' => 'application/vnd.ms-powerpoint',
1621 'wbxml' => 'application/vnd.wap.wbxml',
1622 'wmlc' => 'application/vnd.wap.wmlc',
1623 'dcr' => 'application/x-director',
1624 'dir' => 'application/x-director',
1625 'dxr' => 'application/x-director',
1626 'dvi' => 'application/x-dvi',
1627 'gtar' => 'application/x-gtar',
1628 'php' => 'application/x-httpd-php',
1629 'php4' => 'application/x-httpd-php',
1630 'php3' => 'application/x-httpd-php',
1631 'phtml' => 'application/x-httpd-php',
1632 'phps' => 'application/x-httpd-php-source',
1633 'js' => 'application/x-javascript',
1634 'swf' => 'application/x-shockwave-flash',
1635 'sit' => 'application/x-stuffit',
1636 'tar' => 'application/x-tar',
1637 'tgz' => 'application/x-tar',
1638 'xhtml' => 'application/xhtml+xml',
1639 'xht' => 'application/xhtml+xml',
1640 'zip' => 'application/zip',
1641 'mid' => 'audio/midi',
1642 'midi' => 'audio/midi',
1643 'mpga' => 'audio/mpeg',
1644 'mp2' => 'audio/mpeg',
1645 'mp3' => 'audio/mpeg',
1646 'aif' => 'audio/x-aiff',
1647 'aiff' => 'audio/x-aiff',
1648 'aifc' => 'audio/x-aiff',
1649 'ram' => 'audio/x-pn-realaudio',
1650 'rm' => 'audio/x-pn-realaudio',
1651 'rpm' => 'audio/x-pn-realaudio-plugin',
1652 'ra' => 'audio/x-realaudio',
1653 'rv' => 'video/vnd.rn-realvideo',
1654 'wav' => 'audio/x-wav',
1655 'bmp' => 'image/bmp',
1656 'gif' => 'image/gif',
1657 'jpeg' => 'image/jpeg',
1658 'jpg' => 'image/jpeg',
1659 'jpe' => 'image/jpeg',
1660 'png' => 'image/png',
1661 'tiff' => 'image/tiff',
1662 'tif' => 'image/tiff',
1663 'css' => 'text/css',
1664 'html' => 'text/html',
1665 'htm' => 'text/html',
1666 'shtml' => 'text/html',
1667 'txt' => 'text/plain',
1668 'text' => 'text/plain',
1669 'log' => 'text/plain',
1670 'rtx' => 'text/richtext',
1671 'rtf' => 'text/rtf',
1672 'xml' => 'text/xml',
1673 'xsl' => 'text/xml',
1674 'mpeg' => 'video/mpeg',
1675 'mpg' => 'video/mpeg',
1676 'mpe' => 'video/mpeg',
1677 'qt' => 'video/quicktime',
1678 'mov' => 'video/quicktime',
1679 'avi' => 'video/x-msvideo',
1680 'movie' => 'video/x-sgi-movie',
1681 'doc' => 'application/msword',
1682 'word' => 'application/msword',
1683 'xl' => 'application/excel',
1684 'eml' => 'message/rfc822'
1685 );
1686
1687 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
1688 }
adminb0dd10f2006-08-25 17:25:49 +00001689
1690}
1691// END CI_Email class
1692?>