0) { $this->initialize($config); } else { $this->_smtp_auth = ! ($this->smtp_user == '' && $this->smtp_pass == ''); $this->_safe_mode = (bool) @ini_get('safe_mode'); } log_message('debug', 'Email Class Initialized'); } // -------------------------------------------------------------------- /** * Initialize preferences * * @param array * @return void */ public function initialize($config = array()) { 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->clear(); $this->_smtp_auth = ! ($this->smtp_user == '' && $this->smtp_pass == ''); $this->_safe_mode = (bool) @ini_get('safe_mode'); return $this; } // -------------------------------------------------------------------- /** * Initialize the Email Data * * @param bool * @return object */ public function clear($clear_attachments = FALSE) { $this->_subject = ''; $this->_body = ''; $this->_finalbody = ''; $this->_header_str = ''; $this->_replyto_flag = FALSE; $this->_recipients = array(); $this->_cc_array = array(); $this->_bcc_array = array(); $this->_headers = array(); $this->_debug_msg = array(); $this->_set_header('User-Agent', $this->useragent); $this->_set_header('Date', $this->_set_date()); if ($clear_attachments !== FALSE) { $this->_attach_name = array(); $this->_attach_type = array(); $this->_attach_disp = array(); } return $this; } // -------------------------------------------------------------------- /** * Set FROM * * @param string * @param string * @return object */ public function from($from, $name = '') { if (preg_match('/\<(.*)\>/', $from, $match)) { $from = $match[1]; } if ($this->validate) { $this->validate_email($this->_str_to_array($from)); } // prepare the display name if ($name != '') { // only use Q encoding if there are characters that would require it if ( ! preg_match('/[\200-\377]/', $name)) { // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"'; } else { $name = $this->_prep_q_encoding($name, TRUE); } } $this->_set_header('From', $name.' <'.$from.'>'); $this->_set_header('Return-Path', '<'.$from.'>'); return $this; } // -------------------------------------------------------------------- /** * Set Reply-to * * @param string * @param string * @return object */ public 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 (strncmp($name, '"', 1) !== 0) { $name = '"'.$name.'"'; } $this->_set_header('Reply-To', $name.' <'.$replyto.'>'); $this->_replyto_flag = TRUE; return $this; } // -------------------------------------------------------------------- /** * Set Recipients * * @param string * @return object */ public 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': case 'mail': $this->_recipients = implode(', ', $to); break; } return $this; } // -------------------------------------------------------------------- /** * Set CC * * @param string * @return object */ public function cc($cc) { $cc = $this->clean_email($this->_str_to_array($cc)); if ($this->validate) { $this->validate_email($cc); } $this->_set_header('Cc', implode(', ', $cc)); if ($this->_get_protocol() === 'smtp') { $this->_cc_array = $cc; } return $this; } // -------------------------------------------------------------------- /** * Set BCC * * @param string * @param string * @return object */ public function bcc($bcc, $limit = '') { if ($limit != '' && is_numeric($limit)) { $this->bcc_batch_mode = TRUE; $this->bcc_batch_size = $limit; } $bcc = $this->clean_email($this->_str_to_array($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)); } return $this; } // -------------------------------------------------------------------- /** * Set Email Subject * * @param string * @return object */ public function subject($subject) { $subject = $this->_prep_q_encoding($subject); $this->_set_header('Subject', $subject); return $this; } // -------------------------------------------------------------------- /** * Set Body * * @param string * @return object */ public function message($body) { $this->_body = rtrim(str_replace("\r", '', $body)); /* strip slashes only if magic quotes is ON if we do it with magic quotes OFF, it strips real, user-inputted chars. NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and it will probably not exist in future versions at all. */ if ( ! is_php('5.4') && get_magic_quotes_gpc()) { $this->_body = stripslashes($this->_body); } return $this; } // -------------------------------------------------------------------- /** * Assign file attachments * * @param string * @return object */ public function attach($filename, $disposition = '', $newname = NULL, $mime = '') { $this->_attach_name[] = array($filename, $newname); $this->_attach_disp[] = empty($disposition) ? 'attachment' : $disposition; // Can also be 'inline' Not sure if it matters $this->_attach_type[] = $mime; return $this; } // -------------------------------------------------------------------- /** * Add a Header Item * * @param string * @param string * @return void */ protected function _set_header($header, $value) { $this->_headers[$header] = $value; } // -------------------------------------------------------------------- /** * Convert a String to an Array * * @param string * @return array */ protected function _str_to_array($email) { if ( ! is_array($email)) { return (strpos($email, ',') !== FALSE) ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY) : (array) trim($email); } return $email; } // -------------------------------------------------------------------- /** * Set Multipart Value * * @param string * @return object */ public function set_alt_message($str = '') { $this->alt_message = (string) $str; return $this; } // -------------------------------------------------------------------- /** * Set Mailtype * * @param string * @return object */ public function set_mailtype($type = 'text') { $this->mailtype = ($type === 'html') ? 'html' : 'text'; return $this; } // -------------------------------------------------------------------- /** * Set Wordwrap * * @param bool * @return object */ public function set_wordwrap($wordwrap = TRUE) { $this->wordwrap = (bool) $wordwrap; return $this; } // -------------------------------------------------------------------- /** * Set Protocol * * @param string * @return object */ public function set_protocol($protocol = 'mail') { $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail'; return $this; } // -------------------------------------------------------------------- /** * Set Priority * * @param int * @return object */ public function set_priority($n = 3) { $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3; return $this; } // -------------------------------------------------------------------- /** * Set Newline Character * * @param string * @return object */ public function set_newline($newline = "\n") { $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n"; return $this; } // -------------------------------------------------------------------- /** * Set CRLF * * @param string * @return object */ public function set_crlf($crlf = "\n") { $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf; return $this; } // -------------------------------------------------------------------- /** * Set Message Boundary * * @return void */ protected function _set_boundaries() { $this->_alt_boundary = 'B_ALT_'.uniqid(''); // multipart/alternative $this->_atc_boundary = 'B_ATC_'.uniqid(''); // attachment boundary } // -------------------------------------------------------------------- /** * Get the Message ID * * @return string */ protected function _get_message_id() { $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']); return '<'.uniqid('').strstr($from, '@').'>'; } // -------------------------------------------------------------------- /** * Get Mail Protocol * * @param bool * @return mixed */ protected function _get_protocol($return = TRUE) { $this->protocol = strtolower($this->protocol); in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail'; if ($return == TRUE) { return $this->protocol; } } // -------------------------------------------------------------------- /** * Get Mail Encoding * * @param bool * @return string */ protected function _get_encoding($return = TRUE) { in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit'; foreach ($this->_base_charsets as $charset) { if (strncmp($charset, $this->charset, strlen($charset)) === 0) { $this->_encoding = '7bit'; } } if ($return == TRUE) { return $this->_encoding; } } // -------------------------------------------------------------------- /** * Get content type (text/html/attachment) * * @return string */ protected function _get_content_type() { if ($this->mailtype === 'html') { return (count($this->_attach_name) == 0) ? 'html' : 'html-attach'; } elseif ($this->mailtype === 'text' && count($this->_attach_name) > 0) { return 'plain-attach'; } else { return 'plain'; } } // -------------------------------------------------------------------- /** * Set RFC 822 Date * * @return string */ protected function _set_date() { $timezone = date('Z'); $operator = (strncmp($timezone, '-', 1) === 0) ? '-' : '+'; $timezone = abs($timezone); $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60; return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone); } // -------------------------------------------------------------------- /** * Mime message * * @return string */ protected function _get_mime_message() { return 'This is a multi-part message in MIME format.'.$this->newline.'Your email application may not support this format.'; } // -------------------------------------------------------------------- /** * Validate Email Address * * @param string * @return bool */ public function validate_email($email) { if ( ! is_array($email)) { $this->_set_error_message('lang:email_must_be_array'); return FALSE; } foreach ($email as $val) { if ( ! $this->valid_email($val)) { $this->_set_error_message('lang:email_invalid_address', $val); return FALSE; } } return TRUE; } // -------------------------------------------------------------------- /** * Email Validation * * @param string * @return bool */ public function valid_email($address) { return (bool) preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix', $address); } // -------------------------------------------------------------------- /** * Clean Extended Email Address: Joe Smith * * @param string * @return string */ public function clean_email($email) { if ( ! is_array($email)) { return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email; } $clean_email = array(); foreach ($email as $addy) { $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy; } return $clean_email; } // -------------------------------------------------------------------- /** * Build alternative plain text message * * This public 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 * * @return string */ protected function _get_alt_message() { if ($this->alt_message != '') { return $this->word_wrap($this->alt_message, '76'); } $body = preg_match('/\(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body; $body = str_replace("\t", '', preg_replace('#