blob: 92f2e73fa1faf2d5da6adef134642cd41b24c653 [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 {
324 $body = rtrim(str_replace("\r", "", $body));
325
326 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
327 $this->_body = $this->word_wrap($body);
328 else
329 $this->_body = $body;
330
331 $this->_body = stripslashes($this->_body);
332 }
adminb0dd10f2006-08-25 17:25:49 +0000333
334 // --------------------------------------------------------------------
335
336 /**
337 * Assign file attachments
338 *
339 * @access public
340 * @param string
341 * @return string
342 */
343 function attach($filename, $disposition = 'attachment')
344 {
345 $this->_attach_name[] = $filename;
346 $this->_attach_type[] = $this->_mime_types(next(explode('.', basename($filename))));
admine334c472006-10-21 19:44:22 +0000347 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
adminb0dd10f2006-08-25 17:25:49 +0000348 }
admine334c472006-10-21 19:44:22 +0000349
adminb0dd10f2006-08-25 17:25:49 +0000350 // --------------------------------------------------------------------
351
352 /**
353 * Add a Header Item
354 *
355 * @access public
356 * @param string
357 * @param string
358 * @return void
359 */
360 function _set_header($header, $value)
361 {
362 $this->_headers[$header] = $value;
363 }
adminb0dd10f2006-08-25 17:25:49 +0000364
365 // --------------------------------------------------------------------
366
367 /**
368 * Convert a String to an Array
369 *
370 * @access public
371 * @param string
372 * @return array
373 */
374 function _str_to_array($email)
375 {
376 if ( ! is_array($email))
377 {
378 if (ereg(',$', $email))
379 $email = substr($email, 0, -1);
380
381 if (ereg('^,', $email))
382 $email = substr($email, 1);
383
384 if (ereg(',', $email))
385 {
386 $x = explode(',', $email);
387 $email = array();
388
389 for ($i = 0; $i < count($x); $i ++)
390 $email[] = trim($x[$i]);
391 }
392 else
393 {
394 $email = trim($email);
395 settype($email, "array");
396 }
397 }
398 return $email;
399 }
adminb0dd10f2006-08-25 17:25:49 +0000400
401 // --------------------------------------------------------------------
402
403 /**
404 * Set Multipart Value
405 *
406 * @access public
407 * @param string
408 * @return void
409 */
410 function set_alt_message($str = '')
411 {
412 $this->alt_message = ($str == '') ? '' : $str;
413 }
adminb0dd10f2006-08-25 17:25:49 +0000414
415 // --------------------------------------------------------------------
416
417 /**
418 * Set Mailtype
419 *
420 * @access public
421 * @param string
422 * @return void
423 */
424 function set_mailtype($type = 'text')
425 {
426 $this->mailtype = ($type == 'html') ? 'html' : 'text';
427 }
adminb0dd10f2006-08-25 17:25:49 +0000428
429 // --------------------------------------------------------------------
430
431 /**
432 * Set Wordwrap
433 *
434 * @access public
435 * @param string
436 * @return void
437 */
438 function set_wordwrap($wordwrap = TRUE)
439 {
440 $this->wordwrap = ($wordwrap === FALSE) ? FALSE : TRUE;
441 }
adminb0dd10f2006-08-25 17:25:49 +0000442
443 // --------------------------------------------------------------------
444
445 /**
admin23db0dd2006-10-21 19:16:50 +0000446 * Set Protocol
adminb0dd10f2006-08-25 17:25:49 +0000447 *
448 * @access public
449 * @param string
450 * @return void
451 */
452 function set_protocol($protocol = 'mail')
admine334c472006-10-21 19:44:22 +0000453 {
adminee54c112006-09-28 17:13:38 +0000454 $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
adminb0dd10f2006-08-25 17:25:49 +0000455 }
adminb0dd10f2006-08-25 17:25:49 +0000456
457 // --------------------------------------------------------------------
458
459 /**
460 * Set Priority
461 *
462 * @access public
463 * @param integer
464 * @return void
465 */
466 function set_priority($n = 3)
467 {
admin1cf89aa2006-09-03 18:24:39 +0000468 if ( ! is_numeric($n))
adminb0dd10f2006-08-25 17:25:49 +0000469 {
470 $this->priority = 3;
471 return;
472 }
473
474 if ($n < 1 OR $n > 5)
475 {
476 $this->priority = 3;
477 return;
478 }
479
480 $this->priority = $n;
481 }
adminb0dd10f2006-08-25 17:25:49 +0000482
483 // --------------------------------------------------------------------
484
485 /**
486 * Set Newline Character
487 *
488 * @access public
489 * @param string
490 * @return void
491 */
492 function set_newline($newline = "\n")
493 {
494 if ($newline != "\n" OR $newline != "\r\n" OR $newline != "\r")
495 {
496 $this->newline = "\n";
497 return;
498 }
499
500 $this->newline = $newline;
501 }
adminb0dd10f2006-08-25 17:25:49 +0000502
503 // --------------------------------------------------------------------
504
505 /**
admin23db0dd2006-10-21 19:16:50 +0000506 * Set Message Boundary
adminb0dd10f2006-08-25 17:25:49 +0000507 *
508 * @access private
509 * @return void
510 */
511 function _set_boundaries()
512 {
admin23db0dd2006-10-21 19:16:50 +0000513 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
adminb0dd10f2006-08-25 17:25:49 +0000514 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
515 }
adminb0dd10f2006-08-25 17:25:49 +0000516
517 // --------------------------------------------------------------------
518
519 /**
520 * Get the Message ID
521 *
522 * @access private
523 * @return string
524 */
525 function _get_message_id()
526 {
527 $from = $this->_headers['Return-Path'];
528 $from = str_replace(">", "", $from);
529 $from = str_replace("<", "", $from);
530
admine334c472006-10-21 19:44:22 +0000531 return "<".uniqid('').strstr($from, '@').">";
adminb0dd10f2006-08-25 17:25:49 +0000532 }
adminb0dd10f2006-08-25 17:25:49 +0000533
534 // --------------------------------------------------------------------
535
536 /**
537 * Get Mail Protocol
538 *
539 * @access private
540 * @param bool
541 * @return string
542 */
543 function _get_protocol($return = true)
544 {
545 $this->protocol = strtolower($this->protocol);
adminee54c112006-09-28 17:13:38 +0000546 $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
adminb0dd10f2006-08-25 17:25:49 +0000547
admine334c472006-10-21 19:44:22 +0000548 if ($return == true)
adminb0dd10f2006-08-25 17:25:49 +0000549 return $this->protocol;
550 }
adminb0dd10f2006-08-25 17:25:49 +0000551
552 // --------------------------------------------------------------------
553
554 /**
555 * Get Mail Encoding
556 *
557 * @access private
558 * @param bool
559 * @return string
560 */
561 function _get_encoding($return = true)
562 {
563 $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '7bit' : $this->_encoding;
564
admine334c472006-10-21 19:44:22 +0000565 if ( ! in_array($this->charset, $this->_base_charsets, TRUE))
adminb0dd10f2006-08-25 17:25:49 +0000566 $this->_encoding = "8bit";
567
admine334c472006-10-21 19:44:22 +0000568 if ($return == true)
adminb0dd10f2006-08-25 17:25:49 +0000569 return $this->_encoding;
570 }
admine334c472006-10-21 19:44:22 +0000571
adminb0dd10f2006-08-25 17:25:49 +0000572 // --------------------------------------------------------------------
573
574 /**
575 * Get content type (text/html/attachment)
576 *
577 * @access private
578 * @return string
579 */
580 function _get_content_type()
581 {
582 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
583 return 'html';
584
585 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
586 return 'html-attach';
587
588 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
589 return 'plain-attach';
590
591 else return 'plain';
592 }
adminb0dd10f2006-08-25 17:25:49 +0000593
594 // --------------------------------------------------------------------
595
596 /**
597 * Set RFC 822 Date
598 *
599 * @access public
600 * @return string
601 */
602 function _set_date()
603 {
604 $timezone = date("Z");
605 $operator = (substr($timezone, 0, 1) == '-') ? '-' : '+';
606 $timezone = abs($timezone);
607 $timezone = ($timezone/3600) * 100 + ($timezone % 3600) /60;
608
609 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
610 }
adminb0dd10f2006-08-25 17:25:49 +0000611
612 // --------------------------------------------------------------------
613
614 /**
615 * Mime message
616 *
617 * @access private
618 * @return string
619 */
620 function _get_mime_message()
621 {
622 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
623 }
adminb0dd10f2006-08-25 17:25:49 +0000624
625 // --------------------------------------------------------------------
626
627 /**
628 * Validate Email Address
629 *
630 * @access public
631 * @param string
632 * @return bool
633 */
634 function validate_email($email)
635 {
636 if ( ! is_array($email))
637 {
638 $this->_set_error_message('email_must_be_array');
639 return FALSE;
640 }
641
642 foreach ($email as $val)
643 {
admine334c472006-10-21 19:44:22 +0000644 if ( ! $this->valid_email($val))
adminb0dd10f2006-08-25 17:25:49 +0000645 {
646 $this->_set_error_message('email_invalid_address', $val);
647 return FALSE;
648 }
649 }
650 }
adminb0dd10f2006-08-25 17:25:49 +0000651
652 // --------------------------------------------------------------------
653
654 /**
655 * Email Validation
656 *
657 * @access public
658 * @param string
659 * @return bool
660 */
661 function valid_email($address)
662 {
663 if ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address))
664 return FALSE;
admine334c472006-10-21 19:44:22 +0000665 else
adminb0dd10f2006-08-25 17:25:49 +0000666 return TRUE;
667 }
adminb0dd10f2006-08-25 17:25:49 +0000668
669 // --------------------------------------------------------------------
670
671 /**
672 * Clean Extended Email Address: Joe Smith <joe@smith.com>
673 *
674 * @access public
675 * @param string
676 * @return string
677 */
678 function clean_email($email)
679 {
680 if ( ! is_array($email))
681 {
682 if (preg_match('/\<(.*)\>/', $email, $match))
admine334c472006-10-21 19:44:22 +0000683 return $match['1'];
684 else
685 return $email;
adminb0dd10f2006-08-25 17:25:49 +0000686 }
687
688 $clean_email = array();
689
admine334c472006-10-21 19:44:22 +0000690 for ($i=0; $i < count($email); $i++)
adminb0dd10f2006-08-25 17:25:49 +0000691 {
692 if (preg_match( '/\<(.*)\>/', $email[$i], $match))
admine334c472006-10-21 19:44:22 +0000693 $clean_email[] = $match['1'];
694 else
695 $clean_email[] = $email[$i];
adminb0dd10f2006-08-25 17:25:49 +0000696 }
697
698 return $clean_email;
699 }
adminb0dd10f2006-08-25 17:25:49 +0000700
701 // --------------------------------------------------------------------
702
703 /**
704 * Build alternative plain text message
705 *
706 * This function provides the raw message for use
707 * in plain-text headers of HTML-formatted emails.
admine334c472006-10-21 19:44:22 +0000708 * If the user hasn't specified his own alternative message
adminb0dd10f2006-08-25 17:25:49 +0000709 * it creates one by stripping the HTML
710 *
711 * @access private
712 * @return string
713 */
714 function _get_alt_message()
715 {
admind16d6c22006-09-21 00:45:12 +0000716 if ($this->alt_message != "")
717 {
718 return $this->word_wrap($this->alt_message, '76');
719 }
720
adminb0dd10f2006-08-25 17:25:49 +0000721 if (eregi( '\<body(.*)\</body\>', $this->_body, $match))
722 {
723 $body = $match['1'];
724 $body = substr($body, strpos($body, ">") + 1);
725 }
726 else
727 {
728 $body = $this->_body;
729 }
730
731 $body = trim(strip_tags($body));
732 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
733 $body = str_replace("\t", "", $body);
734
735 for ($i = 20; $i >= 3; $i--)
736 {
737 $n = "";
738
739 for ($x = 1; $x <= $i; $x ++)
740 $n .= "\n";
741
742 $body = str_replace($n, "\n\n", $body);
743 }
744
745 return $this->word_wrap($body, '76');
746 }
adminb0dd10f2006-08-25 17:25:49 +0000747
748 // --------------------------------------------------------------------
749
750 /**
751 * Word Wrap
752 *
753 * @access public
754 * @param string
755 * @param integer
756 * @return string
757 */
758 function word_wrap($str, $chars = '')
759 {
760 if ($chars == '')
761 $chars = ($this->wrapchars == "") ? "76" : $this->wrapchars;
762
763 $lines = split("\n", $str);
764
765 $output = "";
766
admine334c472006-10-21 19:44:22 +0000767 while (list(, $thisline) = each($lines))
adminb0dd10f2006-08-25 17:25:49 +0000768 {
769 if (strlen($thisline) > $chars)
770 {
771 $line = "";
772
773 $words = split(" ", $thisline);
774
admine334c472006-10-21 19:44:22 +0000775 while(list(, $thisword) = each($words))
adminb0dd10f2006-08-25 17:25:49 +0000776 {
admine334c472006-10-21 19:44:22 +0000777 while((strlen($thisword)) > $chars)
adminb0dd10f2006-08-25 17:25:49 +0000778 {
779 if (stristr($thisword, '{unwrap}') !== FALSE OR stristr($thisword, '{/unwrap}') !== FALSE)
780 {
781 break;
782 }
783
784 $cur_pos = 0;
785
786 for($i=0; $i < $chars - 1; $i++)
787 {
788 $output .= $thisword[$i];
789 $cur_pos++;
790 }
791
792 $output .= "\n";
793
794 $thisword = substr($thisword, $cur_pos, (strlen($thisword) - $cur_pos));
795 }
796
admine334c472006-10-21 19:44:22 +0000797 if ((strlen($line) + strlen($thisword)) > $chars)
adminb0dd10f2006-08-25 17:25:49 +0000798 {
799 $output .= $line."\n";
800
801 $line = $thisword." ";
admine334c472006-10-21 19:44:22 +0000802 }
803 else
adminb0dd10f2006-08-25 17:25:49 +0000804 {
805 $line .= $thisword." ";
806 }
807 }
808
809 $output .= $line."\n";
admine334c472006-10-21 19:44:22 +0000810 }
811 else
adminb0dd10f2006-08-25 17:25:49 +0000812 {
813 $output .= $thisline."\n";
814 }
815 }
816
817 return $output;
818 }
adminb0dd10f2006-08-25 17:25:49 +0000819
820 // --------------------------------------------------------------------
821
822 /**
823 * Build final headers
824 *
825 * @access public
826 * @param string
827 * @return string
828 */
829 function _build_headers()
830 {
831 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
832 $this->_set_header('X-Mailer', $this->useragent);
833 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
834 $this->_set_header('Message-ID', $this->_get_message_id());
835 $this->_set_header('Mime-Version', '1.0');
836 }
adminb0dd10f2006-08-25 17:25:49 +0000837
838 // --------------------------------------------------------------------
839
840 /**
841 * Write Headers as a string
842 *
843 * @access public
844 * @return void
845 */
846 function _write_headers()
847 {
848 if ($this->protocol == 'mail')
849 {
850 $this->_subject = $this->_headers['Subject'];
851 unset($this->_headers['Subject']);
852 }
853
854 reset($this->_headers);
855 $this->_header_str = "";
856
admine334c472006-10-21 19:44:22 +0000857 foreach($this->_headers as $key => $val)
adminb0dd10f2006-08-25 17:25:49 +0000858 {
859 $val = trim($val);
860
861 if ($val != "")
862 {
863 $this->_header_str .= $key.": ".$val.$this->newline;
864 }
865 }
866
867 if ($this->_get_protocol() == 'mail')
868 $this->_header_str = substr($this->_header_str, 0, -1);
869 }
adminb0dd10f2006-08-25 17:25:49 +0000870
871 // --------------------------------------------------------------------
872
873 /**
874 * Build Final Body and attachments
875 *
876 * @access public
877 * @return void
878 */
879 function _build_message()
880 {
881 $this->_set_boundaries();
882 $this->_write_headers();
883
884 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
885
886 switch ($this->_get_content_type())
887 {
888 case 'plain' :
889
890 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
891 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
892
893 if ($this->_get_protocol() == 'mail')
894 {
895 $this->_header_str .= $hdr;
896 $this->_finalbody = $this->_body;
897
898 return;
899 }
900
901 $hdr .= $this->newline . $this->newline . $this->_body;
902
903 $this->_finalbody = $hdr;
904 return;
905
906 break;
907 case 'html' :
908
909 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline;
910 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
911 $hdr .= "--" . $this->_alt_boundary . $this->newline;
912
913 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
914 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
915 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
916
917 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
918 $hdr .= "Content-Transfer-Encoding: quoted/printable";
919
920 if ($this->_get_protocol() == 'mail')
921 {
922 $this->_header_str .= $hdr;
923 $this->_finalbody = $this->_body . $this->newline . $this->newline . "--" . $this->_alt_boundary . "--";
924
925 return;
926 }
927
928 $hdr .= $this->newline . $this->newline;
929 $hdr .= $this->_body . $this->newline . $this->newline . "--" . $this->_alt_boundary . "--";
930
931 $this->_finalbody = $hdr;
932 return;
933
934 break;
935 case 'plain-attach' :
936
937 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
938 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
939 $hdr .= "--" . $this->_atc_boundary . $this->newline;
940
941 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
942 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
943
944 if ($this->_get_protocol() == 'mail')
945 {
946 $this->_header_str .= $hdr;
947
948 $body = $this->_body . $this->newline . $this->newline;
949 }
950
951 $hdr .= $this->newline . $this->newline;
952 $hdr .= $this->_body . $this->newline . $this->newline;
953
954 break;
955 case 'html-attach' :
956
957 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
958 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
959 $hdr .= "--" . $this->_atc_boundary . $this->newline;
960
961 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
962 $hdr .= "--" . $this->_alt_boundary . $this->newline;
963
964 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
965 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
966 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
967
968 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
969 $hdr .= "Content-Transfer-Encoding: quoted/printable";
970
971 if ($this->_get_protocol() == 'mail')
972 {
973 $this->_header_str .= $hdr;
974
admine334c472006-10-21 19:44:22 +0000975 $body = $this->_body . $this->newline . $this->newline;
adminb0dd10f2006-08-25 17:25:49 +0000976 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
977 }
978
979 $hdr .= $this->newline . $this->newline;
980 $hdr .= $this->_body . $this->newline . $this->newline;
981 $hdr .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
982
983 break;
984 }
985
986 $attachment = array();
987
988 $z = 0;
989
990 for ($i=0; $i < count($this->_attach_name); $i++)
991 {
992 $filename = $this->_attach_name[$i];
993 $basename = basename($filename);
994 $ctype = $this->_attach_type[$i];
995
996 if ( ! file_exists($filename))
997 {
admine334c472006-10-21 19:44:22 +0000998 $this->_set_error_message('email_attachment_missing', $filename);
adminb0dd10f2006-08-25 17:25:49 +0000999 return FALSE;
1000 }
1001
1002 $h = "--".$this->_atc_boundary.$this->newline;
1003 $h .= "Content-type: ".$ctype."; ";
1004 $h .= "name=\"".$basename."\"".$this->newline;
1005 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
1006 $h .= "Content-Transfer-Encoding: base64".$this->newline;
1007
1008 $attachment[$z++] = $h;
1009 $file = filesize($filename) +1;
1010
1011 if ( ! $fp = fopen($filename, 'r'))
1012 {
admine334c472006-10-21 19:44:22 +00001013 $this->_set_error_message('email_attachment_unredable', $filename);
adminb0dd10f2006-08-25 17:25:49 +00001014 return FALSE;
1015 }
1016
1017 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1018 fclose($fp);
1019 }
1020
1021 if ($this->_get_protocol() == 'mail')
1022 {
1023 $this->_finalbody = $body . implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
1024
1025 return;
1026 }
1027
1028 $this->_finalbody = $hdr.implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
1029
1030 return;
1031 }
adminb0dd10f2006-08-25 17:25:49 +00001032
1033 // --------------------------------------------------------------------
1034
1035 /**
1036 * Send Email
1037 *
1038 * @access public
1039 * @return bool
1040 */
1041 function send()
1042 {
1043 if ($this->_replyto_flag == FALSE)
1044 {
1045 $this->reply_to($this->_headers['From']);
1046 }
1047
1048 if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
1049 ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
1050 ( ! isset($this->_headers['Cc'])))
1051 {
1052 $this->_set_error_message('email_no_recipients');
1053 return FALSE;
1054 }
1055
1056 $this->_build_headers();
1057
1058 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
1059 {
1060 if (count($this->_bcc_array) > $this->bcc_batch_size)
1061 return $this->batch_bcc_send();
1062 }
1063
1064 $this->_build_message();
1065
1066 if ( ! $this->_spool_email())
1067 return FALSE;
1068 else
1069 return TRUE;
1070 }
adminb0dd10f2006-08-25 17:25:49 +00001071
1072 // --------------------------------------------------------------------
1073
1074 /**
admin23db0dd2006-10-21 19:16:50 +00001075 * Batch Bcc Send. Sends groups of BCCs in batches
adminb0dd10f2006-08-25 17:25:49 +00001076 *
1077 * @access public
1078 * @return bool
1079 */
1080 function batch_bcc_send()
1081 {
1082 $float = $this->bcc_batch_size -1;
1083
1084 $flag = 0;
1085 $set = "";
1086
1087 $chunk = array();
1088
1089 for ($i = 0; $i < count($this->_bcc_array); $i++)
1090 {
1091 if (isset($this->_bcc_array[$i]))
1092 $set .= ", ".$this->_bcc_array[$i];
1093
1094 if ($i == $float)
1095 {
1096 $chunk[] = substr($set, 1);
1097 $float = $float + $this->bcc_batch_size;
1098 $set = "";
1099 }
1100
1101 if ($i == count($this->_bcc_array)-1)
1102 $chunk[] = substr($set, 1);
1103 }
1104
1105 for ($i = 0; $i < count($chunk); $i++)
1106 {
1107 unset($this->_headers['Bcc']);
1108 unset($bcc);
1109
1110 $bcc = $this->_str_to_array($chunk[$i]);
1111 $bcc = $this->clean_email($bcc);
1112
1113 if ($this->protocol != 'smtp')
1114 $this->_set_header('Bcc', implode(", ", $bcc));
1115 else
1116 $this->_bcc_array = $bcc;
1117
1118 $this->_build_message();
1119 $this->_spool_email();
1120 }
1121 }
adminb0dd10f2006-08-25 17:25:49 +00001122
1123 // --------------------------------------------------------------------
1124
1125 /**
1126 * Unwrap special elements
1127 *
1128 * @access private
1129 * @return void
1130 */
admine334c472006-10-21 19:44:22 +00001131 function _unwrap_specials()
1132 {
1133 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1134 }
adminb0dd10f2006-08-25 17:25:49 +00001135
1136 // --------------------------------------------------------------------
1137
1138 /**
1139 * Strip line-breaks via callback
1140 *
1141 * @access private
1142 * @return string
1143 */
admine334c472006-10-21 19:44:22 +00001144 function _remove_nl_callback($matches)
1145 {
1146 return preg_replace("/(\r\n)|(\r)|(\n)/", "", $matches['1']);
1147 }
adminb0dd10f2006-08-25 17:25:49 +00001148
1149 // --------------------------------------------------------------------
1150
1151 /**
1152 * Spool mail to the mail server
1153 *
1154 * @access private
admin23db0dd2006-10-21 19:16:50 +00001155 * @return bool
adminb0dd10f2006-08-25 17:25:49 +00001156 */
1157 function _spool_email()
1158 {
admine334c472006-10-21 19:44:22 +00001159 $this->_unwrap_specials();
adminb0dd10f2006-08-25 17:25:49 +00001160
1161 switch ($this->_get_protocol())
1162 {
1163 case 'mail' :
1164
1165 if ( ! $this->_send_with_mail())
1166 {
1167 $this->_set_error_message('email_send_failure_phpmail');
1168 return FALSE;
1169 }
1170 break;
admine334c472006-10-21 19:44:22 +00001171 case 'sendmail' :
adminb0dd10f2006-08-25 17:25:49 +00001172
1173 if ( ! $this->_send_with_sendmail())
1174 {
1175 $this->_set_error_message('email_send_failure_sendmail');
1176 return FALSE;
1177 }
1178 break;
admine334c472006-10-21 19:44:22 +00001179 case 'smtp' :
adminb0dd10f2006-08-25 17:25:49 +00001180
1181 if ( ! $this->_send_with_smtp())
1182 {
1183 $this->_set_error_message('email_send_failure_smtp');
1184 return FALSE;
1185 }
1186 break;
1187
1188 }
1189
1190 $this->_set_error_message('email_sent', $this->_get_protocol());
1191 return true;
1192 }
adminb0dd10f2006-08-25 17:25:49 +00001193
1194 // --------------------------------------------------------------------
1195
1196 /**
1197 * Send using mail()
1198 *
1199 * @access private
1200 * @return bool
1201 */
1202 function _send_with_mail()
1203 {
1204 if ($this->_safe_mode == TRUE)
1205 {
1206 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
1207 return FALSE;
1208 else
1209 return TRUE;
1210 }
1211 else
1212 {
1213 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f".$this->clean_email($this->_headers['From'])))
1214 return FALSE;
1215 else
1216 return TRUE;
1217 }
1218 }
adminb0dd10f2006-08-25 17:25:49 +00001219
1220 // --------------------------------------------------------------------
1221
1222 /**
1223 * Send using Sendmail
1224 *
1225 * @access private
1226 * @return bool
1227 */
1228 function _send_with_sendmail()
1229 {
1230 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1231
1232 if ( ! is_resource($fp))
1233 {
1234 $this->_set_error_message('email_no_socket');
1235 return FALSE;
1236 }
1237
1238 fputs($fp, $this->_header_str);
1239 fputs($fp, $this->_finalbody);
1240 pclose($fp) >> 8 & 0xFF;
1241
1242 return TRUE;
1243 }
adminb0dd10f2006-08-25 17:25:49 +00001244
1245 // --------------------------------------------------------------------
1246
1247 /**
1248 * Send using SMTP
1249 *
1250 * @access private
1251 * @return bool
1252 */
1253 function _send_with_smtp()
1254 {
admine334c472006-10-21 19:44:22 +00001255 if ($this->smtp_host == '')
1256 {
adminb0dd10f2006-08-25 17:25:49 +00001257 $this->_set_error_message('email_no_hostname');
1258 return FALSE;
1259 }
1260
1261 $this->_smtp_connect();
1262 $this->_smtp_authenticate();
1263
1264 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1265
1266 foreach($this->_recipients as $val)
1267 $this->_send_command('to', $val);
1268
1269 if (count($this->_cc_array) > 0)
1270 {
1271 foreach($this->_cc_array as $val)
1272 {
1273 if ($val != "")
1274 $this->_send_command('to', $val);
1275 }
1276 }
1277
1278 if (count($this->_bcc_array) > 0)
1279 {
1280 foreach($this->_bcc_array as $val)
1281 {
1282 if ($val != "")
1283 $this->_send_command('to', $val);
1284 }
1285 }
1286
1287 $this->_send_command('data');
1288
1289 $this->_send_data($this->_header_str . $this->_finalbody);
1290
1291 $this->_send_data('.');
1292
1293 $reply = $this->_get_smtp_data();
1294
1295 $this->_set_error_message($reply);
1296
1297 if (substr($reply, 0, 3) != '250')
1298 {
1299 $this->_set_error_message('email_smtp_error', $reply);
1300 return FALSE;
1301 }
1302
1303 $this->_send_command('quit');
1304 return true;
1305 }
adminb0dd10f2006-08-25 17:25:49 +00001306
1307 // --------------------------------------------------------------------
1308
1309 /**
1310 * SMTP Connect
1311 *
1312 * @access public
1313 * @param string
1314 * @return string
1315 */
1316 function _smtp_connect()
1317 {
1318
admine334c472006-10-21 19:44:22 +00001319 $this->_smtp_connect = fsockopen($this->smtp_host,
adminb0dd10f2006-08-25 17:25:49 +00001320 $this->smtp_port,
admine334c472006-10-21 19:44:22 +00001321 $errno,
1322 $errstr,
adminb0dd10f2006-08-25 17:25:49 +00001323 $this->smtp_timeout);
1324
1325 if( ! is_resource($this->_smtp_connect))
1326 {
1327 $this->_set_error_message('email_smtp_error', $errno." ".$errstr);
1328 return FALSE;
1329 }
1330
1331 $this->_set_error_message($this->_get_smtp_data());
1332 return $this->_send_command('hello');
1333 }
adminb0dd10f2006-08-25 17:25:49 +00001334
1335 // --------------------------------------------------------------------
1336
1337 /**
1338 * Send SMTP command
1339 *
1340 * @access private
1341 * @param string
1342 * @param string
1343 * @return string
1344 */
1345 function _send_command($cmd, $data = '')
1346 {
1347 switch ($cmd)
1348 {
1349 case 'hello' :
1350
1351 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1352 $this->_send_data('EHLO '.$this->_get_hostname());
1353 else
1354 $this->_send_data('HELO '.$this->_get_hostname());
1355
1356 $resp = 250;
1357 break;
1358 case 'from' :
1359
1360 $this->_send_data('MAIL FROM:<'.$data.'>');
1361
1362 $resp = 250;
1363 break;
1364 case 'to' :
1365
1366 $this->_send_data('RCPT TO:<'.$data.'>');
1367
1368 $resp = 250;
1369 break;
1370 case 'data' :
1371
1372 $this->_send_data('DATA');
1373
1374 $resp = 354;
1375 break;
1376 case 'quit' :
1377
1378 $this->_send_data('QUIT');
1379
1380 $resp = 221;
1381 break;
1382 }
1383
1384 $reply = $this->_get_smtp_data();
1385
1386 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1387
1388 if (substr($reply, 0, 3) != $resp)
1389 {
1390 $this->_set_error_message('email_smtp_error', $reply);
1391 return FALSE;
1392 }
1393
1394 if ($cmd == 'quit')
1395 fclose($this->_smtp_connect);
1396
1397 return true;
1398 }
adminb0dd10f2006-08-25 17:25:49 +00001399
1400 // --------------------------------------------------------------------
1401
1402 /**
1403 * SMTP Authenticate
1404 *
1405 * @access private
1406 * @return bool
1407 */
1408 function _smtp_authenticate()
1409 {
1410 if ( ! $this->_smtp_auth)
1411 return true;
1412
1413 if ($this->smtp_user == "" AND $this->smtp_pass == "")
1414 {
1415 $this->_set_error_message('email_no_smtp_unpw');
1416 return FALSE;
1417 }
1418
1419 $this->_send_data('AUTH LOGIN');
1420
1421 $reply = $this->_get_smtp_data();
1422
1423 if (substr($reply, 0, 3) != '334')
1424 {
1425 $this->_set_error_message('email_filed_smtp_login', $reply);
1426 return FALSE;
1427 }
1428
1429 $this->_send_data(base64_encode($this->smtp_user));
1430
1431 $reply = $this->_get_smtp_data();
1432
1433 if (substr($reply, 0, 3) != '334')
1434 {
1435 $this->_set_error_message('email_smtp_auth_un', $reply);
1436 return FALSE;
1437 }
1438
1439 $this->_send_data(base64_encode($this->smtp_pass));
1440
1441 $reply = $this->_get_smtp_data();
1442
1443 if (substr($reply, 0, 3) != '235')
1444 {
1445 $this->_set_error_message('email_smtp_auth_pw', $reply);
1446 return FALSE;
1447 }
1448
1449 return true;
1450 }
adminb0dd10f2006-08-25 17:25:49 +00001451
1452 // --------------------------------------------------------------------
1453
1454 /**
1455 * Send SMTP data
1456 *
1457 * @access private
1458 * @return bool
1459 */
1460 function _send_data($data)
1461 {
1462 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1463 {
1464 $this->_set_error_message('email_smtp_data_failure', $data);
1465 return FALSE;
1466 }
1467 else
1468 return true;
1469 }
adminb0dd10f2006-08-25 17:25:49 +00001470
1471 // --------------------------------------------------------------------
1472
1473 /**
1474 * Get SMTP data
1475 *
1476 * @access private
1477 * @return string
1478 */
1479 function _get_smtp_data()
1480 {
admine334c472006-10-21 19:44:22 +00001481 $data = "";
1482
1483 while ($str = fgets($this->_smtp_connect, 512))
1484 {
adminb0dd10f2006-08-25 17:25:49 +00001485 $data .= $str;
1486
1487 if (substr($str, 3, 1) == " ")
1488 break;
admine334c472006-10-21 19:44:22 +00001489 }
1490
1491 return $data;
adminb0dd10f2006-08-25 17:25:49 +00001492 }
adminb0dd10f2006-08-25 17:25:49 +00001493
1494 // --------------------------------------------------------------------
1495
1496 /**
1497 * Get Hostname
1498 *
1499 * @access private
1500 * @return string
1501 */
1502 function _get_hostname()
1503 {
derek243f5cc2006-10-03 00:25:03 +00001504 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
adminb0dd10f2006-08-25 17:25:49 +00001505 }
adminb0dd10f2006-08-25 17:25:49 +00001506
1507 // --------------------------------------------------------------------
1508
1509 /**
1510 * Get IP
1511 *
1512 * @access private
1513 * @return string
1514 */
1515 function _get_ip()
1516 {
1517 if ($this->_IP !== FALSE)
1518 {
1519 return $this->_IP;
1520 }
1521
1522 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1523 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1524 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1525
1526 if ($cip && $rip) $this->_IP = $cip;
1527 elseif ($rip) $this->_IP = $rip;
1528 elseif ($cip) $this->_IP = $cip;
1529 elseif ($fip) $this->_IP = $fip;
1530
1531 if (strstr($this->_IP, ','))
1532 {
1533 $x = explode(',', $this->_IP);
1534 $this->_IP = end($x);
1535 }
1536
1537 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
1538 $this->_IP = '0.0.0.0';
1539
1540 unset($cip);
1541 unset($rip);
1542 unset($fip);
1543
1544 return $this->_IP;
1545 }
adminb0dd10f2006-08-25 17:25:49 +00001546
1547 // --------------------------------------------------------------------
1548
1549 /**
admin23db0dd2006-10-21 19:16:50 +00001550 * Get Debug Message
adminb0dd10f2006-08-25 17:25:49 +00001551 *
1552 * @access public
1553 * @return string
1554 */
1555 function print_debugger()
1556 {
1557 $msg = '';
1558
1559 if (count($this->_debug_msg) > 0)
1560 {
1561 foreach ($this->_debug_msg as $val)
1562 {
1563 $msg .= $val;
1564 }
1565 }
1566
1567 $msg .= "<pre>".$this->_header_str."\n".$this->_subject."\n".$this->_finalbody.'</pre>';
1568 return $msg;
1569 }
adminb0dd10f2006-08-25 17:25:49 +00001570
1571 // --------------------------------------------------------------------
1572
1573 /**
1574 * Set Message
1575 *
1576 * @access public
1577 * @param string
1578 * @return string
1579 */
1580 function _set_error_message($msg, $val = '')
1581 {
admin88a8ad12006-10-07 03:16:32 +00001582 $CI =& get_instance();
1583 $CI->lang->load('email');
adminb0dd10f2006-08-25 17:25:49 +00001584
admin88a8ad12006-10-07 03:16:32 +00001585 if (FALSE === ($line = $CI->lang->line($msg)))
adminb0dd10f2006-08-25 17:25:49 +00001586 {
1587 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1588 }
1589 else
1590 {
1591 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1592 }
1593 }
adminb0dd10f2006-08-25 17:25:49 +00001594
1595 // --------------------------------------------------------------------
1596
1597 /**
1598 * Mime Types
1599 *
1600 * @access private
1601 * @param string
1602 * @return string
1603 */
1604 function _mime_types($ext = "")
1605 {
1606 $mimes = array( 'hqx' => 'application/mac-binhex40',
1607 'cpt' => 'application/mac-compactpro',
1608 'doc' => 'application/msword',
1609 'bin' => 'application/macbinary',
1610 'dms' => 'application/octet-stream',
1611 'lha' => 'application/octet-stream',
1612 'lzh' => 'application/octet-stream',
1613 'exe' => 'application/octet-stream',
1614 'class' => 'application/octet-stream',
1615 'psd' => 'application/octet-stream',
1616 'so' => 'application/octet-stream',
1617 'sea' => 'application/octet-stream',
1618 'dll' => 'application/octet-stream',
1619 'oda' => 'application/oda',
1620 'pdf' => 'application/pdf',
1621 'ai' => 'application/postscript',
1622 'eps' => 'application/postscript',
1623 'ps' => 'application/postscript',
1624 'smi' => 'application/smil',
1625 'smil' => 'application/smil',
1626 'mif' => 'application/vnd.mif',
1627 'xls' => 'application/vnd.ms-excel',
1628 'ppt' => 'application/vnd.ms-powerpoint',
1629 'wbxml' => 'application/vnd.wap.wbxml',
1630 'wmlc' => 'application/vnd.wap.wmlc',
1631 'dcr' => 'application/x-director',
1632 'dir' => 'application/x-director',
1633 'dxr' => 'application/x-director',
1634 'dvi' => 'application/x-dvi',
1635 'gtar' => 'application/x-gtar',
1636 'php' => 'application/x-httpd-php',
1637 'php4' => 'application/x-httpd-php',
1638 'php3' => 'application/x-httpd-php',
1639 'phtml' => 'application/x-httpd-php',
1640 'phps' => 'application/x-httpd-php-source',
1641 'js' => 'application/x-javascript',
1642 'swf' => 'application/x-shockwave-flash',
1643 'sit' => 'application/x-stuffit',
1644 'tar' => 'application/x-tar',
1645 'tgz' => 'application/x-tar',
1646 'xhtml' => 'application/xhtml+xml',
1647 'xht' => 'application/xhtml+xml',
1648 'zip' => 'application/zip',
1649 'mid' => 'audio/midi',
1650 'midi' => 'audio/midi',
1651 'mpga' => 'audio/mpeg',
1652 'mp2' => 'audio/mpeg',
1653 'mp3' => 'audio/mpeg',
1654 'aif' => 'audio/x-aiff',
1655 'aiff' => 'audio/x-aiff',
1656 'aifc' => 'audio/x-aiff',
1657 'ram' => 'audio/x-pn-realaudio',
1658 'rm' => 'audio/x-pn-realaudio',
1659 'rpm' => 'audio/x-pn-realaudio-plugin',
1660 'ra' => 'audio/x-realaudio',
1661 'rv' => 'video/vnd.rn-realvideo',
1662 'wav' => 'audio/x-wav',
1663 'bmp' => 'image/bmp',
1664 'gif' => 'image/gif',
1665 'jpeg' => 'image/jpeg',
1666 'jpg' => 'image/jpeg',
1667 'jpe' => 'image/jpeg',
1668 'png' => 'image/png',
1669 'tiff' => 'image/tiff',
1670 'tif' => 'image/tiff',
1671 'css' => 'text/css',
1672 'html' => 'text/html',
1673 'htm' => 'text/html',
1674 'shtml' => 'text/html',
1675 'txt' => 'text/plain',
1676 'text' => 'text/plain',
1677 'log' => 'text/plain',
1678 'rtx' => 'text/richtext',
1679 'rtf' => 'text/rtf',
1680 'xml' => 'text/xml',
1681 'xsl' => 'text/xml',
1682 'mpeg' => 'video/mpeg',
1683 'mpg' => 'video/mpeg',
1684 'mpe' => 'video/mpeg',
1685 'qt' => 'video/quicktime',
1686 'mov' => 'video/quicktime',
1687 'avi' => 'video/x-msvideo',
1688 'movie' => 'video/x-sgi-movie',
1689 'doc' => 'application/msword',
1690 'word' => 'application/msword',
1691 'xl' => 'application/excel',
1692 'eml' => 'message/rfc822'
1693 );
1694
1695 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
1696 }
adminb0dd10f2006-08-25 17:25:49 +00001697
1698}
1699// END CI_Email class
1700?>