blob: 1896787420c25228c943853734779a0033cabdc5 [file] [log] [blame]
Derek Allard0fe82972008-01-15 13:58:47 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allard0fe82972008-01-15 13:58:47 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard0fe82972008-01-15 13:58:47 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Email Class
20 *
21 * Permits email to be sent using Mail, Sendmail, or SMTP.
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Libraries
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000027 * @link http://codeigniter.com/user_guide/libraries/email.html
Derek Allard0fe82972008-01-15 13:58:47 +000028 */
29class CI_Email {
30
31 var $useragent = "CodeIgniter";
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
Derek Jonese7c4c322008-01-22 18:16:39 +000039 var $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
Derek Allard0fe82972008-01-15 13:58:47 +000040 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
Derek Jonese7c4c322008-01-22 18:16:39 +000045 var $validate = FALSE; // TRUE/FALSE. Enables email validation
Derek Allard0fe82972008-01-15 13:58:47 +000046 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)
Derek Allard0fe82972008-01-15 13:58:47 +000048 var $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
49 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
50 // distasteful, is the only thing that seems to work for all environments.
Derek Allard80ddb6b2008-02-25 12:51:00 +000051 var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
Derek Jonese7c4c322008-01-22 18:16:39 +000052 var $bcc_batch_mode = FALSE; // TRUE/FALSE Turns on/off Bcc batch feature
53 var $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
Derek Allard80ddb6b2008-02-25 12:51:00 +000054 var $_safe_mode = FALSE;
Derek Allard0fe82972008-01-15 13:58:47 +000055 var $_subject = "";
56 var $_body = "";
57 var $_finalbody = "";
58 var $_alt_boundary = "";
59 var $_atc_boundary = "";
60 var $_header_str = "";
61 var $_smtp_connect = "";
Derek Allard80ddb6b2008-02-25 12:51:00 +000062 var $_encoding = "8bit";
Derek Allard0fe82972008-01-15 13:58:47 +000063 var $_IP = FALSE;
64 var $_smtp_auth = FALSE;
65 var $_replyto_flag = FALSE;
66 var $_debug_msg = array();
67 var $_recipients = array();
68 var $_cc_array = array();
69 var $_bcc_array = array();
70 var $_headers = array();
71 var $_attach_name = array();
72 var $_attach_type = array();
73 var $_attach_disp = array();
74 var $_protocols = array('mail', 'sendmail', 'smtp');
Derek Jonese7c4c322008-01-22 18:16:39 +000075 var $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
Derek Allard0fe82972008-01-15 13:58:47 +000076 var $_bit_depths = array('7bit', '8bit');
77 var $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
78
79
80 /**
81 * Constructor - Sets Email Preferences
82 *
83 * The constructor can be passed an array of config values
84 */
85 function CI_Email($config = array())
86 {
87 if (count($config) > 0)
88 {
89 $this->initialize($config);
90 }
91
Derek Allard80ddb6b2008-02-25 12:51:00 +000092 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
93 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
94
Derek Allard0fe82972008-01-15 13:58:47 +000095 log_message('debug', "Email Class Initialized");
96 }
97
98 // --------------------------------------------------------------------
99
100 /**
101 * Initialize preferences
102 *
103 * @access public
104 * @param array
105 * @return void
106 */
107 function initialize($config = array())
108 {
109 $this->clear();
110 foreach ($config as $key => $val)
111 {
112 if (isset($this->$key))
113 {
114 $method = 'set_'.$key;
115
116 if (method_exists($this, $method))
117 {
118 $this->$method($val);
119 }
120 else
121 {
122 $this->$key = $val;
123 }
124 }
125 }
Derek Allard0fe82972008-01-15 13:58:47 +0000126 }
127
128 // --------------------------------------------------------------------
129
130 /**
131 * Initialize the Email Data
132 *
133 * @access public
134 * @return void
135 */
136 function clear($clear_attachments = FALSE)
137 {
138 $this->_subject = "";
139 $this->_body = "";
140 $this->_finalbody = "";
141 $this->_header_str = "";
142 $this->_replyto_flag = FALSE;
143 $this->_recipients = array();
144 $this->_headers = array();
145 $this->_debug_msg = array();
146
147 $this->_set_header('User-Agent', $this->useragent);
148 $this->_set_header('Date', $this->_set_date());
149
150 if ($clear_attachments !== FALSE)
151 {
152 $this->_attach_name = array();
153 $this->_attach_type = array();
154 $this->_attach_disp = array();
155 }
156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Set FROM
162 *
163 * @access public
164 * @param string
165 * @param string
166 * @return void
167 */
168 function from($from, $name = '')
169 {
170 if (preg_match( '/\<(.*)\>/', $from, $match))
171 $from = $match['1'];
172
173 if ($this->validate)
174 $this->validate_email($this->_str_to_array($from));
175
176 if ($name != '' && substr($name, 0, 1) != '"')
177 {
178 $name = '"'.$name.'"';
179 }
180
181 $this->_set_header('From', $name.' <'.$from.'>');
182 $this->_set_header('Return-Path', '<'.$from.'>');
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Set Reply-to
189 *
190 * @access public
191 * @param string
192 * @param string
193 * @return void
194 */
195 function reply_to($replyto, $name = '')
196 {
197 if (preg_match( '/\<(.*)\>/', $replyto, $match))
198 $replyto = $match['1'];
199
200 if ($this->validate)
201 $this->validate_email($this->_str_to_array($replyto));
202
203 if ($name == '')
204 {
205 $name = $replyto;
206 }
207
208 if (substr($name, 0, 1) != '"')
209 {
210 $name = '"'.$name.'"';
211 }
212
213 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
214 $this->_replyto_flag = TRUE;
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Set Recipients
221 *
222 * @access public
223 * @param string
224 * @return void
225 */
226 function to($to)
227 {
228 $to = $this->_str_to_array($to);
229 $to = $this->clean_email($to);
230
231 if ($this->validate)
232 $this->validate_email($to);
233
234 if ($this->_get_protocol() != 'mail')
235 $this->_set_header('To', implode(", ", $to));
236
237 switch ($this->_get_protocol())
238 {
239 case 'smtp' : $this->_recipients = $to;
240 break;
241 case 'sendmail' : $this->_recipients = implode(", ", $to);
242 break;
243 case 'mail' : $this->_recipients = implode(", ", $to);
244 break;
245 }
246 }
247
248 // --------------------------------------------------------------------
249
250 /**
251 * Set CC
252 *
253 * @access public
254 * @param string
255 * @return void
256 */
257 function cc($cc)
258 {
259 $cc = $this->_str_to_array($cc);
260 $cc = $this->clean_email($cc);
261
262 if ($this->validate)
263 $this->validate_email($cc);
264
265 $this->_set_header('Cc', implode(", ", $cc));
266
267 if ($this->_get_protocol() == "smtp")
268 $this->_cc_array = $cc;
269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Set BCC
275 *
276 * @access public
277 * @param string
278 * @param string
279 * @return void
280 */
281 function bcc($bcc, $limit = '')
282 {
283 if ($limit != '' && is_numeric($limit))
284 {
Derek Jonese7c4c322008-01-22 18:16:39 +0000285 $this->bcc_batch_mode = TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +0000286 $this->bcc_batch_size = $limit;
287 }
288
289 $bcc = $this->_str_to_array($bcc);
290 $bcc = $this->clean_email($bcc);
291
292 if ($this->validate)
293 $this->validate_email($bcc);
294
295 if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
296 $this->_bcc_array = $bcc;
297 else
298 $this->_set_header('Bcc', implode(", ", $bcc));
299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Set Email Subject
305 *
306 * @access public
307 * @param string
308 * @return void
309 */
310 function subject($subject)
311 {
312 $subject = preg_replace("/(\r\n)|(\r)|(\n)/", "", $subject);
313 $subject = preg_replace("/(\t)/", " ", $subject);
314
315 $this->_set_header('Subject', trim($subject));
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Set Body
322 *
323 * @access public
324 * @param string
325 * @return void
326 */
327 function message($body)
328 {
329 $this->_body = stripslashes(rtrim(str_replace("\r", "", $body)));
330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Assign file attachments
336 *
337 * @access public
338 * @param string
339 * @return string
340 */
341 function attach($filename, $disposition = 'attachment')
342 {
343 $this->_attach_name[] = $filename;
344 $this->_attach_type[] = $this->_mime_types(next(explode('.', basename($filename))));
345 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * Add a Header Item
352 *
353 * @access public
354 * @param string
355 * @param string
356 * @return void
357 */
358 function _set_header($header, $value)
359 {
360 $this->_headers[$header] = $value;
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Convert a String to an Array
367 *
368 * @access public
369 * @param string
370 * @return array
371 */
372 function _str_to_array($email)
373 {
374 if ( ! is_array($email))
Derek Allard80ddb6b2008-02-25 12:51:00 +0000375 {
376 if (strpos($email, ',') !== FALSE)
377 {
378 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
Derek Allard0fe82972008-01-15 13:58:47 +0000379 }
380 else
381 {
382 $email = trim($email);
383 settype($email, "array");
384 }
385 }
386 return $email;
387 }
388
389 // --------------------------------------------------------------------
390
391 /**
392 * Set Multipart Value
393 *
394 * @access public
395 * @param string
396 * @return void
397 */
398 function set_alt_message($str = '')
399 {
400 $this->alt_message = ($str == '') ? '' : $str;
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Set Mailtype
407 *
408 * @access public
409 * @param string
410 * @return void
411 */
412 function set_mailtype($type = 'text')
413 {
414 $this->mailtype = ($type == 'html') ? 'html' : 'text';
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Set Wordwrap
421 *
422 * @access public
423 * @param string
424 * @return void
425 */
426 function set_wordwrap($wordwrap = TRUE)
427 {
428 $this->wordwrap = ($wordwrap === FALSE) ? FALSE : TRUE;
429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * Set Protocol
435 *
436 * @access public
437 * @param string
438 * @return void
439 */
440 function set_protocol($protocol = 'mail')
441 {
442 $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
443 }
444
445 // --------------------------------------------------------------------
446
447 /**
448 * Set Priority
449 *
450 * @access public
451 * @param integer
452 * @return void
453 */
454 function set_priority($n = 3)
455 {
456 if ( ! is_numeric($n))
457 {
458 $this->priority = 3;
459 return;
460 }
461
462 if ($n < 1 OR $n > 5)
463 {
464 $this->priority = 3;
465 return;
466 }
467
468 $this->priority = $n;
469 }
470
471 // --------------------------------------------------------------------
472
473 /**
474 * Set Newline Character
475 *
476 * @access public
477 * @param string
478 * @return void
479 */
480 function set_newline($newline = "\n")
481 {
482 if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
483 {
484 $this->newline = "\n";
485 return;
486 }
487
488 $this->newline = $newline;
489 }
490
491 // --------------------------------------------------------------------
492
493 /**
494 * Set Message Boundary
495 *
496 * @access private
497 * @return void
498 */
499 function _set_boundaries()
500 {
501 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
502 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * Get the Message ID
509 *
510 * @access private
511 * @return string
512 */
513 function _get_message_id()
514 {
515 $from = $this->_headers['Return-Path'];
516 $from = str_replace(">", "", $from);
517 $from = str_replace("<", "", $from);
518
519 return "<".uniqid('').strstr($from, '@').">";
520 }
521
522 // --------------------------------------------------------------------
523
524 /**
525 * Get Mail Protocol
526 *
527 * @access private
528 * @param bool
529 * @return string
530 */
Derek Jonese7c4c322008-01-22 18:16:39 +0000531 function _get_protocol($return = TRUE)
Derek Allard0fe82972008-01-15 13:58:47 +0000532 {
533 $this->protocol = strtolower($this->protocol);
534 $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
535
Derek Jonese7c4c322008-01-22 18:16:39 +0000536 if ($return == TRUE)
Derek Allard0fe82972008-01-15 13:58:47 +0000537 return $this->protocol;
538 }
539
540 // --------------------------------------------------------------------
541
542 /**
543 * Get Mail Encoding
544 *
545 * @access private
546 * @param bool
547 * @return string
548 */
Derek Jonese7c4c322008-01-22 18:16:39 +0000549 function _get_encoding($return = TRUE)
Derek Allard0fe82972008-01-15 13:58:47 +0000550 {
Derek Jonese7c4c322008-01-22 18:16:39 +0000551 $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding;
Derek Allard0fe82972008-01-15 13:58:47 +0000552
Derek Jonese7c4c322008-01-22 18:16:39 +0000553 foreach ($this->_base_charsets as $charset)
554 {
555 if (strncmp($charset, $this->charset, strlen($charset)) == 0)
556 {
557 $this->_encoding = '7bit';
558 }
559 }
Derek Allard0fe82972008-01-15 13:58:47 +0000560
Derek Jonese7c4c322008-01-22 18:16:39 +0000561 if ($return == TRUE)
562 {
563 return $this->_encoding;
564 }
Derek Allard0fe82972008-01-15 13:58:47 +0000565 }
566
567 // --------------------------------------------------------------------
568
569 /**
570 * Get content type (text/html/attachment)
571 *
572 * @access private
573 * @return string
574 */
575 function _get_content_type()
576 {
577 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
578 return 'html';
579
580 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
581 return 'html-attach';
582
583 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
584 return 'plain-attach';
585
586 else return 'plain';
587 }
588
589 // --------------------------------------------------------------------
590
591 /**
592 * Set RFC 822 Date
593 *
594 * @access public
595 * @return string
596 */
597 function _set_date()
598 {
599 $timezone = date("Z");
600 $operator = (substr($timezone, 0, 1) == '-') ? '-' : '+';
601 $timezone = abs($timezone);
602 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60;
603
604 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
605 }
606
607 // --------------------------------------------------------------------
608
609 /**
610 * Mime message
611 *
612 * @access private
613 * @return string
614 */
615 function _get_mime_message()
616 {
617 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
618 }
619
620 // --------------------------------------------------------------------
621
622 /**
623 * Validate Email Address
624 *
625 * @access public
626 * @param string
627 * @return bool
628 */
629 function validate_email($email)
630 {
631 if ( ! is_array($email))
632 {
633 $this->_set_error_message('email_must_be_array');
634 return FALSE;
635 }
636
637 foreach ($email as $val)
638 {
639 if ( ! $this->valid_email($val))
640 {
641 $this->_set_error_message('email_invalid_address', $val);
642 return FALSE;
643 }
644 }
645 }
646
647 // --------------------------------------------------------------------
648
649 /**
650 * Email Validation
651 *
652 * @access public
653 * @param string
654 * @return bool
655 */
656 function valid_email($address)
657 {
Derek Allard80ddb6b2008-02-25 12:51:00 +0000658 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +0000659 }
660
661 // --------------------------------------------------------------------
662
663 /**
664 * Clean Extended Email Address: Joe Smith <joe@smith.com>
665 *
666 * @access public
667 * @param string
668 * @return string
669 */
670 function clean_email($email)
671 {
672 if ( ! is_array($email))
673 {
674 if (preg_match('/\<(.*)\>/', $email, $match))
675 return $match['1'];
676 else
677 return $email;
678 }
679
680 $clean_email = array();
Derek Allard80ddb6b2008-02-25 12:51:00 +0000681
Derek Jones80e14042008-01-16 17:46:56 +0000682 foreach ($email as $addy)
Derek Allard0fe82972008-01-15 13:58:47 +0000683 {
Derek Jones80e14042008-01-16 17:46:56 +0000684 if (preg_match( '/\<(.*)\>/', $addy, $match))
685 {
686 $clean_email[] = $match['1'];
687 }
Derek Allard0fe82972008-01-15 13:58:47 +0000688 else
Derek Jones80e14042008-01-16 17:46:56 +0000689 {
690 $clean_email[] = $addy;
691 }
Derek Allard0fe82972008-01-15 13:58:47 +0000692 }
693
694 return $clean_email;
695 }
696
697 // --------------------------------------------------------------------
698
699 /**
700 * Build alternative plain text message
701 *
702 * This function provides the raw message for use
703 * in plain-text headers of HTML-formatted emails.
704 * If the user hasn't specified his own alternative message
705 * it creates one by stripping the HTML
706 *
707 * @access private
708 * @return string
709 */
710 function _get_alt_message()
711 {
712 if ($this->alt_message != "")
713 {
714 return $this->word_wrap($this->alt_message, '76');
715 }
Derek Allard80ddb6b2008-02-25 12:51:00 +0000716
717 if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match))
Derek Allard0fe82972008-01-15 13:58:47 +0000718 {
719 $body = $match['1'];
Derek Allard0fe82972008-01-15 13:58:47 +0000720 }
721 else
722 {
723 $body = $this->_body;
724 }
725
726 $body = trim(strip_tags($body));
727 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
728 $body = str_replace("\t", "", $body);
729
730 for ($i = 20; $i >= 3; $i--)
731 {
732 $n = "";
733
734 for ($x = 1; $x <= $i; $x ++)
735 $n .= "\n";
736
737 $body = str_replace($n, "\n\n", $body);
738 }
739
740 return $this->word_wrap($body, '76');
741 }
742
743 // --------------------------------------------------------------------
744
745 /**
746 * Word Wrap
747 *
748 * @access public
749 * @param string
750 * @param integer
751 * @return string
752 */
753 function word_wrap($str, $charlim = '')
754 {
755 // Se the character limit
756 if ($charlim == '')
757 {
758 $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
759 }
760
761 // Reduce multiple spaces
762 $str = preg_replace("| +|", " ", $str);
763
764 // Standardize newlines
765 $str = preg_replace("/\r\n|\r/", "\n", $str);
766
767 // If the current word is surrounded by {unwrap} tags we'll
768 // strip the entire chunk and replace it with a marker.
769 $unwrap = array();
770 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
771 {
772 for ($i = 0; $i < count($matches['0']); $i++)
773 {
774 $unwrap[] = $matches['1'][$i];
775 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
776 }
777 }
778
779 // Use PHP's native function to do the initial wordwrap.
780 // We set the cut flag to FALSE so that any individual words that are
781 // too long get left alone. In the next step we'll deal with them.
782 $str = wordwrap($str, $charlim, "\n", FALSE);
783
784 // Split the string into individual lines of text and cycle through them
785 $output = "";
786 foreach (explode("\n", $str) as $line)
787 {
788 // Is the line within the allowed character count?
789 // If so we'll join it to the output and continue
790 if (strlen($line) <= $charlim)
791 {
792 $output .= $line.$this->newline;
793 continue;
794 }
795
796 $temp = '';
797 while((strlen($line)) > $charlim)
798 {
799 // If the over-length word is a URL we won't wrap it
800 if (preg_match("!\[url.+\]|://|wwww.!", $line))
801 {
802 break;
803 }
804
805 // Trim the word down
806 $temp .= substr($line, 0, $charlim-1);
807 $line = substr($line, $charlim-1);
808 }
809
810 // If $temp contains data it means we had to split up an over-length
811 // word into smaller chunks so we'll add it back to our current line
812 if ($temp != '')
813 {
814 $output .= $temp.$this->newline.$line;
815 }
816 else
817 {
818 $output .= $line;
819 }
820
821 $output .= $this->newline;
822 }
823
824 // Put our markers back
825 if (count($unwrap) > 0)
826 {
827 foreach ($unwrap as $key => $val)
828 {
829 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
830 }
831 }
832
833 return $output;
834 }
835
836 // --------------------------------------------------------------------
837
838 /**
839 * Build final headers
840 *
841 * @access public
842 * @param string
843 * @return string
844 */
845 function _build_headers()
846 {
847 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
848 $this->_set_header('X-Mailer', $this->useragent);
849 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
850 $this->_set_header('Message-ID', $this->_get_message_id());
851 $this->_set_header('Mime-Version', '1.0');
852 }
853
854 // --------------------------------------------------------------------
855
856 /**
857 * Write Headers as a string
858 *
859 * @access public
860 * @return void
861 */
862 function _write_headers()
863 {
864 if ($this->protocol == 'mail')
865 {
866 $this->_subject = $this->_headers['Subject'];
867 unset($this->_headers['Subject']);
868 }
869
870 reset($this->_headers);
871 $this->_header_str = "";
872
873 foreach($this->_headers as $key => $val)
874 {
875 $val = trim($val);
876
877 if ($val != "")
878 {
879 $this->_header_str .= $key.": ".$val.$this->newline;
880 }
881 }
882
883 if ($this->_get_protocol() == 'mail')
884 $this->_header_str = substr($this->_header_str, 0, -1);
885 }
886
887 // --------------------------------------------------------------------
888
889 /**
890 * Build Final Body and attachments
891 *
892 * @access public
893 * @return void
894 */
895 function _build_message()
896 {
897 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
898 {
899 $this->_body = $this->word_wrap($this->_body);
900 }
901
902 $this->_set_boundaries();
903 $this->_write_headers();
904
905 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
906
907 switch ($this->_get_content_type())
908 {
909 case 'plain' :
910
911 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
912 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
913
914 if ($this->_get_protocol() == 'mail')
915 {
916 $this->_header_str .= $hdr;
917 $this->_finalbody = $this->_body;
918
919 return;
920 }
921
922 $hdr .= $this->newline . $this->newline . $this->_body;
923
924 $this->_finalbody = $hdr;
925 return;
926
927 break;
928 case 'html' :
Derek Allard80ddb6b2008-02-25 12:51:00 +0000929
930 if ($this->send_multipart === FALSE)
931 {
932 $hdr .= "Content-Type: text/html;". $this->newline;
933 }
934 else
935 {
936 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline;
937 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
938 $hdr .= "--" . $this->_alt_boundary . $this->newline;
939
940 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
941 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
942 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
Derek Allard0fe82972008-01-15 13:58:47 +0000943
Derek Allard80ddb6b2008-02-25 12:51:00 +0000944 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
945 $hdr .= "Content-Transfer-Encoding: quoted-printable";
946 }
Derek Allard0fe82972008-01-15 13:58:47 +0000947
948 $this->_body = $this->_prep_quoted_printable($this->_body);
949
950 if ($this->_get_protocol() == 'mail')
951 {
952 $this->_header_str .= $hdr;
Derek Allard80ddb6b2008-02-25 12:51:00 +0000953 $this->_finalbody = $this->_body . $this->newline . $this->newline;
954
955 if ($this->send_multipart !== FALSE)
956 {
957 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
958 }
Derek Allard0fe82972008-01-15 13:58:47 +0000959
960 return;
961 }
962
963 $hdr .= $this->newline . $this->newline;
Derek Allard80ddb6b2008-02-25 12:51:00 +0000964 $hdr .= $this->_body . $this->newline . $this->newline;
965
966 if ($this->send_multipart !== FALSE)
967 {
968 $hdr .= "--" . $this->_alt_boundary . "--";
969 }
Derek Allard0fe82972008-01-15 13:58:47 +0000970
971 $this->_finalbody = $hdr;
972 return;
973
974 break;
975 case 'plain-attach' :
976
977 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
978 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
979 $hdr .= "--" . $this->_atc_boundary . $this->newline;
980
981 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
982 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
983
984 if ($this->_get_protocol() == 'mail')
985 {
986 $this->_header_str .= $hdr;
987
988 $body = $this->_body . $this->newline . $this->newline;
989 }
990
991 $hdr .= $this->newline . $this->newline;
992 $hdr .= $this->_body . $this->newline . $this->newline;
993
994 break;
995 case 'html-attach' :
996
997 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
998 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
999 $hdr .= "--" . $this->_atc_boundary . $this->newline;
1000
1001 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
1002 $hdr .= "--" . $this->_alt_boundary . $this->newline;
1003
1004 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1005 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1006 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1007
1008 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1009 $hdr .= "Content-Transfer-Encoding: quoted-printable";
1010
1011 $this->_body = $this->_prep_quoted_printable($this->_body);
1012
1013 if ($this->_get_protocol() == 'mail')
1014 {
1015 $this->_header_str .= $hdr;
1016
1017 $body = $this->_body . $this->newline . $this->newline;
1018 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
1019 }
1020
1021 $hdr .= $this->newline . $this->newline;
1022 $hdr .= $this->_body . $this->newline . $this->newline;
1023 $hdr .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
1024
1025 break;
1026 }
1027
1028 $attachment = array();
1029
1030 $z = 0;
1031
1032 for ($i=0; $i < count($this->_attach_name); $i++)
1033 {
1034 $filename = $this->_attach_name[$i];
1035 $basename = basename($filename);
1036 $ctype = $this->_attach_type[$i];
1037
1038 if ( ! file_exists($filename))
1039 {
1040 $this->_set_error_message('email_attachment_missing', $filename);
1041 return FALSE;
1042 }
1043
1044 $h = "--".$this->_atc_boundary.$this->newline;
1045 $h .= "Content-type: ".$ctype."; ";
1046 $h .= "name=\"".$basename."\"".$this->newline;
1047 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
1048 $h .= "Content-Transfer-Encoding: base64".$this->newline;
1049
1050 $attachment[$z++] = $h;
1051 $file = filesize($filename) +1;
1052
1053 if ( ! $fp = fopen($filename, 'r'))
1054 {
1055 $this->_set_error_message('email_attachment_unreadable', $filename);
1056 return FALSE;
1057 }
1058
1059 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1060 fclose($fp);
1061 }
1062
1063 if ($this->_get_protocol() == 'mail')
1064 {
1065 $this->_finalbody = $body . implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
1066
1067 return;
1068 }
1069
1070 $this->_finalbody = $hdr.implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
1071
1072 return;
1073 }
1074
1075 // --------------------------------------------------------------------
1076
1077 /**
1078 * Prep Quoted Printable
1079 *
1080 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1081 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1082 *
1083 * @access public
1084 * @param string
1085 * @param integer
1086 * @return string
1087 */
1088 function _prep_quoted_printable($str, $charlim = '')
1089 {
1090 // Set the character limit
1091 // Don't allow over 76, as that will make servers and MUAs barf
1092 // all over quoted-printable data
1093 if ($charlim == '' OR $charlim > '76')
1094 {
1095 $charlim = '76';
1096 }
1097
1098 // Reduce multiple spaces
1099 $str = preg_replace("| +|", " ", $str);
1100
1101 // Standardize newlines
1102 $str = preg_replace("/\r\n|\r/", "\n", $str);
1103
1104 // We are intentionally wrapping so mail servers will encode characters
1105 // properly and MUAs will behave, so {unwrap} must go!
1106 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1107
1108 // Break into an array of lines
1109 $lines = preg_split("/\n/", $str);
1110
1111 $escape = '=';
1112 $output = '';
1113
1114 foreach ($lines as $line)
1115 {
1116 $length = strlen($line);
1117 $temp = '';
1118
1119 // Loop through each character in the line to add soft-wrap
1120 // characters at the end of a line " =\r\n" and add the newly
1121 // processed line(s) to the output (see comment on $crlf class property)
1122 for ($i = 0; $i < $length; $i++)
1123 {
1124 // Grab the next character
1125 $char = substr($line, $i, 1);
1126 $ascii = ord($char);
1127
1128 // Convert spaces and tabs but only if it's the end of the line
1129 if ($i == ($length - 1))
1130 {
1131 $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($char)) : $char;
1132 }
1133
1134 // encode = signs
1135 if ($ascii == '61')
1136 {
1137 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
1138 }
1139
1140 // If we're at the character limit, add the line to the output,
1141 // reset our temp variable, and keep on chuggin'
1142 if ((strlen($temp) + strlen($char)) >= $charlim)
1143 {
1144 $output .= $temp.$escape.$this->crlf;
1145 $temp = '';
1146 }
1147
1148 // Add the character to our temporary line
1149 $temp .= $char;
1150 }
1151
1152 // Add our completed line to the output
1153 $output .= $temp.$this->crlf;
1154 }
1155
1156 // get rid of extra CRLF tacked onto the end
1157 $output = substr($output, 0, strlen($this->crlf) * -1);
1158
1159 return $output;
1160 }
1161
1162 // --------------------------------------------------------------------
1163
1164 /**
1165 * Send Email
1166 *
1167 * @access public
1168 * @return bool
1169 */
1170 function send()
1171 {
1172 if ($this->_replyto_flag == FALSE)
1173 {
1174 $this->reply_to($this->_headers['From']);
1175 }
1176
1177 if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
1178 ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
1179 ( ! isset($this->_headers['Cc'])))
1180 {
1181 $this->_set_error_message('email_no_recipients');
1182 return FALSE;
1183 }
1184
1185 $this->_build_headers();
1186
1187 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
1188 {
1189 if (count($this->_bcc_array) > $this->bcc_batch_size)
1190 return $this->batch_bcc_send();
1191 }
1192
1193 $this->_build_message();
1194
1195 if ( ! $this->_spool_email())
1196 return FALSE;
1197 else
1198 return TRUE;
1199 }
1200
1201 // --------------------------------------------------------------------
1202
1203 /**
1204 * Batch Bcc Send. Sends groups of BCCs in batches
1205 *
1206 * @access public
1207 * @return bool
1208 */
1209 function batch_bcc_send()
1210 {
1211 $float = $this->bcc_batch_size -1;
1212
1213 $flag = 0;
1214 $set = "";
1215
1216 $chunk = array();
1217
1218 for ($i = 0; $i < count($this->_bcc_array); $i++)
1219 {
1220 if (isset($this->_bcc_array[$i]))
1221 $set .= ", ".$this->_bcc_array[$i];
1222
1223 if ($i == $float)
1224 {
1225 $chunk[] = substr($set, 1);
1226 $float = $float + $this->bcc_batch_size;
1227 $set = "";
1228 }
1229
1230 if ($i == count($this->_bcc_array)-1)
1231 $chunk[] = substr($set, 1);
1232 }
1233
1234 for ($i = 0; $i < count($chunk); $i++)
1235 {
1236 unset($this->_headers['Bcc']);
1237 unset($bcc);
1238
1239 $bcc = $this->_str_to_array($chunk[$i]);
1240 $bcc = $this->clean_email($bcc);
1241
1242 if ($this->protocol != 'smtp')
1243 $this->_set_header('Bcc', implode(", ", $bcc));
1244 else
1245 $this->_bcc_array = $bcc;
1246
1247 $this->_build_message();
1248 $this->_spool_email();
1249 }
1250 }
1251
1252 // --------------------------------------------------------------------
1253
1254 /**
1255 * Unwrap special elements
1256 *
1257 * @access private
1258 * @return void
1259 */
1260 function _unwrap_specials()
1261 {
1262 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1263 }
1264
1265 // --------------------------------------------------------------------
1266
1267 /**
1268 * Strip line-breaks via callback
1269 *
1270 * @access private
1271 * @return string
1272 */
1273 function _remove_nl_callback($matches)
1274 {
1275 return preg_replace("/(\r\n)|(\r)|(\n)/", "", $matches['1']);
1276 }
1277
1278 // --------------------------------------------------------------------
1279
1280 /**
1281 * Spool mail to the mail server
1282 *
1283 * @access private
1284 * @return bool
1285 */
1286 function _spool_email()
1287 {
1288 $this->_unwrap_specials();
1289
1290 switch ($this->_get_protocol())
1291 {
1292 case 'mail' :
1293
1294 if ( ! $this->_send_with_mail())
1295 {
1296 $this->_set_error_message('email_send_failure_phpmail');
1297 return FALSE;
1298 }
1299 break;
1300 case 'sendmail' :
1301
1302 if ( ! $this->_send_with_sendmail())
1303 {
1304 $this->_set_error_message('email_send_failure_sendmail');
1305 return FALSE;
1306 }
1307 break;
1308 case 'smtp' :
1309
1310 if ( ! $this->_send_with_smtp())
1311 {
1312 $this->_set_error_message('email_send_failure_smtp');
1313 return FALSE;
1314 }
1315 break;
1316
1317 }
1318
1319 $this->_set_error_message('email_sent', $this->_get_protocol());
Derek Jonese7c4c322008-01-22 18:16:39 +00001320 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001321 }
1322
1323 // --------------------------------------------------------------------
1324
1325 /**
1326 * Send using mail()
1327 *
1328 * @access private
1329 * @return bool
1330 */
1331 function _send_with_mail()
1332 {
1333 if ($this->_safe_mode == TRUE)
1334 {
1335 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
1336 return FALSE;
1337 else
1338 return TRUE;
1339 }
1340 else
1341 {
1342 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1343 // we've encountered servers that seem to require it to be in place.
1344 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
1345 return FALSE;
1346 else
1347 return TRUE;
1348 }
1349 }
1350
1351 // --------------------------------------------------------------------
1352
1353 /**
1354 * Send using Sendmail
1355 *
1356 * @access private
1357 * @return bool
1358 */
1359 function _send_with_sendmail()
1360 {
1361 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1362
1363 if ( ! is_resource($fp))
1364 {
1365 $this->_set_error_message('email_no_socket');
1366 return FALSE;
1367 }
1368
1369 fputs($fp, $this->_header_str);
1370 fputs($fp, $this->_finalbody);
1371 pclose($fp) >> 8 & 0xFF;
1372
1373 return TRUE;
1374 }
1375
1376 // --------------------------------------------------------------------
1377
1378 /**
1379 * Send using SMTP
1380 *
1381 * @access private
1382 * @return bool
1383 */
1384 function _send_with_smtp()
1385 {
1386 if ($this->smtp_host == '')
1387 {
1388 $this->_set_error_message('email_no_hostname');
1389 return FALSE;
1390 }
1391
1392 $this->_smtp_connect();
1393 $this->_smtp_authenticate();
1394
1395 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1396
1397 foreach($this->_recipients as $val)
1398 $this->_send_command('to', $val);
1399
1400 if (count($this->_cc_array) > 0)
1401 {
1402 foreach($this->_cc_array as $val)
1403 {
1404 if ($val != "")
1405 $this->_send_command('to', $val);
1406 }
1407 }
1408
1409 if (count($this->_bcc_array) > 0)
1410 {
1411 foreach($this->_bcc_array as $val)
1412 {
1413 if ($val != "")
1414 $this->_send_command('to', $val);
1415 }
1416 }
1417
1418 $this->_send_command('data');
1419
1420 $this->_send_data($this->_header_str . $this->_finalbody);
1421
1422 $this->_send_data('.');
1423
1424 $reply = $this->_get_smtp_data();
1425
1426 $this->_set_error_message($reply);
1427
1428 if (substr($reply, 0, 3) != '250')
1429 {
1430 $this->_set_error_message('email_smtp_error', $reply);
1431 return FALSE;
1432 }
1433
1434 $this->_send_command('quit');
Derek Jonese7c4c322008-01-22 18:16:39 +00001435 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001436 }
1437
1438 // --------------------------------------------------------------------
1439
1440 /**
1441 * SMTP Connect
1442 *
1443 * @access public
1444 * @param string
1445 * @return string
1446 */
1447 function _smtp_connect()
1448 {
Derek Allard0fe82972008-01-15 13:58:47 +00001449 $this->_smtp_connect = fsockopen($this->smtp_host,
1450 $this->smtp_port,
1451 $errno,
1452 $errstr,
1453 $this->smtp_timeout);
1454
1455 if( ! is_resource($this->_smtp_connect))
1456 {
1457 $this->_set_error_message('email_smtp_error', $errno." ".$errstr);
1458 return FALSE;
1459 }
1460
1461 $this->_set_error_message($this->_get_smtp_data());
1462 return $this->_send_command('hello');
1463 }
1464
1465 // --------------------------------------------------------------------
1466
1467 /**
1468 * Send SMTP command
1469 *
1470 * @access private
1471 * @param string
1472 * @param string
1473 * @return string
1474 */
1475 function _send_command($cmd, $data = '')
1476 {
1477 switch ($cmd)
1478 {
1479 case 'hello' :
1480
1481 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1482 $this->_send_data('EHLO '.$this->_get_hostname());
1483 else
1484 $this->_send_data('HELO '.$this->_get_hostname());
1485
1486 $resp = 250;
1487 break;
1488 case 'from' :
1489
1490 $this->_send_data('MAIL FROM:<'.$data.'>');
1491
1492 $resp = 250;
1493 break;
1494 case 'to' :
1495
1496 $this->_send_data('RCPT TO:<'.$data.'>');
1497
1498 $resp = 250;
1499 break;
1500 case 'data' :
1501
1502 $this->_send_data('DATA');
1503
1504 $resp = 354;
1505 break;
1506 case 'quit' :
1507
1508 $this->_send_data('QUIT');
1509
1510 $resp = 221;
1511 break;
1512 }
1513
1514 $reply = $this->_get_smtp_data();
1515
1516 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1517
1518 if (substr($reply, 0, 3) != $resp)
1519 {
1520 $this->_set_error_message('email_smtp_error', $reply);
1521 return FALSE;
1522 }
1523
1524 if ($cmd == 'quit')
1525 fclose($this->_smtp_connect);
1526
Derek Jonese7c4c322008-01-22 18:16:39 +00001527 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001528 }
1529
1530 // --------------------------------------------------------------------
1531
1532 /**
1533 * SMTP Authenticate
1534 *
1535 * @access private
1536 * @return bool
1537 */
1538 function _smtp_authenticate()
1539 {
1540 if ( ! $this->_smtp_auth)
Derek Jonese7c4c322008-01-22 18:16:39 +00001541 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001542
1543 if ($this->smtp_user == "" AND $this->smtp_pass == "")
1544 {
1545 $this->_set_error_message('email_no_smtp_unpw');
1546 return FALSE;
1547 }
1548
1549 $this->_send_data('AUTH LOGIN');
1550
1551 $reply = $this->_get_smtp_data();
1552
1553 if (substr($reply, 0, 3) != '334')
1554 {
1555 $this->_set_error_message('email_failed_smtp_login', $reply);
1556 return FALSE;
1557 }
1558
1559 $this->_send_data(base64_encode($this->smtp_user));
1560
1561 $reply = $this->_get_smtp_data();
1562
1563 if (substr($reply, 0, 3) != '334')
1564 {
1565 $this->_set_error_message('email_smtp_auth_un', $reply);
1566 return FALSE;
1567 }
1568
1569 $this->_send_data(base64_encode($this->smtp_pass));
1570
1571 $reply = $this->_get_smtp_data();
1572
1573 if (substr($reply, 0, 3) != '235')
1574 {
1575 $this->_set_error_message('email_smtp_auth_pw', $reply);
1576 return FALSE;
1577 }
1578
Derek Jonese7c4c322008-01-22 18:16:39 +00001579 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001580 }
1581
1582 // --------------------------------------------------------------------
1583
1584 /**
1585 * Send SMTP data
1586 *
1587 * @access private
1588 * @return bool
1589 */
1590 function _send_data($data)
1591 {
1592 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1593 {
1594 $this->_set_error_message('email_smtp_data_failure', $data);
1595 return FALSE;
1596 }
1597 else
Derek Jonese7c4c322008-01-22 18:16:39 +00001598 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001599 }
1600
1601 // --------------------------------------------------------------------
1602
1603 /**
1604 * Get SMTP data
1605 *
1606 * @access private
1607 * @return string
1608 */
1609 function _get_smtp_data()
1610 {
1611 $data = "";
1612
1613 while ($str = fgets($this->_smtp_connect, 512))
1614 {
1615 $data .= $str;
1616
1617 if (substr($str, 3, 1) == " ")
Derek Allard80ddb6b2008-02-25 12:51:00 +00001618 break;
Derek Allard0fe82972008-01-15 13:58:47 +00001619 }
1620
1621 return $data;
1622 }
1623
1624 // --------------------------------------------------------------------
1625
1626 /**
1627 * Get Hostname
1628 *
1629 * @access private
1630 * @return string
1631 */
1632 function _get_hostname()
1633 {
1634 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
1635 }
1636
1637 // --------------------------------------------------------------------
1638
1639 /**
1640 * Get IP
1641 *
1642 * @access private
1643 * @return string
1644 */
1645 function _get_ip()
1646 {
1647 if ($this->_IP !== FALSE)
1648 {
1649 return $this->_IP;
1650 }
1651
1652 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1653 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1654 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1655
1656 if ($cip && $rip) $this->_IP = $cip;
1657 elseif ($rip) $this->_IP = $rip;
1658 elseif ($cip) $this->_IP = $cip;
1659 elseif ($fip) $this->_IP = $fip;
1660
1661 if (strstr($this->_IP, ','))
1662 {
1663 $x = explode(',', $this->_IP);
1664 $this->_IP = end($x);
1665 }
1666
1667 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
1668 $this->_IP = '0.0.0.0';
1669
1670 unset($cip);
1671 unset($rip);
1672 unset($fip);
1673
1674 return $this->_IP;
1675 }
1676
1677 // --------------------------------------------------------------------
1678
1679 /**
1680 * Get Debug Message
1681 *
1682 * @access public
1683 * @return string
1684 */
1685 function print_debugger()
1686 {
1687 $msg = '';
1688
1689 if (count($this->_debug_msg) > 0)
1690 {
1691 foreach ($this->_debug_msg as $val)
1692 {
1693 $msg .= $val;
1694 }
1695 }
1696
1697 $msg .= "<pre>".$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
1698 return $msg;
1699 }
1700
1701 // --------------------------------------------------------------------
1702
1703 /**
1704 * Set Message
1705 *
1706 * @access public
1707 * @param string
1708 * @return string
1709 */
1710 function _set_error_message($msg, $val = '')
1711 {
1712 $CI =& get_instance();
1713 $CI->lang->load('email');
1714
1715 if (FALSE === ($line = $CI->lang->line($msg)))
1716 {
1717 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1718 }
1719 else
1720 {
1721 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1722 }
1723 }
1724
1725 // --------------------------------------------------------------------
1726
1727 /**
1728 * Mime Types
1729 *
1730 * @access private
1731 * @param string
1732 * @return string
1733 */
1734 function _mime_types($ext = "")
1735 {
1736 $mimes = array( 'hqx' => 'application/mac-binhex40',
1737 'cpt' => 'application/mac-compactpro',
1738 'doc' => 'application/msword',
1739 'bin' => 'application/macbinary',
1740 'dms' => 'application/octet-stream',
1741 'lha' => 'application/octet-stream',
1742 'lzh' => 'application/octet-stream',
1743 'exe' => 'application/octet-stream',
1744 'class' => 'application/octet-stream',
1745 'psd' => 'application/octet-stream',
1746 'so' => 'application/octet-stream',
1747 'sea' => 'application/octet-stream',
1748 'dll' => 'application/octet-stream',
1749 'oda' => 'application/oda',
1750 'pdf' => 'application/pdf',
1751 'ai' => 'application/postscript',
1752 'eps' => 'application/postscript',
1753 'ps' => 'application/postscript',
1754 'smi' => 'application/smil',
1755 'smil' => 'application/smil',
1756 'mif' => 'application/vnd.mif',
1757 'xls' => 'application/vnd.ms-excel',
1758 'ppt' => 'application/vnd.ms-powerpoint',
1759 'wbxml' => 'application/vnd.wap.wbxml',
1760 'wmlc' => 'application/vnd.wap.wmlc',
1761 'dcr' => 'application/x-director',
1762 'dir' => 'application/x-director',
1763 'dxr' => 'application/x-director',
1764 'dvi' => 'application/x-dvi',
1765 'gtar' => 'application/x-gtar',
1766 'php' => 'application/x-httpd-php',
1767 'php4' => 'application/x-httpd-php',
1768 'php3' => 'application/x-httpd-php',
1769 'phtml' => 'application/x-httpd-php',
1770 'phps' => 'application/x-httpd-php-source',
1771 'js' => 'application/x-javascript',
1772 'swf' => 'application/x-shockwave-flash',
1773 'sit' => 'application/x-stuffit',
1774 'tar' => 'application/x-tar',
1775 'tgz' => 'application/x-tar',
1776 'xhtml' => 'application/xhtml+xml',
1777 'xht' => 'application/xhtml+xml',
1778 'zip' => 'application/zip',
1779 'mid' => 'audio/midi',
1780 'midi' => 'audio/midi',
1781 'mpga' => 'audio/mpeg',
1782 'mp2' => 'audio/mpeg',
1783 'mp3' => 'audio/mpeg',
1784 'aif' => 'audio/x-aiff',
1785 'aiff' => 'audio/x-aiff',
1786 'aifc' => 'audio/x-aiff',
1787 'ram' => 'audio/x-pn-realaudio',
1788 'rm' => 'audio/x-pn-realaudio',
1789 'rpm' => 'audio/x-pn-realaudio-plugin',
1790 'ra' => 'audio/x-realaudio',
1791 'rv' => 'video/vnd.rn-realvideo',
1792 'wav' => 'audio/x-wav',
1793 'bmp' => 'image/bmp',
1794 'gif' => 'image/gif',
1795 'jpeg' => 'image/jpeg',
1796 'jpg' => 'image/jpeg',
1797 'jpe' => 'image/jpeg',
1798 'png' => 'image/png',
1799 'tiff' => 'image/tiff',
1800 'tif' => 'image/tiff',
1801 'css' => 'text/css',
1802 'html' => 'text/html',
1803 'htm' => 'text/html',
1804 'shtml' => 'text/html',
1805 'txt' => 'text/plain',
1806 'text' => 'text/plain',
1807 'log' => 'text/plain',
1808 'rtx' => 'text/richtext',
1809 'rtf' => 'text/rtf',
1810 'xml' => 'text/xml',
1811 'xsl' => 'text/xml',
1812 'mpeg' => 'video/mpeg',
1813 'mpg' => 'video/mpeg',
1814 'mpe' => 'video/mpeg',
1815 'qt' => 'video/quicktime',
1816 'mov' => 'video/quicktime',
1817 'avi' => 'video/x-msvideo',
1818 'movie' => 'video/x-sgi-movie',
1819 'doc' => 'application/msword',
1820 'word' => 'application/msword',
1821 'xl' => 'application/excel',
1822 'eml' => 'message/rfc822'
1823 );
1824
1825 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
1826 }
1827
1828}
1829// END CI_Email class
1830?>