blob: 043e14bc4408c0a053fdf4fafe5a7033621a2372 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +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
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @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
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/email.html
28 */
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
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 $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.
51 var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
52 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
54 var $_safe_mode = FALSE;
55 var $_subject = "";
56 var $_body = "";
57 var $_finalbody = "";
58 var $_alt_boundary = "";
59 var $_atc_boundary = "";
60 var $_header_str = "";
61 var $_smtp_connect = "";
62 var $_encoding = "8bit";
63 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');
75 var $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
76 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 else
92 {
93 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
94 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
95 }
96
97 log_message('debug', "Email Class Initialized");
98 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Initialize preferences
104 *
105 * @access public
106 * @param array
107 * @return void
108 */
109 function initialize($config = array())
110 {
111 $this->clear();
112 foreach ($config as $key => $val)
113 {
114 if (isset($this->$key))
115 {
116 $method = 'set_'.$key;
117
118 if (method_exists($this, $method))
119 {
120 $this->$method($val);
121 }
122 else
123 {
124 $this->$key = $val;
125 }
126 }
127 }
128
129 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
130 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
136 * Initialize the Email Data
137 *
138 * @access public
139 * @return void
140 */
141 function clear($clear_attachments = FALSE)
142 {
143 $this->_subject = "";
144 $this->_body = "";
145 $this->_finalbody = "";
146 $this->_header_str = "";
147 $this->_replyto_flag = FALSE;
148 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500149 $this->_cc_array = array();
150 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 $this->_headers = array();
152 $this->_debug_msg = array();
153
154 $this->_set_header('User-Agent', $this->useragent);
155 $this->_set_header('Date', $this->_set_date());
156
157 if ($clear_attachments !== FALSE)
158 {
159 $this->_attach_name = array();
160 $this->_attach_type = array();
161 $this->_attach_disp = array();
162 }
163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 // --------------------------------------------------------------------
166
167 /**
168 * Set FROM
169 *
170 * @access public
171 * @param string
172 * @param string
173 * @return void
174 */
175 function from($from, $name = '')
176 {
177 if (preg_match( '/\<(.*)\>/', $from, $match))
178 {
179 $from = $match['1'];
180 }
181
182 if ($this->validate)
183 {
184 $this->validate_email($this->_str_to_array($from));
185 }
186
187 // prepare the display name
188 if ($name != '')
189 {
190 // only use Q encoding if there are characters that would require it
191 if ( ! preg_match('/[\200-\377]/', $name))
192 {
193 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000194 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196 else
197 {
198 $name = $this->_prep_q_encoding($name, TRUE);
199 }
200 }
201
202 $this->_set_header('From', $name.' <'.$from.'>');
203 $this->_set_header('Return-Path', '<'.$from.'>');
204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // --------------------------------------------------------------------
207
208 /**
209 * Set Reply-to
210 *
211 * @access public
212 * @param string
213 * @param string
214 * @return void
215 */
216 function reply_to($replyto, $name = '')
217 {
218 if (preg_match( '/\<(.*)\>/', $replyto, $match))
219 {
220 $replyto = $match['1'];
221 }
222
223 if ($this->validate)
224 {
225 $this->validate_email($this->_str_to_array($replyto));
226 }
227
228 if ($name == '')
229 {
230 $name = $replyto;
231 }
232
233 if (strncmp($name, '"', 1) != 0)
234 {
235 $name = '"'.$name.'"';
236 }
237
238 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
239 $this->_replyto_flag = TRUE;
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // --------------------------------------------------------------------
243
244 /**
245 * Set Recipients
246 *
247 * @access public
248 * @param string
249 * @return void
250 */
251 function to($to)
252 {
253 $to = $this->_str_to_array($to);
254 $to = $this->clean_email($to);
255
256 if ($this->validate)
257 {
258 $this->validate_email($to);
259 }
260
261 if ($this->_get_protocol() != 'mail')
262 {
263 $this->_set_header('To', implode(", ", $to));
264 }
265
266 switch ($this->_get_protocol())
267 {
268 case 'smtp' : $this->_recipients = $to;
269 break;
270 case 'sendmail' : $this->_recipients = implode(", ", $to);
271 break;
272 case 'mail' : $this->_recipients = implode(", ", $to);
273 break;
274 }
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 // --------------------------------------------------------------------
278
279 /**
280 * Set CC
281 *
282 * @access public
283 * @param string
284 * @return void
285 */
286 function cc($cc)
287 {
288 $cc = $this->_str_to_array($cc);
289 $cc = $this->clean_email($cc);
290
291 if ($this->validate)
292 {
293 $this->validate_email($cc);
294 }
295
296 $this->_set_header('Cc', implode(", ", $cc));
297
298 if ($this->_get_protocol() == "smtp")
299 {
300 $this->_cc_array = $cc;
301 }
302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // --------------------------------------------------------------------
305
306 /**
307 * Set BCC
308 *
309 * @access public
310 * @param string
311 * @param string
312 * @return void
313 */
314 function bcc($bcc, $limit = '')
315 {
316 if ($limit != '' && is_numeric($limit))
317 {
318 $this->bcc_batch_mode = TRUE;
319 $this->bcc_batch_size = $limit;
320 }
321
322 $bcc = $this->_str_to_array($bcc);
323 $bcc = $this->clean_email($bcc);
324
325 if ($this->validate)
326 {
327 $this->validate_email($bcc);
328 }
329
330 if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
331 {
332 $this->_bcc_array = $bcc;
333 }
334 else
335 {
336 $this->_set_header('Bcc', implode(", ", $bcc));
337 }
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // --------------------------------------------------------------------
341
342 /**
343 * Set Email Subject
344 *
345 * @access public
346 * @param string
347 * @return void
348 */
349 function subject($subject)
350 {
351 $subject = $this->_prep_q_encoding($subject);
352 $this->_set_header('Subject', $subject);
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // --------------------------------------------------------------------
356
357 /**
358 * Set Body
359 *
360 * @access public
361 * @param string
362 * @return void
363 */
364 function message($body)
365 {
366 $this->_body = stripslashes(rtrim(str_replace("\r", "", $body)));
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // --------------------------------------------------------------------
370
371 /**
372 * Assign file attachments
373 *
374 * @access public
375 * @param string
376 * @return void
377 */
378 function attach($filename, $disposition = 'attachment')
379 {
380 $this->_attach_name[] = $filename;
381 $this->_attach_type[] = $this->_mime_types(next(explode('.', basename($filename))));
382 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Add a Header Item
389 *
390 * @access private
391 * @param string
392 * @param string
393 * @return void
394 */
395 function _set_header($header, $value)
396 {
397 $this->_headers[$header] = $value;
398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // --------------------------------------------------------------------
401
402 /**
403 * Convert a String to an Array
404 *
405 * @access private
406 * @param string
407 * @return array
408 */
409 function _str_to_array($email)
410 {
411 if ( ! is_array($email))
412 {
413 if (strpos($email, ',') !== FALSE)
414 {
415 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
416 }
417 else
418 {
419 $email = trim($email);
420 settype($email, "array");
421 }
422 }
423 return $email;
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 // --------------------------------------------------------------------
427
428 /**
429 * Set Multipart Value
430 *
431 * @access public
432 * @param string
433 * @return void
434 */
435 function set_alt_message($str = '')
436 {
437 $this->alt_message = ($str == '') ? '' : $str;
438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // --------------------------------------------------------------------
441
442 /**
443 * Set Mailtype
444 *
445 * @access public
446 * @param string
447 * @return void
448 */
449 function set_mailtype($type = 'text')
450 {
451 $this->mailtype = ($type == 'html') ? 'html' : 'text';
452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 // --------------------------------------------------------------------
455
456 /**
457 * Set Wordwrap
458 *
459 * @access public
460 * @param string
461 * @return void
462 */
463 function set_wordwrap($wordwrap = TRUE)
464 {
465 $this->wordwrap = ($wordwrap === FALSE) ? FALSE : TRUE;
466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 // --------------------------------------------------------------------
469
470 /**
471 * Set Protocol
472 *
473 * @access public
474 * @param string
475 * @return void
476 */
477 function set_protocol($protocol = 'mail')
478 {
479 $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * Set Priority
486 *
487 * @access public
488 * @param integer
489 * @return void
490 */
491 function set_priority($n = 3)
492 {
493 if ( ! is_numeric($n))
494 {
495 $this->priority = 3;
496 return;
497 }
498
499 if ($n < 1 OR $n > 5)
500 {
501 $this->priority = 3;
502 return;
503 }
504
505 $this->priority = $n;
506 }
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 // --------------------------------------------------------------------
509
510 /**
511 * Set Newline Character
512 *
513 * @access public
514 * @param string
515 * @return void
516 */
517 function set_newline($newline = "\n")
518 {
519 if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
520 {
521 $this->newline = "\n";
522 return;
523 }
524
525 $this->newline = $newline;
526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 // --------------------------------------------------------------------
529
530 /**
531 * Set CRLF
532 *
533 * @access public
534 * @param string
535 * @return void
536 */
537 function set_crlf($crlf = "\n")
538 {
539 if ($crlf != "\n" AND $crlf != "\r\n" AND $crlf != "\r")
540 {
541 $this->crlf = "\n";
542 return;
543 }
544
545 $this->crlf = $crlf;
546 }
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 // --------------------------------------------------------------------
549
550 /**
551 * Set Message Boundary
552 *
553 * @access private
554 * @return void
555 */
556 function _set_boundaries()
557 {
558 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
559 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 // --------------------------------------------------------------------
563
564 /**
565 * Get the Message ID
566 *
567 * @access private
568 * @return string
569 */
570 function _get_message_id()
571 {
572 $from = $this->_headers['Return-Path'];
573 $from = str_replace(">", "", $from);
574 $from = str_replace("<", "", $from);
575
576 return "<".uniqid('').strstr($from, '@').">";
577 }
Barry Mienydd671972010-10-04 16:33:58 +0200578
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 // --------------------------------------------------------------------
580
581 /**
582 * Get Mail Protocol
583 *
584 * @access private
585 * @param bool
586 * @return string
587 */
588 function _get_protocol($return = TRUE)
589 {
590 $this->protocol = strtolower($this->protocol);
591 $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
592
593 if ($return == TRUE)
594 {
595 return $this->protocol;
596 }
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 // --------------------------------------------------------------------
600
601 /**
602 * Get Mail Encoding
603 *
604 * @access private
605 * @param bool
606 * @return string
607 */
608 function _get_encoding($return = TRUE)
609 {
610 $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding;
611
612 foreach ($this->_base_charsets as $charset)
613 {
614 if (strncmp($charset, $this->charset, strlen($charset)) == 0)
615 {
616 $this->_encoding = '7bit';
617 }
618 }
619
620 if ($return == TRUE)
621 {
622 return $this->_encoding;
623 }
624 }
625
626 // --------------------------------------------------------------------
627
628 /**
629 * Get content type (text/html/attachment)
630 *
631 * @access private
632 * @return string
633 */
634 function _get_content_type()
635 {
636 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
637 {
638 return 'html';
639 }
640 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
641 {
642 return 'html-attach';
643 }
644 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
645 {
646 return 'plain-attach';
647 }
648 else
649 {
650 return 'plain';
651 }
652 }
Barry Mienydd671972010-10-04 16:33:58 +0200653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 // --------------------------------------------------------------------
655
656 /**
657 * Set RFC 822 Date
658 *
659 * @access private
660 * @return string
661 */
662 function _set_date()
663 {
664 $timezone = date("Z");
665 $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+';
666 $timezone = abs($timezone);
667 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60;
668
669 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 // --------------------------------------------------------------------
673
674 /**
675 * Mime message
676 *
677 * @access private
678 * @return string
679 */
680 function _get_mime_message()
681 {
682 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
683 }
Barry Mienydd671972010-10-04 16:33:58 +0200684
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 // --------------------------------------------------------------------
686
687 /**
688 * Validate Email Address
689 *
690 * @access public
691 * @param string
692 * @return bool
693 */
694 function validate_email($email)
695 {
696 if ( ! is_array($email))
697 {
698 $this->_set_error_message('email_must_be_array');
699 return FALSE;
700 }
701
702 foreach ($email as $val)
703 {
704 if ( ! $this->valid_email($val))
705 {
706 $this->_set_error_message('email_invalid_address', $val);
707 return FALSE;
708 }
709 }
710
711 return TRUE;
712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // --------------------------------------------------------------------
715
716 /**
717 * Email Validation
718 *
719 * @access public
720 * @param string
721 * @return bool
722 */
723 function valid_email($address)
724 {
725 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
726 }
Barry Mienydd671972010-10-04 16:33:58 +0200727
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 // --------------------------------------------------------------------
729
730 /**
731 * Clean Extended Email Address: Joe Smith <joe@smith.com>
732 *
733 * @access public
734 * @param string
735 * @return string
736 */
737 function clean_email($email)
738 {
739 if ( ! is_array($email))
740 {
741 if (preg_match('/\<(.*)\>/', $email, $match))
742 {
Barry Mienydd671972010-10-04 16:33:58 +0200743 return $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 }
Barry Mienydd671972010-10-04 16:33:58 +0200745 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000746 {
Barry Mienydd671972010-10-04 16:33:58 +0200747 return $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 }
749 }
750
751 $clean_email = array();
752
753 foreach ($email as $addy)
754 {
755 if (preg_match( '/\<(.*)\>/', $addy, $match))
756 {
Barry Mienydd671972010-10-04 16:33:58 +0200757 $clean_email[] = $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000758 }
Barry Mienydd671972010-10-04 16:33:58 +0200759 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000760 {
Barry Mienydd671972010-10-04 16:33:58 +0200761 $clean_email[] = $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000762 }
763 }
764
765 return $clean_email;
766 }
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 // --------------------------------------------------------------------
769
770 /**
771 * Build alternative plain text message
772 *
773 * This function provides the raw message for use
774 * in plain-text headers of HTML-formatted emails.
775 * If the user hasn't specified his own alternative message
776 * it creates one by stripping the HTML
777 *
778 * @access private
779 * @return string
780 */
781 function _get_alt_message()
782 {
783 if ($this->alt_message != "")
784 {
785 return $this->word_wrap($this->alt_message, '76');
786 }
787
788 if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match))
789 {
790 $body = $match['1'];
791 }
792 else
793 {
794 $body = $this->_body;
795 }
796
797 $body = trim(strip_tags($body));
798 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
799 $body = str_replace("\t", "", $body);
800
801 for ($i = 20; $i >= 3; $i--)
802 {
803 $n = "";
804
805 for ($x = 1; $x <= $i; $x ++)
806 {
Barry Mienydd671972010-10-04 16:33:58 +0200807 $n .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 }
809
810 $body = str_replace($n, "\n\n", $body);
811 }
812
813 return $this->word_wrap($body, '76');
814 }
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 // --------------------------------------------------------------------
817
818 /**
819 * Word Wrap
820 *
821 * @access public
822 * @param string
823 * @param integer
824 * @return string
825 */
826 function word_wrap($str, $charlim = '')
827 {
828 // Se the character limit
829 if ($charlim == '')
830 {
831 $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
832 }
833
834 // Reduce multiple spaces
835 $str = preg_replace("| +|", " ", $str);
836
837 // Standardize newlines
838 if (strpos($str, "\r") !== FALSE)
839 {
840 $str = str_replace(array("\r\n", "\r"), "\n", $str);
841 }
842
843 // If the current word is surrounded by {unwrap} tags we'll
844 // strip the entire chunk and replace it with a marker.
845 $unwrap = array();
846 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
847 {
848 for ($i = 0; $i < count($matches['0']); $i++)
849 {
850 $unwrap[] = $matches['1'][$i];
851 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
852 }
853 }
854
855 // Use PHP's native function to do the initial wordwrap.
856 // We set the cut flag to FALSE so that any individual words that are
857 // too long get left alone. In the next step we'll deal with them.
858 $str = wordwrap($str, $charlim, "\n", FALSE);
859
860 // Split the string into individual lines of text and cycle through them
861 $output = "";
862 foreach (explode("\n", $str) as $line)
863 {
864 // Is the line within the allowed character count?
865 // If so we'll join it to the output and continue
866 if (strlen($line) <= $charlim)
867 {
868 $output .= $line.$this->newline;
869 continue;
870 }
871
872 $temp = '';
873 while((strlen($line)) > $charlim)
874 {
875 // If the over-length word is a URL we won't wrap it
876 if (preg_match("!\[url.+\]|://|wwww.!", $line))
877 {
878 break;
879 }
880
881 // Trim the word down
882 $temp .= substr($line, 0, $charlim-1);
883 $line = substr($line, $charlim-1);
884 }
885
886 // If $temp contains data it means we had to split up an over-length
887 // word into smaller chunks so we'll add it back to our current line
888 if ($temp != '')
889 {
890 $output .= $temp.$this->newline.$line;
891 }
892 else
893 {
894 $output .= $line;
895 }
896
897 $output .= $this->newline;
898 }
899
900 // Put our markers back
901 if (count($unwrap) > 0)
902 {
903 foreach ($unwrap as $key => $val)
904 {
905 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
906 }
907 }
908
909 return $output;
910 }
Barry Mienydd671972010-10-04 16:33:58 +0200911
Derek Allard2067d1a2008-11-13 22:59:24 +0000912 // --------------------------------------------------------------------
913
914 /**
915 * Build final headers
916 *
917 * @access private
918 * @param string
919 * @return string
920 */
921 function _build_headers()
922 {
923 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
924 $this->_set_header('X-Mailer', $this->useragent);
925 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
926 $this->_set_header('Message-ID', $this->_get_message_id());
927 $this->_set_header('Mime-Version', '1.0');
928 }
Barry Mienydd671972010-10-04 16:33:58 +0200929
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 // --------------------------------------------------------------------
931
932 /**
933 * Write Headers as a string
934 *
935 * @access private
936 * @return void
937 */
938 function _write_headers()
939 {
940 if ($this->protocol == 'mail')
941 {
942 $this->_subject = $this->_headers['Subject'];
943 unset($this->_headers['Subject']);
944 }
945
946 reset($this->_headers);
947 $this->_header_str = "";
948
949 foreach($this->_headers as $key => $val)
950 {
951 $val = trim($val);
952
953 if ($val != "")
954 {
955 $this->_header_str .= $key.": ".$val.$this->newline;
956 }
957 }
958
959 if ($this->_get_protocol() == 'mail')
960 {
Derek Jones1d890882009-02-10 20:32:14 +0000961 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000962 }
963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 // --------------------------------------------------------------------
966
967 /**
968 * Build Final Body and attachments
969 *
970 * @access private
971 * @return void
972 */
973 function _build_message()
974 {
975 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
976 {
977 $this->_body = $this->word_wrap($this->_body);
978 }
979
980 $this->_set_boundaries();
981 $this->_write_headers();
982
983 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -0500984 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000985
986 switch ($this->_get_content_type())
987 {
988 case 'plain' :
989
990 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
991 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
992
993 if ($this->_get_protocol() == 'mail')
994 {
995 $this->_header_str .= $hdr;
996 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
Brandon Jones485d7412010-11-09 16:38:17 -0500998 else
999 {
1000 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
1001 }
1002
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 return;
1004
1005 break;
1006 case 'html' :
1007
1008 if ($this->send_multipart === FALSE)
1009 {
1010 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1011 $hdr .= "Content-Transfer-Encoding: quoted-printable";
1012 }
1013 else
1014 {
Derek Jonesa45e7612009-02-10 18:33:01 +00001015 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001016
Brandon Jones485d7412010-11-09 16:38:17 -05001017 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1018 $body .= "--" . $this->_alt_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001019
Brandon Jones485d7412010-11-09 16:38:17 -05001020 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1021 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1022 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1023
1024 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1025 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 }
Brandon Jones485d7412010-11-09 16:38:17 -05001027
1028 $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
1029
1030
Derek Allard2067d1a2008-11-13 22:59:24 +00001031 if ($this->_get_protocol() == 'mail')
1032 {
1033 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001034 }
1035 else
1036 {
1037 $this->_finalbody = $hdr . $this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 }
1039
Derek Allard2067d1a2008-11-13 22:59:24 +00001040
1041 if ($this->send_multipart !== FALSE)
1042 {
Brandon Jones485d7412010-11-09 16:38:17 -05001043 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 }
1045
Derek Allard2067d1a2008-11-13 22:59:24 +00001046 return;
1047
1048 break;
1049 case 'plain-attach' :
1050
Derek Jonesa45e7612009-02-10 18:33:01 +00001051 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001052
1053 if ($this->_get_protocol() == 'mail')
1054 {
1055 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001056 }
1057
1058 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1059 $body .= "--" . $this->_atc_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001060
Brandon Jones485d7412010-11-09 16:38:17 -05001061 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1062 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001063
Brandon Jones485d7412010-11-09 16:38:17 -05001064 $body .= $this->_body . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001065
1066 break;
1067 case 'html-attach' :
1068
Derek Jonesa45e7612009-02-10 18:33:01 +00001069 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Brandon Jones485d7412010-11-09 16:38:17 -05001070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071 if ($this->_get_protocol() == 'mail')
1072 {
1073 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001074 }
1075
Brandon Jones485d7412010-11-09 16:38:17 -05001076 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1077 $body .= "--" . $this->_atc_boundary . $this->newline;
1078
1079 $body .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
1080 $body .= "--" . $this->_alt_boundary . $this->newline;
1081
1082 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1083 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1084 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1085
1086 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1087 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
1088
1089 $body .= $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
1090 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001091
1092 break;
1093 }
1094
1095 $attachment = array();
1096
1097 $z = 0;
1098
1099 for ($i=0; $i < count($this->_attach_name); $i++)
1100 {
1101 $filename = $this->_attach_name[$i];
1102 $basename = basename($filename);
1103 $ctype = $this->_attach_type[$i];
1104
1105 if ( ! file_exists($filename))
1106 {
1107 $this->_set_error_message('email_attachment_missing', $filename);
1108 return FALSE;
1109 }
1110
1111 $h = "--".$this->_atc_boundary.$this->newline;
1112 $h .= "Content-type: ".$ctype."; ";
1113 $h .= "name=\"".$basename."\"".$this->newline;
1114 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
1115 $h .= "Content-Transfer-Encoding: base64".$this->newline;
1116
1117 $attachment[$z++] = $h;
1118 $file = filesize($filename) +1;
1119
1120 if ( ! $fp = fopen($filename, FOPEN_READ))
1121 {
1122 $this->_set_error_message('email_attachment_unreadable', $filename);
1123 return FALSE;
1124 }
1125
1126 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1127 fclose($fp);
1128 }
1129
Brandon Jones485d7412010-11-09 16:38:17 -05001130 $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
1131
1132
Derek Allard2067d1a2008-11-13 22:59:24 +00001133 if ($this->_get_protocol() == 'mail')
1134 {
Brandon Jones485d7412010-11-09 16:38:17 -05001135 $this->_finalbody = $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 }
Brandon Jones485d7412010-11-09 16:38:17 -05001137 else
1138 {
1139 $this->_finalbody = $hdr . $body;
1140 }
1141
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 return;
1143 }
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 // --------------------------------------------------------------------
1146
1147 /**
1148 * Prep Quoted Printable
1149 *
1150 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1151 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1152 *
1153 * @access private
1154 * @param string
1155 * @param integer
1156 * @return string
1157 */
1158 function _prep_quoted_printable($str, $charlim = '')
1159 {
1160 // Set the character limit
1161 // Don't allow over 76, as that will make servers and MUAs barf
1162 // all over quoted-printable data
1163 if ($charlim == '' OR $charlim > '76')
1164 {
1165 $charlim = '76';
1166 }
1167
1168 // Reduce multiple spaces
1169 $str = preg_replace("| +|", " ", $str);
1170
1171 // kill nulls
1172 $str = preg_replace('/\x00+/', '', $str);
1173
1174 // Standardize newlines
1175 if (strpos($str, "\r") !== FALSE)
1176 {
1177 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1178 }
1179
1180 // We are intentionally wrapping so mail servers will encode characters
1181 // properly and MUAs will behave, so {unwrap} must go!
1182 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1183
1184 // Break into an array of lines
1185 $lines = explode("\n", $str);
1186
1187 $escape = '=';
1188 $output = '';
1189
1190 foreach ($lines as $line)
1191 {
1192 $length = strlen($line);
1193 $temp = '';
1194
1195 // Loop through each character in the line to add soft-wrap
1196 // characters at the end of a line " =\r\n" and add the newly
1197 // processed line(s) to the output (see comment on $crlf class property)
1198 for ($i = 0; $i < $length; $i++)
1199 {
1200 // Grab the next character
1201 $char = substr($line, $i, 1);
1202 $ascii = ord($char);
1203
1204 // Convert spaces and tabs but only if it's the end of the line
1205 if ($i == ($length - 1))
1206 {
1207 $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($ascii)) : $char;
1208 }
1209
1210 // encode = signs
1211 if ($ascii == '61')
1212 {
1213 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
1214 }
1215
1216 // If we're at the character limit, add the line to the output,
1217 // reset our temp variable, and keep on chuggin'
1218 if ((strlen($temp) + strlen($char)) >= $charlim)
1219 {
1220 $output .= $temp.$escape.$this->crlf;
1221 $temp = '';
1222 }
1223
1224 // Add the character to our temporary line
1225 $temp .= $char;
1226 }
1227
1228 // Add our completed line to the output
1229 $output .= $temp.$this->crlf;
1230 }
1231
1232 // get rid of extra CRLF tacked onto the end
1233 $output = substr($output, 0, strlen($this->crlf) * -1);
1234
1235 return $output;
1236 }
1237
1238 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001239
Derek Allard2067d1a2008-11-13 22:59:24 +00001240 /**
1241 * Prep Q Encoding
1242 *
1243 * Performs "Q Encoding" on a string for use in email headers. It's related
1244 * but not identical to quoted-printable, so it has its own method
1245 *
1246 * @access public
1247 * @param str
1248 * @param bool // set to TRUE for processing From: headers
1249 * @return str
1250 */
1251 function _prep_q_encoding($str, $from = FALSE)
1252 {
1253 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1254
1255 // Line length must not exceed 76 characters, so we adjust for
1256 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1257 $limit = 75 - 7 - strlen($this->charset);
1258
1259 // these special characters must be converted too
1260 $convert = array('_', '=', '?');
1261
1262 if ($from === TRUE)
1263 {
1264 $convert[] = ',';
1265 $convert[] = ';';
1266 }
1267
1268 $output = '';
1269 $temp = '';
1270
1271 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1272 {
1273 // Grab the next character
1274 $char = substr($str, $i, 1);
1275 $ascii = ord($char);
1276
1277 // convert ALL non-printable ASCII characters and our specials
1278 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1279 {
1280 $char = '='.dechex($ascii);
1281 }
1282
1283 // handle regular spaces a bit more compactly than =20
1284 if ($ascii == 32)
1285 {
1286 $char = '_';
1287 }
1288
1289 // If we're at the character limit, add the line to the output,
1290 // reset our temp variable, and keep on chuggin'
1291 if ((strlen($temp) + strlen($char)) >= $limit)
1292 {
1293 $output .= $temp.$this->crlf;
1294 $temp = '';
1295 }
1296
1297 // Add the character to our temporary line
1298 $temp .= $char;
1299 }
1300
1301 $str = $output.$temp;
1302
1303 // wrap each line with the shebang, charset, and transfer encoding
1304 // the preceding space on successive lines is required for header "folding"
1305 $str = trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
1306
1307 return $str;
1308 }
1309
1310 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001311
Derek Allard2067d1a2008-11-13 22:59:24 +00001312 /**
1313 * Send Email
1314 *
1315 * @access public
1316 * @return bool
1317 */
1318 function send()
1319 {
1320 if ($this->_replyto_flag == FALSE)
1321 {
1322 $this->reply_to($this->_headers['From']);
1323 }
1324
1325 if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
1326 ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
1327 ( ! isset($this->_headers['Cc'])))
1328 {
1329 $this->_set_error_message('email_no_recipients');
1330 return FALSE;
1331 }
1332
1333 $this->_build_headers();
1334
1335 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
1336 {
1337 if (count($this->_bcc_array) > $this->bcc_batch_size)
1338 return $this->batch_bcc_send();
1339 }
1340
1341 $this->_build_message();
1342
1343 if ( ! $this->_spool_email())
1344 {
1345 return FALSE;
1346 }
1347 else
1348 {
1349 return TRUE;
1350 }
1351 }
Barry Mienydd671972010-10-04 16:33:58 +02001352
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 // --------------------------------------------------------------------
1354
1355 /**
1356 * Batch Bcc Send. Sends groups of BCCs in batches
1357 *
1358 * @access public
1359 * @return bool
1360 */
1361 function batch_bcc_send()
1362 {
1363 $float = $this->bcc_batch_size -1;
1364
1365 $set = "";
1366
1367 $chunk = array();
1368
1369 for ($i = 0; $i < count($this->_bcc_array); $i++)
1370 {
1371 if (isset($this->_bcc_array[$i]))
1372 {
1373 $set .= ", ".$this->_bcc_array[$i];
1374 }
1375
1376 if ($i == $float)
1377 {
1378 $chunk[] = substr($set, 1);
1379 $float = $float + $this->bcc_batch_size;
1380 $set = "";
1381 }
1382
1383 if ($i == count($this->_bcc_array)-1)
1384 {
1385 $chunk[] = substr($set, 1);
1386 }
1387 }
1388
1389 for ($i = 0; $i < count($chunk); $i++)
1390 {
1391 unset($this->_headers['Bcc']);
1392 unset($bcc);
1393
1394 $bcc = $this->_str_to_array($chunk[$i]);
1395 $bcc = $this->clean_email($bcc);
1396
1397 if ($this->protocol != 'smtp')
1398 {
1399 $this->_set_header('Bcc', implode(", ", $bcc));
1400 }
1401 else
1402 {
1403 $this->_bcc_array = $bcc;
1404 }
1405
1406 $this->_build_message();
1407 $this->_spool_email();
1408 }
1409 }
Barry Mienydd671972010-10-04 16:33:58 +02001410
Derek Allard2067d1a2008-11-13 22:59:24 +00001411 // --------------------------------------------------------------------
1412
1413 /**
1414 * Unwrap special elements
1415 *
1416 * @access private
1417 * @return void
1418 */
1419 function _unwrap_specials()
1420 {
1421 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1422 }
Barry Mienydd671972010-10-04 16:33:58 +02001423
Derek Allard2067d1a2008-11-13 22:59:24 +00001424 // --------------------------------------------------------------------
1425
1426 /**
1427 * Strip line-breaks via callback
1428 *
1429 * @access private
1430 * @return string
1431 */
1432 function _remove_nl_callback($matches)
1433 {
1434 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1435 {
1436 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1437 }
1438
1439 return $matches[1];
1440 }
Barry Mienydd671972010-10-04 16:33:58 +02001441
Derek Allard2067d1a2008-11-13 22:59:24 +00001442 // --------------------------------------------------------------------
1443
1444 /**
1445 * Spool mail to the mail server
1446 *
1447 * @access private
1448 * @return bool
1449 */
1450 function _spool_email()
1451 {
1452 $this->_unwrap_specials();
1453
1454 switch ($this->_get_protocol())
1455 {
1456 case 'mail' :
1457
1458 if ( ! $this->_send_with_mail())
1459 {
1460 $this->_set_error_message('email_send_failure_phpmail');
1461 return FALSE;
1462 }
1463 break;
1464 case 'sendmail' :
1465
1466 if ( ! $this->_send_with_sendmail())
1467 {
1468 $this->_set_error_message('email_send_failure_sendmail');
1469 return FALSE;
1470 }
1471 break;
1472 case 'smtp' :
1473
1474 if ( ! $this->_send_with_smtp())
1475 {
1476 $this->_set_error_message('email_send_failure_smtp');
1477 return FALSE;
1478 }
1479 break;
1480
1481 }
1482
1483 $this->_set_error_message('email_sent', $this->_get_protocol());
1484 return TRUE;
1485 }
Barry Mienydd671972010-10-04 16:33:58 +02001486
Derek Allard2067d1a2008-11-13 22:59:24 +00001487 // --------------------------------------------------------------------
1488
1489 /**
1490 * Send using mail()
1491 *
1492 * @access private
1493 * @return bool
1494 */
1495 function _send_with_mail()
1496 {
1497 if ($this->_safe_mode == TRUE)
1498 {
1499 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
1500 {
1501 return FALSE;
1502 }
1503 else
1504 {
1505 return TRUE;
1506 }
1507 }
1508 else
1509 {
1510 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1511 // we've encountered servers that seem to require it to be in place.
Brandon Jones485d7412010-11-09 16:38:17 -05001512
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
1514 {
1515 return FALSE;
1516 }
1517 else
1518 {
1519 return TRUE;
1520 }
1521 }
1522 }
Barry Mienydd671972010-10-04 16:33:58 +02001523
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 // --------------------------------------------------------------------
1525
1526 /**
1527 * Send using Sendmail
1528 *
1529 * @access private
1530 * @return bool
1531 */
1532 function _send_with_sendmail()
1533 {
1534 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1535
Derek Jones4cefaa42009-04-29 19:13:56 +00001536 if ($fp === FALSE OR $fp === NULL)
1537 {
1538 // server probably has popen disabled, so nothing we can do to get a verbose error.
1539 return FALSE;
1540 }
Derek Jones71141ce2010-03-02 16:41:20 -06001541
Derek Jonesc630bcf2008-11-17 21:09:45 +00001542 fputs($fp, $this->_header_str);
1543 fputs($fp, $this->_finalbody);
1544
Barry Mienydd671972010-10-04 16:33:58 +02001545 $status = pclose($fp);
1546
Derek Jonesc630bcf2008-11-17 21:09:45 +00001547 if (version_compare(PHP_VERSION, '4.2.3') == -1)
1548 {
1549 $status = $status >> 8 & 0xFF;
Barry Mienydd671972010-10-04 16:33:58 +02001550 }
Derek Jones71141ce2010-03-02 16:41:20 -06001551
Derek Jones604873f2008-11-18 15:57:24 +00001552 if ($status != 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 {
Derek Jones604873f2008-11-18 15:57:24 +00001554 $this->_set_error_message('email_exit_status', $status);
Derek Allard2067d1a2008-11-13 22:59:24 +00001555 $this->_set_error_message('email_no_socket');
1556 return FALSE;
1557 }
1558
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 return TRUE;
1560 }
Barry Mienydd671972010-10-04 16:33:58 +02001561
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 // --------------------------------------------------------------------
1563
1564 /**
1565 * Send using SMTP
1566 *
1567 * @access private
1568 * @return bool
1569 */
1570 function _send_with_smtp()
1571 {
1572 if ($this->smtp_host == '')
1573 {
1574 $this->_set_error_message('email_no_hostname');
1575 return FALSE;
1576 }
1577
1578 $this->_smtp_connect();
1579 $this->_smtp_authenticate();
1580
1581 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1582
1583 foreach($this->_recipients as $val)
1584 {
1585 $this->_send_command('to', $val);
1586 }
1587
1588 if (count($this->_cc_array) > 0)
1589 {
1590 foreach($this->_cc_array as $val)
1591 {
1592 if ($val != "")
1593 {
1594 $this->_send_command('to', $val);
1595 }
1596 }
1597 }
1598
1599 if (count($this->_bcc_array) > 0)
1600 {
1601 foreach($this->_bcc_array as $val)
1602 {
1603 if ($val != "")
1604 {
1605 $this->_send_command('to', $val);
1606 }
1607 }
1608 }
1609
1610 $this->_send_command('data');
1611
1612 // perform dot transformation on any lines that begin with a dot
1613 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
1614
1615 $this->_send_data('.');
1616
1617 $reply = $this->_get_smtp_data();
1618
1619 $this->_set_error_message($reply);
1620
1621 if (strncmp($reply, '250', 3) != 0)
1622 {
1623 $this->_set_error_message('email_smtp_error', $reply);
1624 return FALSE;
1625 }
1626
1627 $this->_send_command('quit');
1628 return TRUE;
1629 }
Barry Mienydd671972010-10-04 16:33:58 +02001630
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 // --------------------------------------------------------------------
1632
1633 /**
1634 * SMTP Connect
1635 *
1636 * @access private
1637 * @param string
1638 * @return string
1639 */
1640 function _smtp_connect()
1641 {
1642 $this->_smtp_connect = fsockopen($this->smtp_host,
1643 $this->smtp_port,
1644 $errno,
1645 $errstr,
1646 $this->smtp_timeout);
1647
1648 if( ! is_resource($this->_smtp_connect))
1649 {
1650 $this->_set_error_message('email_smtp_error', $errno." ".$errstr);
1651 return FALSE;
1652 }
1653
1654 $this->_set_error_message($this->_get_smtp_data());
1655 return $this->_send_command('hello');
1656 }
Barry Mienydd671972010-10-04 16:33:58 +02001657
Derek Allard2067d1a2008-11-13 22:59:24 +00001658 // --------------------------------------------------------------------
1659
1660 /**
1661 * Send SMTP command
1662 *
1663 * @access private
1664 * @param string
1665 * @param string
1666 * @return string
1667 */
1668 function _send_command($cmd, $data = '')
1669 {
1670 switch ($cmd)
1671 {
1672 case 'hello' :
1673
1674 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1675 $this->_send_data('EHLO '.$this->_get_hostname());
1676 else
1677 $this->_send_data('HELO '.$this->_get_hostname());
1678
1679 $resp = 250;
1680 break;
1681 case 'from' :
1682
1683 $this->_send_data('MAIL FROM:<'.$data.'>');
1684
1685 $resp = 250;
1686 break;
1687 case 'to' :
1688
1689 $this->_send_data('RCPT TO:<'.$data.'>');
1690
1691 $resp = 250;
1692 break;
1693 case 'data' :
1694
1695 $this->_send_data('DATA');
1696
1697 $resp = 354;
1698 break;
1699 case 'quit' :
1700
1701 $this->_send_data('QUIT');
1702
1703 $resp = 221;
1704 break;
1705 }
1706
1707 $reply = $this->_get_smtp_data();
1708
1709 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1710
1711 if (substr($reply, 0, 3) != $resp)
1712 {
1713 $this->_set_error_message('email_smtp_error', $reply);
1714 return FALSE;
1715 }
1716
1717 if ($cmd == 'quit')
1718 {
1719 fclose($this->_smtp_connect);
1720 }
1721
1722 return TRUE;
1723 }
Barry Mienydd671972010-10-04 16:33:58 +02001724
Derek Allard2067d1a2008-11-13 22:59:24 +00001725 // --------------------------------------------------------------------
1726
1727 /**
1728 * SMTP Authenticate
1729 *
1730 * @access private
1731 * @return bool
1732 */
1733 function _smtp_authenticate()
1734 {
1735 if ( ! $this->_smtp_auth)
1736 {
1737 return TRUE;
1738 }
1739
1740 if ($this->smtp_user == "" AND $this->smtp_pass == "")
1741 {
1742 $this->_set_error_message('email_no_smtp_unpw');
1743 return FALSE;
1744 }
1745
1746 $this->_send_data('AUTH LOGIN');
1747
1748 $reply = $this->_get_smtp_data();
1749
1750 if (strncmp($reply, '334', 3) != 0)
1751 {
1752 $this->_set_error_message('email_failed_smtp_login', $reply);
1753 return FALSE;
1754 }
1755
1756 $this->_send_data(base64_encode($this->smtp_user));
1757
1758 $reply = $this->_get_smtp_data();
1759
1760 if (strncmp($reply, '334', 3) != 0)
1761 {
1762 $this->_set_error_message('email_smtp_auth_un', $reply);
1763 return FALSE;
1764 }
1765
1766 $this->_send_data(base64_encode($this->smtp_pass));
1767
1768 $reply = $this->_get_smtp_data();
1769
1770 if (strncmp($reply, '235', 3) != 0)
1771 {
1772 $this->_set_error_message('email_smtp_auth_pw', $reply);
1773 return FALSE;
1774 }
1775
1776 return TRUE;
1777 }
Barry Mienydd671972010-10-04 16:33:58 +02001778
Derek Allard2067d1a2008-11-13 22:59:24 +00001779 // --------------------------------------------------------------------
1780
1781 /**
1782 * Send SMTP data
1783 *
1784 * @access private
1785 * @return bool
1786 */
1787 function _send_data($data)
1788 {
1789 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1790 {
1791 $this->_set_error_message('email_smtp_data_failure', $data);
1792 return FALSE;
1793 }
1794 else
1795 {
1796 return TRUE;
1797 }
1798 }
Barry Mienydd671972010-10-04 16:33:58 +02001799
Derek Allard2067d1a2008-11-13 22:59:24 +00001800 // --------------------------------------------------------------------
1801
1802 /**
1803 * Get SMTP data
1804 *
1805 * @access private
1806 * @return string
1807 */
1808 function _get_smtp_data()
1809 {
1810 $data = "";
1811
1812 while ($str = fgets($this->_smtp_connect, 512))
1813 {
1814 $data .= $str;
1815
1816 if (substr($str, 3, 1) == " ")
1817 {
1818 break;
1819 }
1820 }
1821
1822 return $data;
1823 }
Barry Mienydd671972010-10-04 16:33:58 +02001824
Derek Allard2067d1a2008-11-13 22:59:24 +00001825 // --------------------------------------------------------------------
1826
1827 /**
1828 * Get Hostname
1829 *
1830 * @access private
1831 * @return string
1832 */
1833 function _get_hostname()
1834 {
1835 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
1836 }
Barry Mienydd671972010-10-04 16:33:58 +02001837
Derek Allard2067d1a2008-11-13 22:59:24 +00001838 // --------------------------------------------------------------------
1839
1840 /**
1841 * Get IP
1842 *
1843 * @access private
1844 * @return string
1845 */
1846 function _get_ip()
1847 {
1848 if ($this->_IP !== FALSE)
1849 {
1850 return $this->_IP;
1851 }
1852
1853 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1854 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1855 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1856
Barry Mienydd671972010-10-04 16:33:58 +02001857 if ($cip && $rip) $this->_IP = $cip;
Derek Allard2067d1a2008-11-13 22:59:24 +00001858 elseif ($rip) $this->_IP = $rip;
1859 elseif ($cip) $this->_IP = $cip;
1860 elseif ($fip) $this->_IP = $fip;
1861
Robin Sowell76b369e2010-03-19 11:15:28 -04001862 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001863 {
1864 $x = explode(',', $this->_IP);
1865 $this->_IP = end($x);
1866 }
1867
1868 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
1869 {
1870 $this->_IP = '0.0.0.0';
1871 }
1872
1873 unset($cip);
1874 unset($rip);
1875 unset($fip);
1876
1877 return $this->_IP;
1878 }
Barry Mienydd671972010-10-04 16:33:58 +02001879
Derek Allard2067d1a2008-11-13 22:59:24 +00001880 // --------------------------------------------------------------------
1881
1882 /**
1883 * Get Debug Message
1884 *
1885 * @access public
1886 * @return string
1887 */
1888 function print_debugger()
1889 {
1890 $msg = '';
1891
1892 if (count($this->_debug_msg) > 0)
1893 {
1894 foreach ($this->_debug_msg as $val)
1895 {
1896 $msg .= $val;
1897 }
1898 }
1899
1900 $msg .= "<pre>".$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
1901 return $msg;
1902 }
Barry Mienydd671972010-10-04 16:33:58 +02001903
Derek Allard2067d1a2008-11-13 22:59:24 +00001904 // --------------------------------------------------------------------
1905
1906 /**
1907 * Set Message
1908 *
1909 * @access private
1910 * @param string
1911 * @return string
1912 */
1913 function _set_error_message($msg, $val = '')
1914 {
1915 $CI =& get_instance();
1916 $CI->lang->load('email');
1917
1918 if (FALSE === ($line = $CI->lang->line($msg)))
1919 {
1920 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1921 }
1922 else
1923 {
1924 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1925 }
1926 }
Barry Mienydd671972010-10-04 16:33:58 +02001927
Derek Allard2067d1a2008-11-13 22:59:24 +00001928 // --------------------------------------------------------------------
1929
1930 /**
1931 * Mime Types
1932 *
1933 * @access private
1934 * @param string
1935 * @return string
1936 */
1937 function _mime_types($ext = "")
1938 {
1939 $mimes = array( 'hqx' => 'application/mac-binhex40',
1940 'cpt' => 'application/mac-compactpro',
1941 'doc' => 'application/msword',
1942 'bin' => 'application/macbinary',
1943 'dms' => 'application/octet-stream',
1944 'lha' => 'application/octet-stream',
1945 'lzh' => 'application/octet-stream',
1946 'exe' => 'application/octet-stream',
1947 'class' => 'application/octet-stream',
1948 'psd' => 'application/octet-stream',
1949 'so' => 'application/octet-stream',
1950 'sea' => 'application/octet-stream',
1951 'dll' => 'application/octet-stream',
1952 'oda' => 'application/oda',
1953 'pdf' => 'application/pdf',
1954 'ai' => 'application/postscript',
1955 'eps' => 'application/postscript',
1956 'ps' => 'application/postscript',
1957 'smi' => 'application/smil',
1958 'smil' => 'application/smil',
1959 'mif' => 'application/vnd.mif',
1960 'xls' => 'application/vnd.ms-excel',
1961 'ppt' => 'application/vnd.ms-powerpoint',
1962 'wbxml' => 'application/vnd.wap.wbxml',
1963 'wmlc' => 'application/vnd.wap.wmlc',
1964 'dcr' => 'application/x-director',
1965 'dir' => 'application/x-director',
1966 'dxr' => 'application/x-director',
1967 'dvi' => 'application/x-dvi',
1968 'gtar' => 'application/x-gtar',
1969 'php' => 'application/x-httpd-php',
1970 'php4' => 'application/x-httpd-php',
1971 'php3' => 'application/x-httpd-php',
1972 'phtml' => 'application/x-httpd-php',
1973 'phps' => 'application/x-httpd-php-source',
1974 'js' => 'application/x-javascript',
1975 'swf' => 'application/x-shockwave-flash',
1976 'sit' => 'application/x-stuffit',
1977 'tar' => 'application/x-tar',
1978 'tgz' => 'application/x-tar',
1979 'xhtml' => 'application/xhtml+xml',
1980 'xht' => 'application/xhtml+xml',
1981 'zip' => 'application/zip',
1982 'mid' => 'audio/midi',
1983 'midi' => 'audio/midi',
1984 'mpga' => 'audio/mpeg',
1985 'mp2' => 'audio/mpeg',
1986 'mp3' => 'audio/mpeg',
1987 'aif' => 'audio/x-aiff',
1988 'aiff' => 'audio/x-aiff',
1989 'aifc' => 'audio/x-aiff',
1990 'ram' => 'audio/x-pn-realaudio',
1991 'rm' => 'audio/x-pn-realaudio',
1992 'rpm' => 'audio/x-pn-realaudio-plugin',
1993 'ra' => 'audio/x-realaudio',
1994 'rv' => 'video/vnd.rn-realvideo',
1995 'wav' => 'audio/x-wav',
1996 'bmp' => 'image/bmp',
1997 'gif' => 'image/gif',
1998 'jpeg' => 'image/jpeg',
1999 'jpg' => 'image/jpeg',
2000 'jpe' => 'image/jpeg',
2001 'png' => 'image/png',
2002 'tiff' => 'image/tiff',
2003 'tif' => 'image/tiff',
2004 'css' => 'text/css',
2005 'html' => 'text/html',
2006 'htm' => 'text/html',
2007 'shtml' => 'text/html',
2008 'txt' => 'text/plain',
2009 'text' => 'text/plain',
2010 'log' => 'text/plain',
2011 'rtx' => 'text/richtext',
2012 'rtf' => 'text/rtf',
2013 'xml' => 'text/xml',
2014 'xsl' => 'text/xml',
2015 'mpeg' => 'video/mpeg',
2016 'mpg' => 'video/mpeg',
2017 'mpe' => 'video/mpeg',
2018 'qt' => 'video/quicktime',
2019 'mov' => 'video/quicktime',
2020 'avi' => 'video/x-msvideo',
2021 'movie' => 'video/x-sgi-movie',
2022 'doc' => 'application/msword',
2023 'word' => 'application/msword',
2024 'xl' => 'application/excel',
2025 'eml' => 'message/rfc822'
2026 );
2027
2028 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
2029 }
2030
2031}
2032// END CI_Email class
2033
2034/* End of file Email.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00002035/* Location: ./system/libraries/Email.php */