1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * CodeIgniter Email Class
\r
21 * Permits email to be sent using Mail, Sendmail, or SMTP.
\r
23 * @package CodeIgniter
\r
24 * @subpackage Libraries
\r
25 * @category Libraries
\r
26 * @author ExpressionEngine Dev Team
\r
27 * @link http://codeigniter.com/user_guide/libraries/email.html
\r
31 var $useragent = "CodeIgniter";
\r
32 var $mailpath = "/usr/sbin/sendmail"; // Sendmail path
\r
33 var $protocol = "mail"; // mail/sendmail/smtp
\r
34 var $smtp_host = ""; // SMTP Server. Example: mail.earthlink.net
\r
35 var $smtp_user = ""; // SMTP Username
\r
36 var $smtp_pass = ""; // SMTP Password
\r
37 var $smtp_port = "25"; // SMTP Port
\r
38 var $smtp_timeout = 5; // SMTP Timeout in seconds
\r
39 var $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
\r
40 var $wrapchars = "76"; // Number of characters to wrap at.
\r
41 var $mailtype = "text"; // text/html Defines email formatting
\r
42 var $charset = "utf-8"; // Default char set: iso-8859-1 or us-ascii
\r
43 var $multipart = "mixed"; // "mixed" (in the body) or "related" (separate)
\r
44 var $alt_message = ''; // Alternative message for HTML emails
\r
45 var $validate = FALSE; // TRUE/FALSE. Enables email validation
\r
46 var $priority = "3"; // Default priority (1 - 5)
\r
47 var $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
\r
48 var $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
\r
49 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
\r
50 // distasteful, is the only thing that seems to work for all environments.
\r
51 var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
\r
52 var $bcc_batch_mode = FALSE; // TRUE/FALSE Turns on/off Bcc batch feature
\r
53 var $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
\r
54 var $_safe_mode = FALSE;
\r
57 var $_finalbody = "";
\r
58 var $_alt_boundary = "";
\r
59 var $_atc_boundary = "";
\r
60 var $_header_str = "";
\r
61 var $_smtp_connect = "";
\r
62 var $_encoding = "8bit";
\r
64 var $_smtp_auth = FALSE;
\r
65 var $_replyto_flag = FALSE;
\r
66 var $_debug_msg = array();
\r
67 var $_recipients = array();
\r
68 var $_cc_array = array();
\r
69 var $_bcc_array = array();
\r
70 var $_headers = array();
\r
71 var $_attach_name = array();
\r
72 var $_attach_type = array();
\r
73 var $_attach_disp = array();
\r
74 var $_protocols = array('mail', 'sendmail', 'smtp');
\r
75 var $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
\r
76 var $_bit_depths = array('7bit', '8bit');
\r
77 var $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
\r
81 * Constructor - Sets Email Preferences
\r
83 * The constructor can be passed an array of config values
\r
85 function CI_Email($config = array())
\r
87 if (count($config) > 0)
\r
89 $this->initialize($config);
\r
93 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
\r
94 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
\r
97 log_message('debug', "Email Class Initialized");
\r
100 // --------------------------------------------------------------------
\r
103 * Initialize preferences
\r
109 function initialize($config = array())
\r
112 foreach ($config as $key => $val)
\r
114 if (isset($this->$key))
\r
116 $method = 'set_'.$key;
\r
118 if (method_exists($this, $method))
\r
120 $this->$method($val);
\r
124 $this->$key = $val;
\r
129 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
\r
130 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
\r
133 // --------------------------------------------------------------------
\r
136 * Initialize the Email Data
\r
141 function clear($clear_attachments = FALSE)
\r
143 $this->_subject = "";
\r
145 $this->_finalbody = "";
\r
146 $this->_header_str = "";
\r
147 $this->_replyto_flag = FALSE;
\r
148 $this->_recipients = array();
\r
149 $this->_headers = array();
\r
150 $this->_debug_msg = array();
\r
152 $this->_set_header('User-Agent', $this->useragent);
\r
153 $this->_set_header('Date', $this->_set_date());
\r
155 if ($clear_attachments !== FALSE)
\r
157 $this->_attach_name = array();
\r
158 $this->_attach_type = array();
\r
159 $this->_attach_disp = array();
\r
163 // --------------------------------------------------------------------
\r
173 function from($from, $name = '')
\r
175 if (preg_match( '/\<(.*)\>/', $from, $match))
\r
177 $from = $match['1'];
\r
180 if ($this->validate)
\r
182 $this->validate_email($this->_str_to_array($from));
\r
185 if ($name != '' && strncmp($name, '"', 1) != 0)
\r
187 $name = '"'.$name.'"';
\r
190 $this->_set_header('From', $name.' <'.$from.'>');
\r
191 $this->_set_header('Return-Path', '<'.$from.'>');
\r
194 // --------------------------------------------------------------------
\r
204 function reply_to($replyto, $name = '')
\r
206 if (preg_match( '/\<(.*)\>/', $replyto, $match))
\r
208 $replyto = $match['1'];
\r
211 if ($this->validate)
\r
213 $this->validate_email($this->_str_to_array($replyto));
\r
221 if (strncmp($name, '"', 1) != 0)
\r
223 $name = '"'.$name.'"';
\r
226 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
\r
227 $this->_replyto_flag = TRUE;
\r
230 // --------------------------------------------------------------------
\r
241 $to = $this->_str_to_array($to);
\r
242 $to = $this->clean_email($to);
\r
244 if ($this->validate)
\r
246 $this->validate_email($to);
\r
249 if ($this->_get_protocol() != 'mail')
\r
251 $this->_set_header('To', implode(", ", $to));
\r
254 switch ($this->_get_protocol())
\r
256 case 'smtp' : $this->_recipients = $to;
\r
258 case 'sendmail' : $this->_recipients = implode(", ", $to);
\r
260 case 'mail' : $this->_recipients = implode(", ", $to);
\r
265 // --------------------------------------------------------------------
\r
276 $cc = $this->_str_to_array($cc);
\r
277 $cc = $this->clean_email($cc);
\r
279 if ($this->validate)
\r
281 $this->validate_email($cc);
\r
284 $this->_set_header('Cc', implode(", ", $cc));
\r
286 if ($this->_get_protocol() == "smtp")
\r
288 $this->_cc_array = $cc;
\r
292 // --------------------------------------------------------------------
\r
302 function bcc($bcc, $limit = '')
\r
304 if ($limit != '' && is_numeric($limit))
\r
306 $this->bcc_batch_mode = TRUE;
\r
307 $this->bcc_batch_size = $limit;
\r
310 $bcc = $this->_str_to_array($bcc);
\r
311 $bcc = $this->clean_email($bcc);
\r
313 if ($this->validate)
\r
315 $this->validate_email($bcc);
\r
318 if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
\r
320 $this->_bcc_array = $bcc;
\r
324 $this->_set_header('Bcc', implode(", ", $bcc));
\r
328 // --------------------------------------------------------------------
\r
331 * Set Email Subject
\r
337 function subject($subject)
\r
339 if (strpos($subject, "\r") !== FALSE OR strpos($subject, "\n") !== FALSE)
\r
341 $subject = str_replace(array("\r\n", "\r", "\n"), '', $subject);
\r
344 if (strpos($subject, "\t"))
\r
346 $subject = str_replace("\t", ' ', $subject);
\r
349 $this->_set_header('Subject', trim($subject));
\r
352 // --------------------------------------------------------------------
\r
361 function message($body)
\r
363 $this->_body = stripslashes(rtrim(str_replace("\r", "", $body)));
\r
366 // --------------------------------------------------------------------
\r
369 * Assign file attachments
\r
375 function attach($filename, $disposition = 'attachment')
\r
377 $this->_attach_name[] = $filename;
\r
378 $this->_attach_type[] = $this->_mime_types(next(explode('.', basename($filename))));
\r
379 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
\r
382 // --------------------------------------------------------------------
\r
385 * Add a Header Item
\r
392 function _set_header($header, $value)
\r
394 $this->_headers[$header] = $value;
\r
397 // --------------------------------------------------------------------
\r
400 * Convert a String to an Array
\r
406 function _str_to_array($email)
\r
408 if ( ! is_array($email))
\r
410 if (strpos($email, ',') !== FALSE)
\r
412 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
\r
416 $email = trim($email);
\r
417 settype($email, "array");
\r
423 // --------------------------------------------------------------------
\r
426 * Set Multipart Value
\r
432 function set_alt_message($str = '')
\r
434 $this->alt_message = ($str == '') ? '' : $str;
\r
437 // --------------------------------------------------------------------
\r
446 function set_mailtype($type = 'text')
\r
448 $this->mailtype = ($type == 'html') ? 'html' : 'text';
\r
451 // --------------------------------------------------------------------
\r
460 function set_wordwrap($wordwrap = TRUE)
\r
462 $this->wordwrap = ($wordwrap === FALSE) ? FALSE : TRUE;
\r
465 // --------------------------------------------------------------------
\r
474 function set_protocol($protocol = 'mail')
\r
476 $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
\r
479 // --------------------------------------------------------------------
\r
488 function set_priority($n = 3)
\r
490 if ( ! is_numeric($n))
\r
492 $this->priority = 3;
\r
496 if ($n < 1 OR $n > 5)
\r
498 $this->priority = 3;
\r
502 $this->priority = $n;
\r
505 // --------------------------------------------------------------------
\r
508 * Set Newline Character
\r
514 function set_newline($newline = "\n")
\r
516 if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
\r
518 $this->newline = "\n";
\r
522 $this->newline = $newline;
\r
525 // --------------------------------------------------------------------
\r
534 function set_crlf($crlf = "\n")
\r
536 if ($crlf != "\n" AND $crlf != "\r\n" AND $crlf != "\r")
\r
538 $this->crlf = "\n";
\r
542 $this->crlf = $crlf;
\r
545 // --------------------------------------------------------------------
\r
548 * Set Message Boundary
\r
553 function _set_boundaries()
\r
555 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
\r
556 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
\r
559 // --------------------------------------------------------------------
\r
562 * Get the Message ID
\r
567 function _get_message_id()
\r
569 $from = $this->_headers['Return-Path'];
\r
570 $from = str_replace(">", "", $from);
\r
571 $from = str_replace("<", "", $from);
\r
573 return "<".uniqid('').strstr($from, '@').">";
\r
576 // --------------------------------------------------------------------
\r
579 * Get Mail Protocol
\r
585 function _get_protocol($return = TRUE)
\r
587 $this->protocol = strtolower($this->protocol);
\r
588 $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
\r
590 if ($return == TRUE)
\r
592 return $this->protocol;
\r
596 // --------------------------------------------------------------------
\r
599 * Get Mail Encoding
\r
605 function _get_encoding($return = TRUE)
\r
607 $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding;
\r
609 foreach ($this->_base_charsets as $charset)
\r
611 if (strncmp($charset, $this->charset, strlen($charset)) == 0)
\r
613 $this->_encoding = '7bit';
\r
617 if ($return == TRUE)
\r
619 return $this->_encoding;
\r
623 // --------------------------------------------------------------------
\r
626 * Get content type (text/html/attachment)
\r
631 function _get_content_type()
\r
633 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
\r
637 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
\r
639 return 'html-attach';
\r
641 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
\r
643 return 'plain-attach';
\r
651 // --------------------------------------------------------------------
\r
659 function _set_date()
\r
661 $timezone = date("Z");
\r
662 $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+';
\r
663 $timezone = abs($timezone);
\r
664 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60;
\r
666 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
\r
669 // --------------------------------------------------------------------
\r
677 function _get_mime_message()
\r
679 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
\r
682 // --------------------------------------------------------------------
\r
685 * Validate Email Address
\r
691 function validate_email($email)
\r
693 if ( ! is_array($email))
\r
695 $this->_set_error_message('email_must_be_array');
\r
699 foreach ($email as $val)
\r
701 if ( ! $this->valid_email($val))
\r
703 $this->_set_error_message('email_invalid_address', $val);
\r
709 // --------------------------------------------------------------------
\r
718 function valid_email($address)
\r
720 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
\r
723 // --------------------------------------------------------------------
\r
726 * Clean Extended Email Address: Joe Smith <joe@smith.com>
\r
732 function clean_email($email)
\r
734 if ( ! is_array($email))
\r
736 if (preg_match('/\<(.*)\>/', $email, $match))
\r
738 return $match['1'];
\r
746 $clean_email = array();
\r
748 foreach ($email as $addy)
\r
750 if (preg_match( '/\<(.*)\>/', $addy, $match))
\r
752 $clean_email[] = $match['1'];
\r
756 $clean_email[] = $addy;
\r
760 return $clean_email;
\r
763 // --------------------------------------------------------------------
\r
766 * Build alternative plain text message
\r
768 * This function provides the raw message for use
\r
769 * in plain-text headers of HTML-formatted emails.
\r
770 * If the user hasn't specified his own alternative message
\r
771 * it creates one by stripping the HTML
\r
776 function _get_alt_message()
\r
778 if ($this->alt_message != "")
\r
780 return $this->word_wrap($this->alt_message, '76');
\r
783 if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match))
\r
785 $body = $match['1'];
\r
789 $body = $this->_body;
\r
792 $body = trim(strip_tags($body));
\r
793 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
\r
794 $body = str_replace("\t", "", $body);
\r
796 for ($i = 20; $i >= 3; $i--)
\r
800 for ($x = 1; $x <= $i; $x ++)
\r
805 $body = str_replace($n, "\n\n", $body);
\r
808 return $this->word_wrap($body, '76');
\r
811 // --------------------------------------------------------------------
\r
821 function word_wrap($str, $charlim = '')
\r
823 // Se the character limit
\r
824 if ($charlim == '')
\r
826 $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
\r
829 // Reduce multiple spaces
\r
830 $str = preg_replace("| +|", " ", $str);
\r
832 // Standardize newlines
\r
833 if (strpos($str, "\r") !== FALSE)
\r
835 $str = str_replace(array("\r\n", "\r"), "\n", $str);
\r
838 // If the current word is surrounded by {unwrap} tags we'll
\r
839 // strip the entire chunk and replace it with a marker.
\r
841 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
\r
843 for ($i = 0; $i < count($matches['0']); $i++)
\r
845 $unwrap[] = $matches['1'][$i];
\r
846 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
\r
850 // Use PHP's native function to do the initial wordwrap.
\r
851 // We set the cut flag to FALSE so that any individual words that are
\r
852 // too long get left alone. In the next step we'll deal with them.
\r
853 $str = wordwrap($str, $charlim, "\n", FALSE);
\r
855 // Split the string into individual lines of text and cycle through them
\r
857 foreach (explode("\n", $str) as $line)
\r
859 // Is the line within the allowed character count?
\r
860 // If so we'll join it to the output and continue
\r
861 if (strlen($line) <= $charlim)
\r
863 $output .= $line.$this->newline;
\r
868 while((strlen($line)) > $charlim)
\r
870 // If the over-length word is a URL we won't wrap it
\r
871 if (preg_match("!\[url.+\]|://|wwww.!", $line))
\r
876 // Trim the word down
\r
877 $temp .= substr($line, 0, $charlim-1);
\r
878 $line = substr($line, $charlim-1);
\r
881 // If $temp contains data it means we had to split up an over-length
\r
882 // word into smaller chunks so we'll add it back to our current line
\r
885 $output .= $temp.$this->newline.$line;
\r
892 $output .= $this->newline;
\r
895 // Put our markers back
\r
896 if (count($unwrap) > 0)
\r
898 foreach ($unwrap as $key => $val)
\r
900 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
\r
907 // --------------------------------------------------------------------
\r
910 * Build final headers
\r
916 function _build_headers()
\r
918 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
\r
919 $this->_set_header('X-Mailer', $this->useragent);
\r
920 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
\r
921 $this->_set_header('Message-ID', $this->_get_message_id());
\r
922 $this->_set_header('Mime-Version', '1.0');
\r
925 // --------------------------------------------------------------------
\r
928 * Write Headers as a string
\r
933 function _write_headers()
\r
935 if ($this->protocol == 'mail')
\r
937 $this->_subject = $this->_headers['Subject'];
\r
938 unset($this->_headers['Subject']);
\r
941 reset($this->_headers);
\r
942 $this->_header_str = "";
\r
944 foreach($this->_headers as $key => $val)
\r
950 $this->_header_str .= $key.": ".$val.$this->newline;
\r
954 if ($this->_get_protocol() == 'mail')
\r
956 $this->_header_str = substr($this->_header_str, 0, -1);
\r
960 // --------------------------------------------------------------------
\r
963 * Build Final Body and attachments
\r
968 function _build_message()
\r
970 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
\r
972 $this->_body = $this->word_wrap($this->_body);
\r
975 $this->_set_boundaries();
\r
976 $this->_write_headers();
\r
978 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
\r
980 switch ($this->_get_content_type())
\r
984 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
\r
985 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
\r
987 if ($this->_get_protocol() == 'mail')
\r
989 $this->_header_str .= $hdr;
\r
990 $this->_finalbody = $this->_body;
\r
995 $hdr .= $this->newline . $this->newline . $this->_body;
\r
997 $this->_finalbody = $hdr;
\r
1003 if ($this->send_multipart === FALSE)
\r
1005 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
\r
1006 $hdr .= "Content-Transfer-Encoding: quoted-printable";
\r
1010 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline;
\r
1011 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
\r
1012 $hdr .= "--" . $this->_alt_boundary . $this->newline;
\r
1014 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
\r
1015 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
\r
1016 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
\r
1018 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
\r
1019 $hdr .= "Content-Transfer-Encoding: quoted-printable";
\r
1022 $this->_body = $this->_prep_quoted_printable($this->_body);
\r
1024 if ($this->_get_protocol() == 'mail')
\r
1026 $this->_header_str .= $hdr;
\r
1027 $this->_finalbody = $this->_body . $this->newline . $this->newline;
\r
1029 if ($this->send_multipart !== FALSE)
\r
1031 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
\r
1037 $hdr .= $this->newline . $this->newline;
\r
1038 $hdr .= $this->_body . $this->newline . $this->newline;
\r
1040 if ($this->send_multipart !== FALSE)
\r
1042 $hdr .= "--" . $this->_alt_boundary . "--";
\r
1045 $this->_finalbody = $hdr;
\r
1049 case 'plain-attach' :
\r
1051 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
\r
1052 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
\r
1053 $hdr .= "--" . $this->_atc_boundary . $this->newline;
\r
1055 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
\r
1056 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
\r
1058 if ($this->_get_protocol() == 'mail')
\r
1060 $this->_header_str .= $hdr;
\r
1062 $body = $this->_body . $this->newline . $this->newline;
\r
1065 $hdr .= $this->newline . $this->newline;
\r
1066 $hdr .= $this->_body . $this->newline . $this->newline;
\r
1069 case 'html-attach' :
\r
1071 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
\r
1072 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
\r
1073 $hdr .= "--" . $this->_atc_boundary . $this->newline;
\r
1075 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
\r
1076 $hdr .= "--" . $this->_alt_boundary . $this->newline;
\r
1078 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
\r
1079 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
\r
1080 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
\r
1082 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
\r
1083 $hdr .= "Content-Transfer-Encoding: quoted-printable";
\r
1085 $this->_body = $this->_prep_quoted_printable($this->_body);
\r
1087 if ($this->_get_protocol() == 'mail')
\r
1089 $this->_header_str .= $hdr;
\r
1091 $body = $this->_body . $this->newline . $this->newline;
\r
1092 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
\r
1095 $hdr .= $this->newline . $this->newline;
\r
1096 $hdr .= $this->_body . $this->newline . $this->newline;
\r
1097 $hdr .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
\r
1102 $attachment = array();
\r
1106 for ($i=0; $i < count($this->_attach_name); $i++)
\r
1108 $filename = $this->_attach_name[$i];
\r
1109 $basename = basename($filename);
\r
1110 $ctype = $this->_attach_type[$i];
\r
1112 if ( ! file_exists($filename))
\r
1114 $this->_set_error_message('email_attachment_missing', $filename);
\r
1118 $h = "--".$this->_atc_boundary.$this->newline;
\r
1119 $h .= "Content-type: ".$ctype."; ";
\r
1120 $h .= "name=\"".$basename."\"".$this->newline;
\r
1121 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
\r
1122 $h .= "Content-Transfer-Encoding: base64".$this->newline;
\r
1124 $attachment[$z++] = $h;
\r
1125 $file = filesize($filename) +1;
\r
1127 if ( ! $fp = fopen($filename, FOPEN_READ))
\r
1129 $this->_set_error_message('email_attachment_unreadable', $filename);
\r
1133 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
\r
1137 if ($this->_get_protocol() == 'mail')
\r
1139 $this->_finalbody = $body . implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
\r
1144 $this->_finalbody = $hdr.implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
\r
1149 // --------------------------------------------------------------------
\r
1152 * Prep Quoted Printable
\r
1154 * Prepares string for Quoted-Printable Content-Transfer-Encoding
\r
1155 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
\r
1162 function _prep_quoted_printable($str, $charlim = '')
\r
1164 // Set the character limit
\r
1165 // Don't allow over 76, as that will make servers and MUAs barf
\r
1166 // all over quoted-printable data
\r
1167 if ($charlim == '' OR $charlim > '76')
\r
1172 // Reduce multiple spaces
\r
1173 $str = preg_replace("| +|", " ", $str);
\r
1176 $str = preg_replace('/\x00+/', '', $str);
\r
1178 // Standardize newlines
\r
1179 if (strpos($str, "\r") !== FALSE)
\r
1181 $str = str_replace(array("\r\n", "\r"), "\n", $str);
\r
1184 // We are intentionally wrapping so mail servers will encode characters
\r
1185 // properly and MUAs will behave, so {unwrap} must go!
\r
1186 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
\r
1188 // Break into an array of lines
\r
1189 $lines = explode("\n", $str);
\r
1194 foreach ($lines as $line)
\r
1196 $length = strlen($line);
\r
1199 // Loop through each character in the line to add soft-wrap
\r
1200 // characters at the end of a line " =\r\n" and add the newly
\r
1201 // processed line(s) to the output (see comment on $crlf class property)
\r
1202 for ($i = 0; $i < $length; $i++)
\r
1204 // Grab the next character
\r
1205 $char = substr($line, $i, 1);
\r
1206 $ascii = ord($char);
\r
1208 // Convert spaces and tabs but only if it's the end of the line
\r
1209 if ($i == ($length - 1))
\r
1211 $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($ascii)) : $char;
\r
1215 if ($ascii == '61')
\r
1217 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
\r
1220 // If we're at the character limit, add the line to the output,
\r
1221 // reset our temp variable, and keep on chuggin'
\r
1222 if ((strlen($temp) + strlen($char)) >= $charlim)
\r
1224 $output .= $temp.$escape.$this->crlf;
\r
1228 // Add the character to our temporary line
\r
1232 // Add our completed line to the output
\r
1233 $output .= $temp.$this->crlf;
\r
1236 // get rid of extra CRLF tacked onto the end
\r
1237 $output = substr($output, 0, strlen($this->crlf) * -1);
\r
1242 // --------------------------------------------------------------------
\r
1252 if ($this->_replyto_flag == FALSE)
\r
1254 $this->reply_to($this->_headers['From']);
\r
1257 if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
\r
1258 ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
\r
1259 ( ! isset($this->_headers['Cc'])))
\r
1261 $this->_set_error_message('email_no_recipients');
\r
1265 $this->_build_headers();
\r
1267 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
\r
1269 if (count($this->_bcc_array) > $this->bcc_batch_size)
\r
1270 return $this->batch_bcc_send();
\r
1273 $this->_build_message();
\r
1275 if ( ! $this->_spool_email())
\r
1285 // --------------------------------------------------------------------
\r
1288 * Batch Bcc Send. Sends groups of BCCs in batches
\r
1293 function batch_bcc_send()
\r
1295 $float = $this->bcc_batch_size -1;
\r
1301 for ($i = 0; $i < count($this->_bcc_array); $i++)
\r
1303 if (isset($this->_bcc_array[$i]))
\r
1305 $set .= ", ".$this->_bcc_array[$i];
\r
1310 $chunk[] = substr($set, 1);
\r
1311 $float = $float + $this->bcc_batch_size;
\r
1315 if ($i == count($this->_bcc_array)-1)
\r
1317 $chunk[] = substr($set, 1);
\r
1321 for ($i = 0; $i < count($chunk); $i++)
\r
1323 unset($this->_headers['Bcc']);
\r
1326 $bcc = $this->_str_to_array($chunk[$i]);
\r
1327 $bcc = $this->clean_email($bcc);
\r
1329 if ($this->protocol != 'smtp')
\r
1331 $this->_set_header('Bcc', implode(", ", $bcc));
\r
1335 $this->_bcc_array = $bcc;
\r
1338 $this->_build_message();
\r
1339 $this->_spool_email();
\r
1343 // --------------------------------------------------------------------
\r
1346 * Unwrap special elements
\r
1351 function _unwrap_specials()
\r
1353 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
\r
1356 // --------------------------------------------------------------------
\r
1359 * Strip line-breaks via callback
\r
1364 function _remove_nl_callback($matches)
\r
1366 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
\r
1368 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
\r
1371 return $matches[1];
\r
1374 // --------------------------------------------------------------------
\r
1377 * Spool mail to the mail server
\r
1382 function _spool_email()
\r
1384 $this->_unwrap_specials();
\r
1386 switch ($this->_get_protocol())
\r
1390 if ( ! $this->_send_with_mail())
\r
1392 $this->_set_error_message('email_send_failure_phpmail');
\r
1398 if ( ! $this->_send_with_sendmail())
\r
1400 $this->_set_error_message('email_send_failure_sendmail');
\r
1406 if ( ! $this->_send_with_smtp())
\r
1408 $this->_set_error_message('email_send_failure_smtp');
\r
1415 $this->_set_error_message('email_sent', $this->_get_protocol());
\r
1419 // --------------------------------------------------------------------
\r
1422 * Send using mail()
\r
1427 function _send_with_mail()
\r
1429 if ($this->_safe_mode == TRUE)
\r
1431 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
\r
1442 // most documentation of sendmail using the "-f" flag lacks a space after it, however
\r
1443 // we've encountered servers that seem to require it to be in place.
\r
1444 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
\r
1455 // --------------------------------------------------------------------
\r
1458 * Send using Sendmail
\r
1463 function _send_with_sendmail()
\r
1465 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
\r
1467 if ( ! is_resource($fp))
\r
1469 $this->_set_error_message('email_no_socket');
\r
1473 fputs($fp, $this->_header_str);
\r
1474 fputs($fp, $this->_finalbody);
\r
1475 pclose($fp) >> 8 & 0xFF;
\r
1480 // --------------------------------------------------------------------
\r
1488 function _send_with_smtp()
\r
1490 if ($this->smtp_host == '')
\r
1492 $this->_set_error_message('email_no_hostname');
\r
1496 $this->_smtp_connect();
\r
1497 $this->_smtp_authenticate();
\r
1499 $this->_send_command('from', $this->clean_email($this->_headers['From']));
\r
1501 foreach($this->_recipients as $val)
\r
1503 $this->_send_command('to', $val);
\r
1506 if (count($this->_cc_array) > 0)
\r
1508 foreach($this->_cc_array as $val)
\r
1512 $this->_send_command('to', $val);
\r
1517 if (count($this->_bcc_array) > 0)
\r
1519 foreach($this->_bcc_array as $val)
\r
1523 $this->_send_command('to', $val);
\r
1528 $this->_send_command('data');
\r
1530 // perform dot transformation on any lines that begin with a dot
\r
1531 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
\r
1533 $this->_send_data('.');
\r
1535 $reply = $this->_get_smtp_data();
\r
1537 $this->_set_error_message($reply);
\r
1539 if (strncmp($reply, '250', 3) != 0)
\r
1541 $this->_set_error_message('email_smtp_error', $reply);
\r
1545 $this->_send_command('quit');
\r
1549 // --------------------------------------------------------------------
\r
1558 function _smtp_connect()
\r
1560 $this->_smtp_connect = fsockopen($this->smtp_host,
\r
1564 $this->smtp_timeout);
\r
1566 if( ! is_resource($this->_smtp_connect))
\r
1568 $this->_set_error_message('email_smtp_error', $errno." ".$errstr);
\r
1572 $this->_set_error_message($this->_get_smtp_data());
\r
1573 return $this->_send_command('hello');
\r
1576 // --------------------------------------------------------------------
\r
1579 * Send SMTP command
\r
1586 function _send_command($cmd, $data = '')
\r
1592 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
\r
1593 $this->_send_data('EHLO '.$this->_get_hostname());
\r
1595 $this->_send_data('HELO '.$this->_get_hostname());
\r
1601 $this->_send_data('MAIL FROM:<'.$data.'>');
\r
1607 $this->_send_data('RCPT TO:<'.$data.'>');
\r
1613 $this->_send_data('DATA');
\r
1619 $this->_send_data('QUIT');
\r
1625 $reply = $this->_get_smtp_data();
\r
1627 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
\r
1629 if (substr($reply, 0, 3) != $resp)
\r
1631 $this->_set_error_message('email_smtp_error', $reply);
\r
1635 if ($cmd == 'quit')
\r
1637 fclose($this->_smtp_connect);
\r
1643 // --------------------------------------------------------------------
\r
1646 * SMTP Authenticate
\r
1651 function _smtp_authenticate()
\r
1653 if ( ! $this->_smtp_auth)
\r
1658 if ($this->smtp_user == "" AND $this->smtp_pass == "")
\r
1660 $this->_set_error_message('email_no_smtp_unpw');
\r
1664 $this->_send_data('AUTH LOGIN');
\r
1666 $reply = $this->_get_smtp_data();
\r
1668 if (strncmp($reply, '334', 3) != 0)
\r
1670 $this->_set_error_message('email_failed_smtp_login', $reply);
\r
1674 $this->_send_data(base64_encode($this->smtp_user));
\r
1676 $reply = $this->_get_smtp_data();
\r
1678 if (strncmp($reply, '334', 3) != 0)
\r
1680 $this->_set_error_message('email_smtp_auth_un', $reply);
\r
1684 $this->_send_data(base64_encode($this->smtp_pass));
\r
1686 $reply = $this->_get_smtp_data();
\r
1688 if (strncmp($reply, '235', 3) != 0)
\r
1690 $this->_set_error_message('email_smtp_auth_pw', $reply);
\r
1697 // --------------------------------------------------------------------
\r
1705 function _send_data($data)
\r
1707 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
\r
1709 $this->_set_error_message('email_smtp_data_failure', $data);
\r
1718 // --------------------------------------------------------------------
\r
1726 function _get_smtp_data()
\r
1730 while ($str = fgets($this->_smtp_connect, 512))
\r
1734 if (substr($str, 3, 1) == " ")
\r
1743 // --------------------------------------------------------------------
\r
1751 function _get_hostname()
\r
1753 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
\r
1756 // --------------------------------------------------------------------
\r
1764 function _get_ip()
\r
1766 if ($this->_IP !== FALSE)
\r
1768 return $this->_IP;
\r
1771 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
\r
1772 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
\r
1773 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
\r
1775 if ($cip && $rip) $this->_IP = $cip;
\r
1776 elseif ($rip) $this->_IP = $rip;
\r
1777 elseif ($cip) $this->_IP = $cip;
\r
1778 elseif ($fip) $this->_IP = $fip;
\r
1780 if (strstr($this->_IP, ','))
\r
1782 $x = explode(',', $this->_IP);
\r
1783 $this->_IP = end($x);
\r
1786 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
\r
1788 $this->_IP = '0.0.0.0';
\r
1795 return $this->_IP;
\r
1798 // --------------------------------------------------------------------
\r
1801 * Get Debug Message
\r
1806 function print_debugger()
\r
1810 if (count($this->_debug_msg) > 0)
\r
1812 foreach ($this->_debug_msg as $val)
\r
1818 $msg .= "<pre>".$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
\r
1822 // --------------------------------------------------------------------
\r
1831 function _set_error_message($msg, $val = '')
\r
1833 $CI =& get_instance();
\r
1834 $CI->lang->load('email');
\r
1836 if (FALSE === ($line = $CI->lang->line($msg)))
\r
1838 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
\r
1842 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
\r
1846 // --------------------------------------------------------------------
\r
1855 function _mime_types($ext = "")
\r
1857 $mimes = array( 'hqx' => 'application/mac-binhex40',
\r
1858 'cpt' => 'application/mac-compactpro',
\r
1859 'doc' => 'application/msword',
\r
1860 'bin' => 'application/macbinary',
\r
1861 'dms' => 'application/octet-stream',
\r
1862 'lha' => 'application/octet-stream',
\r
1863 'lzh' => 'application/octet-stream',
\r
1864 'exe' => 'application/octet-stream',
\r
1865 'class' => 'application/octet-stream',
\r
1866 'psd' => 'application/octet-stream',
\r
1867 'so' => 'application/octet-stream',
\r
1868 'sea' => 'application/octet-stream',
\r
1869 'dll' => 'application/octet-stream',
\r
1870 'oda' => 'application/oda',
\r
1871 'pdf' => 'application/pdf',
\r
1872 'ai' => 'application/postscript',
\r
1873 'eps' => 'application/postscript',
\r
1874 'ps' => 'application/postscript',
\r
1875 'smi' => 'application/smil',
\r
1876 'smil' => 'application/smil',
\r
1877 'mif' => 'application/vnd.mif',
\r
1878 'xls' => 'application/vnd.ms-excel',
\r
1879 'ppt' => 'application/vnd.ms-powerpoint',
\r
1880 'wbxml' => 'application/vnd.wap.wbxml',
\r
1881 'wmlc' => 'application/vnd.wap.wmlc',
\r
1882 'dcr' => 'application/x-director',
\r
1883 'dir' => 'application/x-director',
\r
1884 'dxr' => 'application/x-director',
\r
1885 'dvi' => 'application/x-dvi',
\r
1886 'gtar' => 'application/x-gtar',
\r
1887 'php' => 'application/x-httpd-php',
\r
1888 'php4' => 'application/x-httpd-php',
\r
1889 'php3' => 'application/x-httpd-php',
\r
1890 'phtml' => 'application/x-httpd-php',
\r
1891 'phps' => 'application/x-httpd-php-source',
\r
1892 'js' => 'application/x-javascript',
\r
1893 'swf' => 'application/x-shockwave-flash',
\r
1894 'sit' => 'application/x-stuffit',
\r
1895 'tar' => 'application/x-tar',
\r
1896 'tgz' => 'application/x-tar',
\r
1897 'xhtml' => 'application/xhtml+xml',
\r
1898 'xht' => 'application/xhtml+xml',
\r
1899 'zip' => 'application/zip',
\r
1900 'mid' => 'audio/midi',
\r
1901 'midi' => 'audio/midi',
\r
1902 'mpga' => 'audio/mpeg',
\r
1903 'mp2' => 'audio/mpeg',
\r
1904 'mp3' => 'audio/mpeg',
\r
1905 'aif' => 'audio/x-aiff',
\r
1906 'aiff' => 'audio/x-aiff',
\r
1907 'aifc' => 'audio/x-aiff',
\r
1908 'ram' => 'audio/x-pn-realaudio',
\r
1909 'rm' => 'audio/x-pn-realaudio',
\r
1910 'rpm' => 'audio/x-pn-realaudio-plugin',
\r
1911 'ra' => 'audio/x-realaudio',
\r
1912 'rv' => 'video/vnd.rn-realvideo',
\r
1913 'wav' => 'audio/x-wav',
\r
1914 'bmp' => 'image/bmp',
\r
1915 'gif' => 'image/gif',
\r
1916 'jpeg' => 'image/jpeg',
\r
1917 'jpg' => 'image/jpeg',
\r
1918 'jpe' => 'image/jpeg',
\r
1919 'png' => 'image/png',
\r
1920 'tiff' => 'image/tiff',
\r
1921 'tif' => 'image/tiff',
\r
1922 'css' => 'text/css',
\r
1923 'html' => 'text/html',
\r
1924 'htm' => 'text/html',
\r
1925 'shtml' => 'text/html',
\r
1926 'txt' => 'text/plain',
\r
1927 'text' => 'text/plain',
\r
1928 'log' => 'text/plain',
\r
1929 'rtx' => 'text/richtext',
\r
1930 'rtf' => 'text/rtf',
\r
1931 'xml' => 'text/xml',
\r
1932 'xsl' => 'text/xml',
\r
1933 'mpeg' => 'video/mpeg',
\r
1934 'mpg' => 'video/mpeg',
\r
1935 'mpe' => 'video/mpeg',
\r
1936 'qt' => 'video/quicktime',
\r
1937 'mov' => 'video/quicktime',
\r
1938 'avi' => 'video/x-msvideo',
\r
1939 'movie' => 'video/x-sgi-movie',
\r
1940 'doc' => 'application/msword',
\r
1941 'word' => 'application/msword',
\r
1942 'xl' => 'application/excel',
\r
1943 'eml' => 'message/rfc822'
\r
1946 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
\r
1950 // END CI_Email class
\r
1952 /* End of file Email.php */
\r
1953 /* Location: ./system/libraries/Email.php */