From b0dd10f8171945e0c1f3527dd1e9d18b043e01a7 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 25 Aug 2006 17:25:49 +0000 Subject: Initial Import --- system/libraries/Email.php | 1740 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1740 insertions(+) create mode 100644 system/libraries/Email.php (limited to 'system/libraries/Email.php') diff --git a/system/libraries/Email.php b/system/libraries/Email.php new file mode 100644 index 000000000..96dc0014d --- /dev/null +++ b/system/libraries/Email.php @@ -0,0 +1,1740 @@ + 0) + { + $this->initialize($config); + } + + log_message('debug', "Email Class Initialized"); + } + // END CI_Email() + + // -------------------------------------------------------------------- + + /** + * Initialize preferences + * + * @access public + * @param array + * @return void + */ + function initialize($config = array()) + { + $this->clear(); + foreach ($config as $key => $val) + { + if (isset($this->$key)) + { + $method = 'set_'.$key; + + if (method_exists($this, $method)) + { + $this->$method($val); + } + else + { + $this->$key = $val; + } + } + } + $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE; + $this->_safe_mode = (@ini_get("safe_mode") == 0) ? FALSE : TRUE; + } + // END initialize() + + // -------------------------------------------------------------------- + + /** + * Initialize the Email Data + * + * @access public + * @return void + */ + function clear() + { + $this->_subject = ""; + $this->_body = ""; + $this->_finalbody = ""; + $this->_header_str = ""; + $this->_replyto_flag = FALSE; + $this->_recipients = array(); + $this->_headers = array(); + $this->_debug_msg = array(); + + $this->_set_header('User-Agent', $this->useragent); + $this->_set_header('Date', $this->_set_date()); + } + // END clear() + + // -------------------------------------------------------------------- + + /** + * Set FROM + * + * @access public + * @param string + * @param string + * @return void + */ + function from($from, $name = '') + { + if (preg_match( '/\<(.*)\>/', $from, $match)) + $from = $match['1']; + + if ($this->validate) + $this->validate_email($this->_str_to_array($from)); + + if ($name != '' && substr($name, 0, 1) != '"') + { + $name = '"'.$name.'"'; + } + + $this->_set_header('From', $name.' <'.$from.'>'); + $this->_set_header('Return-Path', '<'.$from.'>'); + } + // END from() + + // -------------------------------------------------------------------- + + /** + * Set Reply-to + * + * @access public + * @param string + * @param string + * @return void + */ + function reply_to($replyto, $name = '') + { + if (preg_match( '/\<(.*)\>/', $replyto, $match)) + $replyto = $match['1']; + + if ($this->validate) + $this->validate_email($this->_str_to_array($replyto)); + + if ($name == '') + { + $name = $replyto; + } + + if (substr($name, 0, 1) != '"') + { + $name = '"'.$name.'"'; + } + + $this->_set_header('Reply-To', $name.' <'.$replyto.'>'); + $this->_replyto_flag = TRUE; + } + // END reply_to() + + // -------------------------------------------------------------------- + + /** + * Set Recipients + * + * @access public + * @param string + * @return void + */ + function to($to) + { + $to = $this->_str_to_array($to); + $to = $this->clean_email($to); + + if ($this->validate) + $this->validate_email($to); + + if ($this->_get_protocol() != 'mail') + $this->_set_header('To', implode(", ", $to)); + + switch ($this->_get_protocol()) + { + case 'smtp' : $this->_recipients = $to; + break; + case 'sendmail' : $this->_recipients = implode(", ", $to); + break; + case 'mail' : $this->_recipients = implode(", ", $to); + break; + } + } + // END to() + + // -------------------------------------------------------------------- + + /** + * Set CC + * + * @access public + * @param string + * @return void + */ + function cc($cc) + { + $cc = $this->_str_to_array($cc); + $cc = $this->clean_email($cc); + + if ($this->validate) + $this->validate_email($cc); + + $this->_set_header('Cc', implode(", ", $cc)); + + if ($this->_get_protocol() == "smtp") + $this->_cc_array = $cc; + } + // END cc() + + // -------------------------------------------------------------------- + + /** + * Set BCC + * + * @access public + * @param string + * @param string + * @return void + */ + function bcc($bcc, $limit = '') + { + if ($limit != '' && ctype_digit($limit)) + { + $this->bcc_batch_mode = true; + $this->bcc_batch_size = $limit; + } + + $bcc = $this->_str_to_array($bcc); + $bcc = $this->clean_email($bcc); + + if ($this->validate) + $this->validate_email($bcc); + + if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size)) + $this->_bcc_array = $bcc; + else + $this->_set_header('Bcc', implode(", ", $bcc)); + } + // END bcc() + + // -------------------------------------------------------------------- + + /** + * Set Email Subject + * + * @access public + * @param string + * @return void + */ + function subject($subject) + { + $subject = preg_replace("/(\r\n)|(\r)|(\n)/", "", $subject); + $subject = preg_replace("/(\t)/", " ", $subject); + + $this->_set_header('Subject', trim($subject)); + } + // END subject() + + // -------------------------------------------------------------------- + + /** + * Set Body + * + * @access public + * @param string + * @return void + */ + function message($body) + { + $body = rtrim(str_replace("\r", "", $body)); + + if ($this->wordwrap === TRUE AND $this->mailtype != 'html') + $this->_body = $this->word_wrap($body); + else + $this->_body = $body; + + $this->_body = stripslashes($this->_body); + } + // END message() + + // -------------------------------------------------------------------- + + /** + * Assign file attachments + * + * @access public + * @param string + * @return string + */ + function attach($filename, $disposition = 'attachment') + { + $this->_attach_name[] = $filename; + $this->_attach_type[] = $this->_mime_types(next(explode('.', basename($filename)))); + $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters + } + // END attach() + + // -------------------------------------------------------------------- + + /** + * Add a Header Item + * + * @access public + * @param string + * @param string + * @return void + */ + function _set_header($header, $value) + { + $this->_headers[$header] = $value; + } + // END _set_header() + + // -------------------------------------------------------------------- + + /** + * Convert a String to an Array + * + * @access public + * @param string + * @return array + */ + function _str_to_array($email) + { + if ( ! is_array($email)) + { + if (ereg(',$', $email)) + $email = substr($email, 0, -1); + + if (ereg('^,', $email)) + $email = substr($email, 1); + + if (ereg(',', $email)) + { + $x = explode(',', $email); + $email = array(); + + for ($i = 0; $i < count($x); $i ++) + $email[] = trim($x[$i]); + } + else + { + $email = trim($email); + settype($email, "array"); + } + } + return $email; + } + // END _str_to_array() + + // -------------------------------------------------------------------- + + /** + * Set Multipart Value + * + * @access public + * @param string + * @return void + */ + function set_alt_message($str = '') + { + $this->alt_message = ($str == '') ? '' : $str; + } + // END set_alt_message() + + // -------------------------------------------------------------------- + + /** + * Set Mailtype + * + * @access public + * @param string + * @return void + */ + function set_mailtype($type = 'text') + { + $this->mailtype = ($type == 'html') ? 'html' : 'text'; + } + // END set_mailtype() + + // -------------------------------------------------------------------- + + /** + * Set Wordwrap + * + * @access public + * @param string + * @return void + */ + function set_wordwrap($wordwrap = TRUE) + { + $this->wordwrap = ($wordwrap === FALSE) ? FALSE : TRUE; + } + // END set_wordwrap() + + // -------------------------------------------------------------------- + + /** + * Set Protocal + * + * @access public + * @param string + * @return void + */ + function set_protocol($protocol = 'mail') + { + $this->protocol = ( ! in_array($protocol, $this->_protocols)) ? 'mail' : strtolower($protocol); + } + // END set_protocol() + + // -------------------------------------------------------------------- + + /** + * Set Priority + * + * @access public + * @param integer + * @return void + */ + function set_priority($n = 3) + { + if ( ! ctype_digit($n)) + { + $this->priority = 3; + return; + } + + if ($n < 1 OR $n > 5) + { + $this->priority = 3; + return; + } + + $this->priority = $n; + } + // END set_priority() + + // -------------------------------------------------------------------- + + /** + * Set Newline Character + * + * @access public + * @param string + * @return void + */ + function set_newline($newline = "\n") + { + if ($newline != "\n" OR $newline != "\r\n" OR $newline != "\r") + { + $this->newline = "\n"; + return; + } + + $this->newline = $newline; + } + // END set_newline() + + // -------------------------------------------------------------------- + + /** + * Set Message Boundry + * + * @access private + * @return void + */ + function _set_boundaries() + { + $this->_alt_boundary = "B_ALT_".uniqid(''); // mulipart/alternative + $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary + } + // END _set_boundaries() + + // -------------------------------------------------------------------- + + /** + * Get the Message ID + * + * @access private + * @return string + */ + function _get_message_id() + { + $from = $this->_headers['Return-Path']; + $from = str_replace(">", "", $from); + $from = str_replace("<", "", $from); + + return "<".uniqid('').strstr($from, '@').">"; + } + // END _get_message_id() + + // -------------------------------------------------------------------- + + /** + * Get Mail Protocol + * + * @access private + * @param bool + * @return string + */ + function _get_protocol($return = true) + { + $this->protocol = strtolower($this->protocol); + $this->protocol = ( ! in_array($this->protocol, $this->_protocols)) ? 'mail' : $this->protocol; + + if ($return == true) + return $this->protocol; + } + // END _get_protocol() + + // -------------------------------------------------------------------- + + /** + * Get Mail Encoding + * + * @access private + * @param bool + * @return string + */ + function _get_encoding($return = true) + { + $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '7bit' : $this->_encoding; + + if ( ! in_array($this->charset, $this->_base_charsets)) + $this->_encoding = "8bit"; + + if ($return == true) + return $this->_encoding; + } + // END _get_encoding() + + // -------------------------------------------------------------------- + + /** + * Get content type (text/html/attachment) + * + * @access private + * @return string + */ + function _get_content_type() + { + if ($this->mailtype == 'html' && count($this->_attach_name) == 0) + return 'html'; + + elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0) + return 'html-attach'; + + elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0) + return 'plain-attach'; + + else return 'plain'; + } + // END _get_content_type() + + // -------------------------------------------------------------------- + + /** + * Set RFC 822 Date + * + * @access public + * @return string + */ + function _set_date() + { + $timezone = date("Z"); + $operator = (substr($timezone, 0, 1) == '-') ? '-' : '+'; + $timezone = abs($timezone); + $timezone = ($timezone/3600) * 100 + ($timezone % 3600) /60; + + return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone); + } + // END _set_date() + + // -------------------------------------------------------------------- + + /** + * Mime message + * + * @access private + * @return string + */ + function _get_mime_message() + { + return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format."; + } + // END _get_mime_message() + + // -------------------------------------------------------------------- + + /** + * Validate Email Address + * + * @access public + * @param string + * @return bool + */ + function validate_email($email) + { + if ( ! is_array($email)) + { + $this->_set_error_message('email_must_be_array'); + return FALSE; + } + + foreach ($email as $val) + { + if ( ! $this->valid_email($val)) + { + $this->_set_error_message('email_invalid_address', $val); + return FALSE; + } + } + } + // END validate_email() + + // -------------------------------------------------------------------- + + /** + * Email Validation + * + * @access public + * @param string + * @return bool + */ + function valid_email($address) + { + if ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) + return FALSE; + else + return TRUE; + } + // END valid_email() + + // -------------------------------------------------------------------- + + /** + * Clean Extended Email Address: Joe Smith + * + * @access public + * @param string + * @return string + */ + function clean_email($email) + { + if ( ! is_array($email)) + { + if (preg_match('/\<(.*)\>/', $email, $match)) + return $match['1']; + else + return $email; + } + + $clean_email = array(); + + for ($i=0; $i < count($email); $i++) + { + if (preg_match( '/\<(.*)\>/', $email[$i], $match)) + $clean_email[] = $match['1']; + else + $clean_email[] = $email[$i]; + } + + return $clean_email; + } + // END clean_email() + + // -------------------------------------------------------------------- + + /** + * Build alternative plain text message + * + * This function provides the raw message for use + * in plain-text headers of HTML-formatted emails. + * If the user hasn't specified his own alternative message + * it creates one by stripping the HTML + * + * @access private + * @return string + */ + function _get_alt_message() + { + if (eregi( '\', $this->_body, $match)) + { + $body = $match['1']; + $body = substr($body, strpos($body, ">") + 1); + } + else + { + $body = $this->_body; + } + + $body = trim(strip_tags($body)); + $body = preg_replace( '#