0) { return true; } else { return false; } } else { if (($value != '') && ($value != 'NULL') && (strlen(trim($value)) > 0)) { return true; } else { return false; } } } class CEmail { var $html; var $text; var $output; var $html_text; var $html_images; var $image_types; var $build_params; var $attachments; var $headers; function CEmail($headers = '') { if ($headers == '') $headers = array(); $this->html_images = array(); $this->headers = array(); if (CEmail_LINEFEED == 'CRLF') { $this->lf = "\r\n"; } else { $this->lf = "\n"; } $this->image_types = array('gif' => 'image/gif', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpe' => 'image/jpeg', 'bmp' => 'image/bmp', 'png' => 'image/png', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'swf' => 'application/x-shockwave-flash'); $this->build_params['html_encoding'] = 'quoted-printable'; $this->build_params['text_encoding'] = '7bit'; $this->build_params['html_charset'] = constant('CHARSET'); $this->build_params['text_charset'] = constant('CHARSET'); $this->build_params['text_wrap'] = 998; $this->headers[] = 'MIME-Version: 1.0'; reset($headers); while (list(,$value) = each($headers)) { if (tep_not_null($value)) { $this->headers[] = $value; } } } function get_file($filename) { $return = ''; if ($fp = fopen($filename, 'rb')) { while (!feof($fp)) { $return .= fread($fp, 1024); } fclose($fp); return $return; } else { return false; } } function find_html_images($images_dir) { while (list($key, ) = each($this->image_types)) { $extensions[] = $key; } preg_match_all('/"([^"]+\.(' . implode('|', $extensions).'))"/Ui', $this->html, $images); for ($i=0; $ihtml = str_replace($images[1][$i], basename($images[1][$i]), $this->html); } } if (tep_not_null($html_images)) { $html_images = array_unique($html_images); sort($html_images); for ($i=0; $iget_file($images_dir . $html_images[$i])) { $content_type = $this->image_types[substr($html_images[$i], strrpos($html_images[$i], '.') + 1)]; $this->add_html_image($image, basename($html_images[$i]), $content_type); } } } } function add_text($text = '') { $this->text = $text; } function add_html($html, $text = NULL, $images_dir = NULL) { $this->html = $html; $this->html_text = $text; if (isset($images_dir)) $this->find_html_images($images_dir); } function add_html_image($file, $name = '', $c_type='application/octet-stream') { $this->html_images[] = array('body' => $file, 'name' => $name, 'c_type' => $c_type, 'cid' => md5(uniqid(time()))); } function add_attachment($file, $name = '', $c_type='application/octet-stream', $encoding = 'base64') { $this->attachments[] = array('body' => $file, 'name' => $name, 'c_type' => $c_type, 'encoding' => $encoding); } function add_text_part(&$obj, $text) { $params['content_type'] = 'text/plain'; $params['encoding'] = $this->build_params['text_encoding']; $params['charset'] = $this->build_params['text_charset']; if (is_object($obj)) { return $obj->addSubpart($text, $params); } else { return new mime($text, $params); } } function add_html_part(&$obj) { $params['content_type'] = 'text/html'; $params['encoding'] = $this->build_params['html_encoding']; $params['charset'] = $this->build_params['html_charset']; if (is_object($obj)) { return $obj->addSubpart($this->html, $params); } else { return new mime($this->html, $params); } } function add_mixed_part() { $params['content_type'] = 'multipart/mixed'; return new mime('', $params); } function add_alternative_part(&$obj) { $params['content_type'] = 'multipart/alternative'; if (is_object($obj)) { return $obj->addSubpart('', $params); } else { return new mime('', $params); } } function add_related_part(&$obj) { $params['content_type'] = 'multipart/related'; if (is_object($obj)) { return $obj->addSubpart('', $params); } else { return new mime('', $params); } } function add_html_image_part(&$obj, $value) { $params['content_type'] = $value['c_type']; $params['encoding'] = 'base64'; $params['disposition'] = 'inline'; $params['dfilename'] = $value['name']; $params['cid'] = $value['cid']; $obj->addSubpart($value['body'], $params); } function add_attachment_part(&$obj, $value) { $params['content_type'] = $value['c_type']; $params['encoding'] = $value['encoding']; $params['disposition'] = 'attachment'; $params['dfilename'] = $value['name']; $obj->addSubpart($value['body'], $params); } function build_message($params = '') { if ($params == '') $params = array(); if (count($params) > 0) { reset($params); while(list($key, $value) = each($params)) { $this->build_params[$key] = $value; } } if (tep_not_null($this->html_images)) { reset($this->html_images); while (list(,$value) = each($this->html_images)) { $this->html = str_replace($value['name'], 'cid:' . $value['cid'], $this->html); } } $null = NULL; $attachments = ((tep_not_null($this->attachments)) ? true : false); $html_images = ((tep_not_null($this->html_images)) ? true : false); $html = ((tep_not_null($this->html)) ? true : false); $text = ((tep_not_null($this->text)) ? true : false); switch (true) { case (($text == true) && ($attachments == false)): $message = $this->add_text_part($null, $this->text); break; case (($text == false) && ($attachments == true) && ($html == false)): $message = $this->add_mixed_part(); for ($i=0; $iattachments); $i++) { $this->add_attachment_part($message, $this->attachments[$i]); } break; case (($text == true) && ($attachments == true)): $message = $this->add_mixed_part(); $this->add_text_part($message, $this->text); for ($i=0; $iattachments); $i++) { $this->add_attachment_part($message, $this->attachments[$i]); } break; case (($html == true) && ($attachments == false) && ($html_images == false)): if (tep_not_null($this->html_text)) { $message = $this->add_alternative_part($null); $this->add_text_part($message, $this->html_text); $this->add_html_part($message); } else { $message = $this->add_html_part($null); } break; case (($html == true) && ($attachments == false) && ($html_images == true)): if (tep_not_null($this->html_text)) { $message = $this->add_alternative_part($null); $this->add_text_part($message, $this->html_text); $related = $this->add_related_part($message); } else { $message = $this->add_related_part($null); $related = $message; } $this->add_html_part($related); for ($i=0; $ihtml_images); $i++) { $this->add_html_image_part($related, $this->html_images[$i]); } break; case (($html == true) && ($attachments == true) && ($html_images == false)): $message = $this->add_mixed_part(); if (tep_not_null($this->html_text)) { $alt = $this->add_alternative_part($message); $this->add_text_part($alt, $this->html_text); $this->add_html_part($alt); } else { $this->add_html_part($message); } for ($i=0; $iattachments); $i++) { $this->add_attachment_part($message, $this->attachments[$i]); } break; case (($html == true) && ($attachments == true) && ($html_images == true)): $message = $this->add_mixed_part(); if (tep_not_null($this->html_text)) { $alt = $this->add_alternative_part($message); $this->add_text_part($alt, $this->html_text); $rel = $this->add_related_part($alt); } else { $rel = $this->add_related_part($message); } $this->add_html_part($rel); for ($i=0; $ihtml_images); $i++) { $this->add_html_image_part($rel, $this->html_images[$i]); } for ($i=0; $iattachments); $i++) { $this->add_attachment_part($message, $this->attachments[$i]); } break; } if ( (isset($message)) && (is_object($message)) ) { $output = $message->encode(); $this->output = $output['body']; reset($output['headers']); while (list($key, $value) = each($output['headers'])) { $headers[] = $key . ': ' . $value; } $this->headers = array_merge($this->headers, $headers); return true; } else { return false; } } function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') { $to = (($to_name != '') ? '"' . $to_name . '" <' . $to_addr . '>' : $to_addr); $from = (($from_name != '') ? '"' . $from_name . '" <' . $from_addr . '>' : $from_addr); if (is_string($headers)) { $headers = explode($this->lf, trim($headers)); } for ($i=0; $ioutput, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers)); } else { return mail($to, $subject, $this->output, 'From: '.$from.$this->lf.implode($this->lf, $this->headers).$this->lf.implode($this->lf, $xtra_headers)); } } function get_rfc822($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') { $date = 'Date: ' . date('D, d M y H:i:s'); $to = (($to_name != '') ? 'To: "' . $to_name . '" <' . $to_addr . '>' : 'To: ' . $to_addr); $from = (($from_name != '') ? 'From: "' . $from_name . '" <' . $from_addr . '>' : 'From: ' . $from_addr); if (is_string($subject)) { $subject = 'Subject: ' . $subject; } if (is_string($headers)) { $headers = explode($this->lf, trim($headers)); } for ($i=0; $iheaders, $xtra_headers); return $date . $this->lf . $from . $this->lf . $to . $this->lf . $subject . $this->lf . implode($this->lf, $headers) . $this->lf . $this->lf . $this->output; } } class mime { var $_encoding; var $_subparts; var $_encoded; var $_headers; var $_body; function mime($body, $params = '') { if ($params == '') $params = array(); if (EMAIL_LINEFEED == 'CRLF') { $this->lf = "\r\n"; } else { $this->lf = "\n"; } reset($params); while (list($key, $value) = each($params)) { switch ($key) { case 'content_type': $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : ''); break; case 'encoding': $this->_encoding = $value; $headers['Content-Transfer-Encoding'] = $value; break; case 'cid': $headers['Content-ID'] = '<' . $value . '>'; break; case 'disposition': $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : ''); break; case 'dfilename': if (isset($headers['Content-Disposition'])) { $headers['Content-Disposition'] .= '; filename="' . $value . '"'; } else { $dfilename = $value; } break; case 'description': $headers['Content-Description'] = $value; break; case 'charset': if (isset($headers['Content-Type'])) { $headers['Content-Type'] .= '; charset="' . $value . '"'; } else { $charset = $value; } break; } } if (!isset($_headers['Content-Type'])) { $_headers['Content-Type'] = 'text/plain'; } $this->_encoded = array(); $this->_headers = $headers; $this->_body = $body; } function encode() { $encoded = $this->_encoded; if (tep_not_null($this->_subparts)) { $boundary = '=_' . md5(uniqid(tep_rand()) . microtime()); $this->_headers['Content-Type'] .= ';' . $this->lf . chr(9) . 'boundary="' . $boundary . '"'; for ($i=0; $i_subparts); $i++) { $headers = array(); $_subparts = $this->_subparts[$i]; $tmp = $_subparts->encode(); reset($tmp['headers']); while (list($key, $value) = each($tmp['headers'])) { $headers[] = $key . ': ' . $value; } $subparts[] = implode($this->lf, $headers) . $this->lf . $this->lf . $tmp['body']; } $encoded['body'] = '--' . $boundary . $this->lf . implode('--' . $boundary . $this->lf, $subparts) . '--' . $boundary.'--' . $this->lf; } else { $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . $this->lf; } $encoded['headers'] = $this->_headers; return $encoded; } function addSubPart($body, $params) { $this->_subparts[] = new mime($body, $params); return $this->_subparts[count($this->_subparts) - 1]; } function _getEncodedData($data, $encoding) { switch ($encoding) { case '7bit': return $data; break; case 'quoted-printable': return $this->_quotedPrintableEncode($data); break; case 'base64': return rtrim(chunk_split(base64_encode($data), 76, $this->lf)); break; } } function _quotedPrintableEncode($input , $line_max = 76) { $lines = preg_split("/\r\n|\r|\n/", $input); $eol = $this->lf; $escape = '='; $output = ''; while (list(, $line) = each($lines)) { $linlen = strlen($line); $newline = ''; for ($i = 0; $i < $linlen; $i++) { $char = substr($line, $i, 1); $dec = ord($char); if ( ($dec == 32) && ($i == ($linlen - 1)) ) { $char = '=20'; } elseif ($dec == 9) { } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { $char = $escape . strtoupper(sprintf('%02s', dechex($dec))); } if ((strlen($newline) + strlen($char)) >= $line_max) { $output .= $newline . $escape . $eol; $newline = ''; } $newline .= $char; } $output .= $newline . $eol; } $output = substr($output, 0, -1 * strlen($eol)); return $output; } } ?>