1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * CodeIgniter Email Class
21 * Permits email to be sent using Mail, Sendmail, or SMTP.
23 * @package CodeIgniter
24 * @subpackage Libraries
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/email.html
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 $smtp_crypto = ""; // SMTP Encryption. Can be null, tls or ssl.
40 var $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
41 var $wrapchars = "76"; // Number of characters to wrap at.
42 var $mailtype = "text"; // text/html Defines email formatting
43 var $charset = "utf-8"; // Default char set: iso-8859-1 or us-ascii
44 var $multipart = "mixed"; // "mixed" (in the body) or "related" (separate)
45 var $alt_message = ''; // Alternative message for HTML emails
46 var $validate = FALSE; // TRUE/FALSE. Enables email validation
47 var $priority = "3"; // Default priority (1 - 5)
48 var $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
49 var $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
50 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
51 // distasteful, is the only thing that seems to work for all environments.
52 var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
53 var $bcc_batch_mode = FALSE; // TRUE/FALSE Turns on/off Bcc batch feature
54 var $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
55 var $_safe_mode = FALSE;
59 var $_alt_boundary = "";
60 var $_atc_boundary = "";
61 var $_header_str = "";
62 var $_smtp_connect = "";
63 var $_encoding = "8bit";
65 var $_smtp_auth = FALSE;
66 var $_replyto_flag = FALSE;
67 var $_debug_msg = array();
68 var $_recipients = array();
69 var $_cc_array = array();
70 var $_bcc_array = array();
71 var $_headers = array();
72 var $_attach_name = array();
73 var $_attach_type = array();
74 var $_attach_disp = array();
75 var $_protocols = array('mail', 'sendmail', 'smtp');
76 var $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
77 var $_bit_depths = array('7bit', '8bit');
78 var $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
82 * Constructor - Sets Email Preferences
84 * The constructor can be passed an array of config values
86 public function __construct($config = array())
88 if (count($config) > 0)
90 $this->initialize($config);
94 $this->_smtp_auth
= ($this->smtp_user
== '' AND $this->smtp_pass
== '') ? FALSE : TRUE;
95 $this->_safe_mode
= ((boolean
)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
98 log_message('debug', "Email Class Initialized");
101 // --------------------------------------------------------------------
104 * Initialize preferences
110 public function initialize($config = array())
112 foreach ($config as $key => $val)
114 if (isset($this->$key))
116 $method = 'set_'.$key;
118 if (method_exists($this, $method))
120 $this->$method($val);
130 $this->_smtp_auth
= ($this->smtp_user
== '' AND $this->smtp_pass
== '') ? FALSE : TRUE;
131 $this->_safe_mode
= ((boolean
)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
136 // --------------------------------------------------------------------
139 * Initialize the Email Data
144 public function clear($clear_attachments = FALSE)
146 $this->_subject
= "";
148 $this->_finalbody
= "";
149 $this->_header_str
= "";
150 $this->_replyto_flag
= FALSE;
151 $this->_recipients
= array();
152 $this->_cc_array
= array();
153 $this->_bcc_array
= array();
154 $this->_headers
= array();
155 $this->_debug_msg
= array();
157 $this->_set_header('User-Agent', $this->useragent
);
158 $this->_set_header('Date', $this->_set_date());
160 if ($clear_attachments !== FALSE)
162 $this->_attach_name
= array();
163 $this->_attach_type
= array();
164 $this->_attach_disp
= array();
170 // --------------------------------------------------------------------
180 public function from($from, $name = '')
182 if (preg_match( '/\<(.*)\>/', $from, $match))
189 $this->validate_email($this->_str_to_array($from));
192 // prepare the display name
195 // only use Q encoding if there are characters that would require it
196 if ( ! preg_match('/[\200-\377]/', $name))
198 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
199 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
203 $name = $this->_prep_q_encoding($name, TRUE);
207 $this->_set_header('From', $name.' <'.$from.'>');
208 $this->_set_header('Return-Path', '<'.$from.'>');
213 // --------------------------------------------------------------------
223 public function reply_to($replyto, $name = '')
225 if (preg_match( '/\<(.*)\>/', $replyto, $match))
227 $replyto = $match['1'];
232 $this->validate_email($this->_str_to_array($replyto));
240 if (strncmp($name, '"', 1) != 0)
242 $name = '"'.$name.'"';
245 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
246 $this->_replyto_flag
= TRUE;
251 // --------------------------------------------------------------------
260 public function to($to)
262 $to = $this->_str_to_array($to);
263 $to = $this->clean_email($to);
267 $this->validate_email($to);
270 if ($this->_get_protocol() != 'mail')
272 $this->_set_header('To', implode(", ", $to));
275 switch ($this->_get_protocol())
278 $this->_recipients
= $to;
282 $this->_recipients
= implode(", ", $to);
289 // --------------------------------------------------------------------
298 public function cc($cc)
300 $cc = $this->_str_to_array($cc);
301 $cc = $this->clean_email($cc);
305 $this->validate_email($cc);
308 $this->_set_header('Cc', implode(", ", $cc));
310 if ($this->_get_protocol() == "smtp")
312 $this->_cc_array
= $cc;
318 // --------------------------------------------------------------------
328 public function bcc($bcc, $limit = '')
330 if ($limit != '' && is_numeric($limit))
332 $this->bcc_batch_mode
= TRUE;
333 $this->bcc_batch_size
= $limit;
336 $bcc = $this->_str_to_array($bcc);
337 $bcc = $this->clean_email($bcc);
341 $this->validate_email($bcc);
344 if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode
&& count($bcc) > $this->bcc_batch_size
))
346 $this->_bcc_array
= $bcc;
350 $this->_set_header('Bcc', implode(", ", $bcc));
356 // --------------------------------------------------------------------
365 public function subject($subject)
367 $subject = $this->_prep_q_encoding($subject);
368 $this->_set_header('Subject', $subject);
372 // --------------------------------------------------------------------
381 public function message($body)
383 $this->_body
= rtrim(str_replace("\r", "", $body));
385 /* strip slashes only if magic quotes is ON
386 if we do it with magic quotes OFF, it strips real, user-inputted chars.
388 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
389 it will probably not exist in future versions at all.
391 if ( ! is_php('5.4') && get_magic_quotes_gpc())
393 $this->_body
= stripslashes($this->_body
);
399 // --------------------------------------------------------------------
402 * Assign file attachments
408 public function attach($filename, $disposition = 'attachment')
410 $this->_attach_name
[] = $filename;
411 $this->_attach_type
[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION
));
412 $this->_attach_disp
[] = $disposition; // Can also be 'inline' Not sure if it matters
416 // --------------------------------------------------------------------
426 protected function _set_header($header, $value)
428 $this->_headers
[$header] = $value;
431 // --------------------------------------------------------------------
434 * Convert a String to an Array
440 protected function _str_to_array($email)
442 if ( ! is_array($email))
444 if (strpos($email, ',') !== FALSE)
446 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY
);
450 $email = trim($email);
451 settype($email, "array");
457 // --------------------------------------------------------------------
460 * Set Multipart Value
466 public function set_alt_message($str = '')
468 $this->alt_message
= $str;
472 // --------------------------------------------------------------------
481 public function set_mailtype($type = 'text')
483 $this->mailtype
= ($type == 'html') ? 'html' : 'text';
487 // --------------------------------------------------------------------
496 public function set_wordwrap($wordwrap = TRUE)
498 $this->wordwrap
= ($wordwrap === FALSE) ? FALSE : TRUE;
502 // --------------------------------------------------------------------
511 public function set_protocol($protocol = 'mail')
513 $this->protocol
= ( ! in_array($protocol, $this->_protocols
, TRUE)) ? 'mail' : strtolower($protocol);
517 // --------------------------------------------------------------------
526 public function set_priority($n = 3)
528 if ( ! is_numeric($n))
534 if ($n < 1 OR $n > 5)
540 $this->priority
= $n;
544 // --------------------------------------------------------------------
547 * Set Newline Character
553 public function set_newline($newline = "\n")
555 if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
557 $this->newline
= "\n";
561 $this->newline
= $newline;
566 // --------------------------------------------------------------------
575 public function set_crlf($crlf = "\n")
577 if ($crlf != "\n" AND $crlf != "\r\n" AND $crlf != "\r")
588 // --------------------------------------------------------------------
591 * Set Message Boundary
596 protected function _set_boundaries()
598 $this->_alt_boundary
= "B_ALT_".uniqid(''); // multipart/alternative
599 $this->_atc_boundary
= "B_ATC_".uniqid(''); // attachment boundary
602 // --------------------------------------------------------------------
610 protected function _get_message_id()
612 $from = $this->_headers
['Return-Path'];
613 $from = str_replace(">", "", $from);
614 $from = str_replace("<", "", $from);
616 return "<".uniqid('').strstr($from, '@').">";
619 // --------------------------------------------------------------------
628 protected function _get_protocol($return = TRUE)
630 $this->protocol
= strtolower($this->protocol
);
631 $this->protocol
= ( ! in_array($this->protocol
, $this->_protocols
, TRUE)) ? 'mail' : $this->protocol
;
635 return $this->protocol
;
639 // --------------------------------------------------------------------
648 protected function _get_encoding($return = TRUE)
650 $this->_encoding
= ( ! in_array($this->_encoding
, $this->_bit_depths
)) ? '8bit' : $this->_encoding
;
652 foreach ($this->_base_charsets
as $charset)
654 if (strncmp($charset, $this->charset
, strlen($charset)) == 0)
656 $this->_encoding
= '7bit';
662 return $this->_encoding
;
666 // --------------------------------------------------------------------
669 * Get content type (text/html/attachment)
674 protected function _get_content_type()
676 if ($this->mailtype
== 'html' && count($this->_attach_name
) == 0)
680 elseif ($this->mailtype
== 'html' && count($this->_attach_name
) > 0)
682 return 'html-attach';
684 elseif ($this->mailtype
== 'text' && count($this->_attach_name
) > 0)
686 return 'plain-attach';
694 // --------------------------------------------------------------------
702 protected function _set_date()
704 $timezone = date("Z");
705 $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+';
706 $timezone = abs($timezone);
707 $timezone = floor($timezone/3600) * 100 +
($timezone %
3600 ) / 60;
709 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
712 // --------------------------------------------------------------------
720 protected function _get_mime_message()
722 return "This is a multi-part message in MIME format.".$this->newline
."Your email application may not support this format.";
725 // --------------------------------------------------------------------
728 * Validate Email Address
734 public function validate_email($email)
736 if ( ! is_array($email))
738 $this->_set_error_message('lang:email_must_be_array');
742 foreach ($email as $val)
744 if ( ! $this->valid_email($val))
746 $this->_set_error_message('lang:email_invalid_address', $val);
754 // --------------------------------------------------------------------
763 public function valid_email($address)
765 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
768 // --------------------------------------------------------------------
771 * Clean Extended Email Address: Joe Smith <joe@smith.com>
777 public function clean_email($email)
779 if ( ! is_array($email))
781 if (preg_match('/\<(.*)\>/', $email, $match))
791 $clean_email = array();
793 foreach ($email as $addy)
795 if (preg_match( '/\<(.*)\>/', $addy, $match))
797 $clean_email[] = $match['1'];
801 $clean_email[] = $addy;
808 // --------------------------------------------------------------------
811 * Build alternative plain text message
813 * This public function provides the raw message for use
814 * in plain-text headers of HTML-formatted emails.
815 * If the user hasn't specified his own alternative message
816 * it creates one by stripping the HTML
821 protected function _get_alt_message()
823 if ($this->alt_message
!= "")
825 return $this->word_wrap($this->alt_message
, '76');
828 if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body
, $match))
834 $body = $this->_body
;
837 $body = trim(strip_tags($body));
838 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
839 $body = str_replace("\t", "", $body);
841 for ($i = 20; $i >= 3; $i--)
845 for ($x = 1; $x <= $i; $x ++
)
850 $body = str_replace($n, "\n\n", $body);
853 return $this->word_wrap($body, '76');
856 // --------------------------------------------------------------------
866 public function word_wrap($str, $charlim = '')
868 // Se the character limit
871 $charlim = ($this->wrapchars
== "") ? "76" : $this->wrapchars
;
874 // Reduce multiple spaces
875 $str = preg_replace("| +|", " ", $str);
877 // Standardize newlines
878 if (strpos($str, "\r") !== FALSE)
880 $str = str_replace(array("\r\n", "\r"), "\n", $str);
883 // If the current word is surrounded by {unwrap} tags we'll
884 // strip the entire chunk and replace it with a marker.
886 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
888 for ($i = 0; $i < count($matches['0']); $i++
)
890 $unwrap[] = $matches['1'][$i];
891 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
895 // Use PHP's native public function to do the initial wordwrap.
896 // We set the cut flag to FALSE so that any individual words that are
897 // too long get left alone. In the next step we'll deal with them.
898 $str = wordwrap($str, $charlim, "\n", FALSE);
900 // Split the string into individual lines of text and cycle through them
902 foreach (explode("\n", $str) as $line)
904 // Is the line within the allowed character count?
905 // If so we'll join it to the output and continue
906 if (strlen($line) <= $charlim)
908 $output .= $line.$this->newline
;
913 while ((strlen($line)) > $charlim)
915 // If the over-length word is a URL we won't wrap it
916 if (preg_match("!\[url.+\]|://|wwww.!", $line))
921 // Trim the word down
922 $temp .= substr($line, 0, $charlim-1);
923 $line = substr($line, $charlim-1);
926 // If $temp contains data it means we had to split up an over-length
927 // word into smaller chunks so we'll add it back to our current line
930 $output .= $temp.$this->newline
.$line;
937 $output .= $this->newline
;
940 // Put our markers back
941 if (count($unwrap) > 0)
943 foreach ($unwrap as $key => $val)
945 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
952 // --------------------------------------------------------------------
955 * Build final headers
961 protected function _build_headers()
963 $this->_set_header('X-Sender', $this->clean_email($this->_headers
['From']));
964 $this->_set_header('X-Mailer', $this->useragent
);
965 $this->_set_header('X-Priority', $this->_priorities
[$this->priority
- 1]);
966 $this->_set_header('Message-ID', $this->_get_message_id());
967 $this->_set_header('Mime-Version', '1.0');
970 // --------------------------------------------------------------------
973 * Write Headers as a string
978 protected function _write_headers()
980 if ($this->protocol
== 'mail')
982 $this->_subject
= $this->_headers
['Subject'];
983 unset($this->_headers
['Subject']);
986 reset($this->_headers
);
987 $this->_header_str
= "";
989 foreach ($this->_headers
as $key => $val)
995 $this->_header_str
.= $key.": ".$val.$this->newline
;
999 if ($this->_get_protocol() == 'mail')
1001 $this->_header_str
= rtrim($this->_header_str
);
1005 // --------------------------------------------------------------------
1008 * Build Final Body and attachments
1013 protected function _build_message()
1015 if ($this->wordwrap
=== TRUE AND $this->mailtype
!= 'html')
1017 $this->_body
= $this->word_wrap($this->_body
);
1020 $this->_set_boundaries();
1021 $this->_write_headers();
1023 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline
: '';
1026 switch ($this->_get_content_type())
1030 $hdr .= "Content-Type: text/plain; charset=" . $this->charset
. $this->newline
;
1031 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
1033 if ($this->_get_protocol() == 'mail')
1035 $this->_header_str
.= $hdr;
1036 $this->_finalbody
= $this->_body
;
1040 $this->_finalbody
= $hdr . $this->newline
. $this->newline
. $this->_body
;
1048 if ($this->send_multipart
=== FALSE)
1050 $hdr .= "Content-Type: text/html; charset=" . $this->charset
. $this->newline
;
1051 $hdr .= "Content-Transfer-Encoding: quoted-printable";
1055 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary
. "\"" . $this->newline
. $this->newline
;
1057 $body .= $this->_get_mime_message() . $this->newline
. $this->newline
;
1058 $body .= "--" . $this->_alt_boundary
. $this->newline
;
1060 $body .= "Content-Type: text/plain; charset=" . $this->charset
. $this->newline
;
1061 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline
. $this->newline
;
1062 $body .= $this->_get_alt_message() . $this->newline
. $this->newline
. "--" . $this->_alt_boundary
. $this->newline
;
1064 $body .= "Content-Type: text/html; charset=" . $this->charset
. $this->newline
;
1065 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline
. $this->newline
;
1068 $this->_finalbody
= $body . $this->_prep_quoted_printable($this->_body
) . $this->newline
. $this->newline
;
1071 if ($this->_get_protocol() == 'mail')
1073 $this->_header_str
.= $hdr;
1077 $this->_finalbody
= $hdr . $this->_finalbody
;
1081 if ($this->send_multipart
!== FALSE)
1083 $this->_finalbody
.= "--" . $this->_alt_boundary
. "--";
1089 case 'plain-attach' :
1091 $hdr .= "Content-Type: multipart/".$this->multipart
."; boundary=\"" . $this->_atc_boundary
."\"" . $this->newline
. $this->newline
;
1093 if ($this->_get_protocol() == 'mail')
1095 $this->_header_str
.= $hdr;
1098 $body .= $this->_get_mime_message() . $this->newline
. $this->newline
;
1099 $body .= "--" . $this->_atc_boundary
. $this->newline
;
1101 $body .= "Content-Type: text/plain; charset=" . $this->charset
. $this->newline
;
1102 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline
. $this->newline
;
1104 $body .= $this->_body
. $this->newline
. $this->newline
;
1107 case 'html-attach' :
1109 $hdr .= "Content-Type: multipart/".$this->multipart
."; boundary=\"" . $this->_atc_boundary
."\"" . $this->newline
. $this->newline
;
1111 if ($this->_get_protocol() == 'mail')
1113 $this->_header_str
.= $hdr;
1116 $body .= $this->_get_mime_message() . $this->newline
. $this->newline
;
1117 $body .= "--" . $this->_atc_boundary
. $this->newline
;
1119 $body .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary
. "\"" . $this->newline
.$this->newline
;
1120 $body .= "--" . $this->_alt_boundary
. $this->newline
;
1122 $body .= "Content-Type: text/plain; charset=" . $this->charset
. $this->newline
;
1123 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline
. $this->newline
;
1124 $body .= $this->_get_alt_message() . $this->newline
. $this->newline
. "--" . $this->_alt_boundary
. $this->newline
;
1126 $body .= "Content-Type: text/html; charset=" . $this->charset
. $this->newline
;
1127 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline
. $this->newline
;
1129 $body .= $this->_prep_quoted_printable($this->_body
) . $this->newline
. $this->newline
;
1130 $body .= "--" . $this->_alt_boundary
. "--" . $this->newline
. $this->newline
;
1135 $attachment = array();
1139 for ($i=0; $i < count($this->_attach_name
); $i++
)
1141 $filename = $this->_attach_name
[$i];
1142 $basename = basename($filename);
1143 $ctype = $this->_attach_type
[$i];
1145 if ( ! file_exists($filename))
1147 $this->_set_error_message('lang:email_attachment_missing', $filename);
1151 $h = "--".$this->_atc_boundary
.$this->newline
;
1152 $h .= "Content-type: ".$ctype."; ";
1153 $h .= "name=\"".$basename."\"".$this->newline
;
1154 $h .= "Content-Disposition: ".$this->_attach_disp
[$i].";".$this->newline
;
1155 $h .= "Content-Transfer-Encoding: base64".$this->newline
;
1157 $attachment[$z++
] = $h;
1158 $file = filesize($filename) +
1;
1160 if ( ! $fp = fopen($filename, FOPEN_READ
))
1162 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
1166 $attachment[$z++
] = chunk_split(base64_encode(fread($fp, $file)));
1170 $body .= implode($this->newline
, $attachment).$this->newline
."--".$this->_atc_boundary
."--";
1173 if ($this->_get_protocol() == 'mail')
1175 $this->_finalbody
= $body;
1179 $this->_finalbody
= $hdr . $body;
1185 // --------------------------------------------------------------------
1188 * Prep Quoted Printable
1190 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1191 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1198 protected function _prep_quoted_printable($str, $charlim = '')
1200 // Set the character limit
1201 // Don't allow over 76, as that will make servers and MUAs barf
1202 // all over quoted-printable data
1203 if ($charlim == '' OR $charlim > '76')
1208 // Reduce multiple spaces
1209 $str = preg_replace("| +|", " ", $str);
1212 $str = preg_replace('/\x00+/', '', $str);
1214 // Standardize newlines
1215 if (strpos($str, "\r") !== FALSE)
1217 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1220 // We are intentionally wrapping so mail servers will encode characters
1221 // properly and MUAs will behave, so {unwrap} must go!
1222 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1224 // Break into an array of lines
1225 $lines = explode("\n", $str);
1230 foreach ($lines as $line)
1232 $length = strlen($line);
1235 // Loop through each character in the line to add soft-wrap
1236 // characters at the end of a line " =\r\n" and add the newly
1237 // processed line(s) to the output (see comment on $crlf class property)
1238 for ($i = 0; $i < $length; $i++
)
1240 // Grab the next character
1241 $char = substr($line, $i, 1);
1242 $ascii = ord($char);
1244 // Convert spaces and tabs but only if it's the end of the line
1245 if ($i == ($length - 1))
1247 $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($ascii)) : $char;
1253 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
1256 // If we're at the character limit, add the line to the output,
1257 // reset our temp variable, and keep on chuggin'
1258 if ((strlen($temp) +
strlen($char)) >= $charlim)
1260 $output .= $temp.$escape.$this->crlf
;
1264 // Add the character to our temporary line
1268 // Add our completed line to the output
1269 $output .= $temp.$this->crlf
;
1272 // get rid of extra CRLF tacked onto the end
1273 $output = substr($output, 0, strlen($this->crlf
) * -1);
1278 // --------------------------------------------------------------------
1283 * Performs "Q Encoding" on a string for use in email headers. It's related
1284 * but not identical to quoted-printable, so it has its own method
1288 * @param bool // set to TRUE for processing From: headers
1291 protected function _prep_q_encoding($str, $from = FALSE)
1293 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1295 // Line length must not exceed 76 characters, so we adjust for
1296 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1297 $limit = 75 - 7 - strlen($this->charset
);
1299 // these special characters must be converted too
1300 $convert = array('_', '=', '?');
1311 for ($i = 0, $length = strlen($str); $i < $length; $i++
)
1313 // Grab the next character
1314 $char = substr($str, $i, 1);
1315 $ascii = ord($char);
1317 // convert ALL non-printable ASCII characters and our specials
1318 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1320 $char = '='.dechex($ascii);
1323 // handle regular spaces a bit more compactly than =20
1329 // If we're at the character limit, add the line to the output,
1330 // reset our temp variable, and keep on chuggin'
1331 if ((strlen($temp) +
strlen($char)) >= $limit)
1333 $output .= $temp.$this->crlf
;
1337 // Add the character to our temporary line
1341 $str = $output.$temp;
1343 // wrap each line with the shebang, charset, and transfer encoding
1344 // the preceding space on successive lines is required for header "folding"
1345 $str = trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset
.'?Q?$1?=', $str));
1350 // --------------------------------------------------------------------
1358 public function send()
1360 if ($this->_replyto_flag
== FALSE)
1362 $this->reply_to($this->_headers
['From']);
1365 if (( ! isset($this->_recipients
) AND ! isset($this->_headers
['To'])) AND
1366 ( ! isset($this->_bcc_array
) AND ! isset($this->_headers
['Bcc'])) AND
1367 ( ! isset($this->_headers
['Cc'])))
1369 $this->_set_error_message('lang:email_no_recipients');
1373 $this->_build_headers();
1375 if ($this->bcc_batch_mode
AND count($this->_bcc_array
) > 0)
1377 if (count($this->_bcc_array
) > $this->bcc_batch_size
)
1378 return $this->batch_bcc_send();
1381 $this->_build_message();
1383 if ( ! $this->_spool_email())
1393 // --------------------------------------------------------------------
1396 * Batch Bcc Send. Sends groups of BCCs in batches
1401 public function batch_bcc_send()
1403 $float = $this->bcc_batch_size
-1;
1409 for ($i = 0; $i < count($this->_bcc_array
); $i++
)
1411 if (isset($this->_bcc_array
[$i]))
1413 $set .= ", ".$this->_bcc_array
[$i];
1418 $chunk[] = substr($set, 1);
1419 $float = $float +
$this->bcc_batch_size
;
1423 if ($i == count($this->_bcc_array
)-1)
1425 $chunk[] = substr($set, 1);
1429 for ($i = 0; $i < count($chunk); $i++
)
1431 unset($this->_headers
['Bcc']);
1434 $bcc = $this->_str_to_array($chunk[$i]);
1435 $bcc = $this->clean_email($bcc);
1437 if ($this->protocol
!= 'smtp')
1439 $this->_set_header('Bcc', implode(", ", $bcc));
1443 $this->_bcc_array
= $bcc;
1446 $this->_build_message();
1447 $this->_spool_email();
1451 // --------------------------------------------------------------------
1454 * Unwrap special elements
1459 protected function _unwrap_specials()
1461 $this->_finalbody
= preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody
);
1464 // --------------------------------------------------------------------
1467 * Strip line-breaks via callback
1472 protected function _remove_nl_callback($matches)
1474 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1476 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1482 // --------------------------------------------------------------------
1485 * Spool mail to the mail server
1490 protected function _spool_email()
1492 $this->_unwrap_specials();
1494 switch ($this->_get_protocol())
1498 if ( ! $this->_send_with_mail())
1500 $this->_set_error_message('lang:email_send_failure_phpmail');
1506 if ( ! $this->_send_with_sendmail())
1508 $this->_set_error_message('lang:email_send_failure_sendmail');
1514 if ( ! $this->_send_with_smtp())
1516 $this->_set_error_message('lang:email_send_failure_smtp');
1523 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
1527 // --------------------------------------------------------------------
1535 protected function _send_with_mail()
1537 if ($this->_safe_mode
== TRUE)
1539 if ( ! mail($this->_recipients
, $this->_subject
, $this->_finalbody
, $this->_header_str
))
1550 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1551 // we've encountered servers that seem to require it to be in place.
1553 if ( ! mail($this->_recipients
, $this->_subject
, $this->_finalbody
, $this->_header_str
, "-f ".$this->clean_email($this->_headers
['From'])))
1564 // --------------------------------------------------------------------
1567 * Send using Sendmail
1572 protected function _send_with_sendmail()
1574 $fp = @popen($this->mailpath
. " -oi -f ".$this->clean_email($this->_headers
['From'])." -t", 'w');
1576 if ($fp === FALSE OR $fp === NULL)
1578 // server probably has popen disabled, so nothing we can do to get a verbose error.
1582 fputs($fp, $this->_header_str
);
1583 fputs($fp, $this->_finalbody
);
1585 $status = pclose($fp);
1587 if (version_compare(PHP_VERSION
, '4.2.3') == -1)
1589 $status = $status >> 8 & 0xFF;
1594 $this->_set_error_message('lang:email_exit_status', $status);
1595 $this->_set_error_message('lang:email_no_socket');
1602 // --------------------------------------------------------------------
1610 protected function _send_with_smtp()
1612 if ($this->smtp_host
== '')
1614 $this->_set_error_message('lang:email_no_hostname');
1618 $this->_smtp_connect();
1619 $this->_smtp_authenticate();
1621 $this->_send_command('from', $this->clean_email($this->_headers
['From']));
1623 foreach ($this->_recipients
as $val)
1625 $this->_send_command('to', $val);
1628 if (count($this->_cc_array
) > 0)
1630 foreach ($this->_cc_array
as $val)
1634 $this->_send_command('to', $val);
1639 if (count($this->_bcc_array
) > 0)
1641 foreach ($this->_bcc_array
as $val)
1645 $this->_send_command('to', $val);
1650 $this->_send_command('data');
1652 // perform dot transformation on any lines that begin with a dot
1653 $this->_send_data($this->_header_str
. preg_replace('/^\./m', '..$1', $this->_finalbody));
1655 $this->_send_data('.');
1657 $reply = $this->_get_smtp_data();
1659 $this->_set_error_message($reply);
1661 if (strncmp($reply, '250', 3) != 0)
1663 $this->_set_error_message('lang
:email_smtp_error
', $reply);
1667 $this->_send_command('quit
');
1671 // --------------------------------------------------------------------
1680 protected function _smtp_connect()
1683 if ($this->smtp_crypto == 'ssl
')
1685 $this->_smtp_connect
= fsockopen($ssl.$this->smtp_host
,
1689 $this->smtp_timeout
);
1691 if ( ! is_resource($this->_smtp_connect
))
1693 $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
1697 $this->_set_error_message($this->_get_smtp_data());
1699 if ($this->smtp_crypto
== 'tls')
1701 $this->_send_command('hello');
1702 $this->_send_command('starttls');
1703 stream_socket_enable_crypto($this->_smtp_connect
, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT
);
1706 return $this->_send_command('hello');
1709 // --------------------------------------------------------------------
1719 protected function _send_command($cmd, $data = '')
1725 if ($this->_smtp_auth
OR $this->_get_encoding() == '8bit')
1726 $this->_send_data('EHLO '.$this->_get_hostname());
1728 $this->_send_data('HELO '.$this->_get_hostname());
1734 $this->_send_data('STARTTLS');
1740 $this->_send_data('MAIL FROM:<'.$data.'>');
1746 $this->_send_data('RCPT TO:<'.$data.'>');
1752 $this->_send_data('DATA');
1758 $this->_send_data('QUIT');
1764 $reply = $this->_get_smtp_data();
1766 $this->_debug_msg
[] = "<pre>".$cmd.": ".$reply."</pre>";
1768 if (substr($reply, 0, 3) != $resp)
1770 $this->_set_error_message('lang:email_smtp_error', $reply);
1776 fclose($this->_smtp_connect
);
1782 // --------------------------------------------------------------------
1790 protected function _smtp_authenticate()
1792 if ( ! $this->_smtp_auth
)
1797 if ($this->smtp_user
== "" AND $this->smtp_pass
== "")
1799 $this->_set_error_message('lang:email_no_smtp_unpw');
1803 $this->_send_data('AUTH LOGIN');
1805 $reply = $this->_get_smtp_data();
1807 if (strncmp($reply, '334', 3) != 0)
1809 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
1813 $this->_send_data(base64_encode($this->smtp_user
));
1815 $reply = $this->_get_smtp_data();
1817 if (strncmp($reply, '334', 3) != 0)
1819 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
1823 $this->_send_data(base64_encode($this->smtp_pass
));
1825 $reply = $this->_get_smtp_data();
1827 if (strncmp($reply, '235', 3) != 0)
1829 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
1836 // --------------------------------------------------------------------
1844 protected function _send_data($data)
1846 if ( ! fwrite($this->_smtp_connect
, $data . $this->newline
))
1848 $this->_set_error_message('lang:email_smtp_data_failure', $data);
1857 // --------------------------------------------------------------------
1865 protected function _get_smtp_data()
1869 while ($str = fgets($this->_smtp_connect
, 512))
1873 if (substr($str, 3, 1) == " ")
1882 // --------------------------------------------------------------------
1890 protected function _get_hostname()
1892 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
1895 // --------------------------------------------------------------------
1903 protected function _get_ip()
1905 if ($this->_IP
!== FALSE)
1910 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1911 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1912 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1914 if ($cip && $rip) $this->_IP
= $cip;
1915 elseif ($rip) $this->_IP
= $rip;
1916 elseif ($cip) $this->_IP
= $cip;
1917 elseif ($fip) $this->_IP
= $fip;
1919 if (strpos($this->_IP
, ',') !== FALSE)
1921 $x = explode(',', $this->_IP
);
1922 $this->_IP
= end($x);
1925 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP
))
1927 $this->_IP
= '0.0.0.0';
1937 // --------------------------------------------------------------------
1945 public function print_debugger()
1949 if (count($this->_debug_msg
) > 0)
1951 foreach ($this->_debug_msg
as $val)
1957 $msg .= "<pre>".$this->_header_str
."\n".htmlspecialchars($this->_subject
)."\n".htmlspecialchars($this->_finalbody
).'</pre>';
1961 // --------------------------------------------------------------------
1970 protected function _set_error_message($msg, $val = '')
1972 $CI =& get_instance();
1973 $CI->lang
->load('email');
1975 if (substr($msg, 0, 5) != 'lang:' || FALSE === ($line = $CI->lang
->line(substr($msg, 5))))
1977 $this->_debug_msg
[] = str_replace('%s', $val, $msg)."<br />";
1981 $this->_debug_msg
[] = str_replace('%s', $val, $line)."<br />";
1985 // --------------------------------------------------------------------
1994 protected function _mime_types($ext = "")
1996 $mimes = array( 'hqx' => 'application/mac-binhex40',
1997 'cpt' => 'application/mac-compactpro',
1998 'doc' => 'application/msword',
1999 'bin' => 'application/macbinary',
2000 'dms' => 'application/octet-stream',
2001 'lha' => 'application/octet-stream',
2002 'lzh' => 'application/octet-stream',
2003 'exe' => 'application/octet-stream',
2004 'class' => 'application/octet-stream',
2005 'psd' => 'application/octet-stream',
2006 'so' => 'application/octet-stream',
2007 'sea' => 'application/octet-stream',
2008 'dll' => 'application/octet-stream',
2009 'oda' => 'application/oda',
2010 'pdf' => 'application/pdf',
2011 'ai' => 'application/postscript',
2012 'eps' => 'application/postscript',
2013 'ps' => 'application/postscript',
2014 'smi' => 'application/smil',
2015 'smil' => 'application/smil',
2016 'mif' => 'application/vnd.mif',
2017 'xls' => 'application/vnd.ms-excel',
2018 'ppt' => 'application/vnd.ms-powerpoint',
2019 'wbxml' => 'application/vnd.wap.wbxml',
2020 'wmlc' => 'application/vnd.wap.wmlc',
2021 'dcr' => 'application/x-director',
2022 'dir' => 'application/x-director',
2023 'dxr' => 'application/x-director',
2024 'dvi' => 'application/x-dvi',
2025 'gtar' => 'application/x-gtar',
2026 'php' => 'application/x-httpd-php',
2027 'php4' => 'application/x-httpd-php',
2028 'php3' => 'application/x-httpd-php',
2029 'phtml' => 'application/x-httpd-php',
2030 'phps' => 'application/x-httpd-php-source',
2031 'js' => 'application/x-javascript',
2032 'swf' => 'application/x-shockwave-flash',
2033 'sit' => 'application/x-stuffit',
2034 'tar' => 'application/x-tar',
2035 'tgz' => 'application/x-tar',
2036 'xhtml' => 'application/xhtml+xml',
2037 'xht' => 'application/xhtml+xml',
2038 'zip' => 'application/zip',
2039 'mid' => 'audio/midi',
2040 'midi' => 'audio/midi',
2041 'mpga' => 'audio/mpeg',
2042 'mp2' => 'audio/mpeg',
2043 'mp3' => 'audio/mpeg',
2044 'aif' => 'audio/x-aiff',
2045 'aiff' => 'audio/x-aiff',
2046 'aifc' => 'audio/x-aiff',
2047 'ram' => 'audio/x-pn-realaudio',
2048 'rm' => 'audio/x-pn-realaudio',
2049 'rpm' => 'audio/x-pn-realaudio-plugin',
2050 'ra' => 'audio/x-realaudio',
2051 'rv' => 'video/vnd.rn-realvideo',
2052 'wav' => 'audio/x-wav',
2053 'bmp' => 'image/bmp',
2054 'gif' => 'image/gif',
2055 'jpeg' => 'image/jpeg',
2056 'jpg' => 'image/jpeg',
2057 'jpe' => 'image/jpeg',
2058 'png' => 'image/png',
2059 'tiff' => 'image/tiff',
2060 'tif' => 'image/tiff',
2061 'css' => 'text/css',
2062 'html' => 'text/html',
2063 'htm' => 'text/html',
2064 'shtml' => 'text/html',
2065 'txt' => 'text/plain',
2066 'text' => 'text/plain',
2067 'log' => 'text/plain',
2068 'rtx' => 'text/richtext',
2069 'rtf' => 'text/rtf',
2070 'xml' => 'text/xml',
2071 'xsl' => 'text/xml',
2072 'mpeg' => 'video/mpeg',
2073 'mpg' => 'video/mpeg',
2074 'mpe' => 'video/mpeg',
2075 'qt' => 'video/quicktime',
2076 'mov' => 'video/quicktime',
2077 'avi' => 'video/x-msvideo',
2078 'movie' => 'video/x-sgi-movie',
2079 'doc' => 'application/msword',
2080 'word' => 'application/msword',
2081 'xl' => 'application/excel',
2082 'eml' => 'message/rfc822'
2085 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
2089 // END CI_Email class
2091 /* End of file Email.php */
2092 /* Location: ./system/libraries/Email.php */