$headers, 'body' => $body ); } return; } $parts = self::splitMime($body, $boundary); for($i=0; $i $headers, 'body' => $body); } else if($headers['content-type']['type'] == "message/rfc822") { self::parseMessage($body, $result); } } } } public static function splitMime($body, $boundary) { $start = 0; $res = array(); $body = self::remove_LF($body); // Extract the mime parts excluding the boundary itself $p = strpos($body, '--' . $boundary . "\n", $start); if($p === false) { // no parts found! return array(); } // Position after first boundary line $start = $p + 3 + strlen($boundary); while(($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) { $res[] = substr($body, $start, $p-$start); $start = $p + 3 + strlen($boundary); } // No more parts, find end boundary $p = strpos($body, '--' . $boundary . '--', $start); if($p === false) { return array(); } // The remaining part also needs to be parsed: $res[] = substr($body, $start, $p - $start); return $res; } public static function splitMessage($message, &$headers, &$body, $EOL = "\n") { self::splitMessageRaw($message, $headers, $body); $headers = self::splitHeaders($headers); } public static function splitMessageRaw($message, &$headers, &$body, $EOL = "\n") { $headers = []; $body = ''; $message = self::remove_LF($message); // Find an empty line between headers and body, otherwise we got a header-only message if(strpos($message, $EOL . $EOL)) { list($headers, $body) = explode($EOL . $EOL, $message, 2); } else { $headers = $message; } } public static function splitHeaders($headers) { $headers = self::headersToArray($headers); // normalize header names foreach ($headers as $name => $header) { $lower = strtolower($name); if($lower == $name) { continue; } unset($headers[$name]); if(!isset($headers[$lower])) { $headers[$lower] = $header; continue; } if(is_array($headers[$lower])) { $headers[$lower][] = $header; continue; } $headers[$lower] = array($headers[$lower], $header); } // Add some default values, if they are missing if(!isset($headers['content-type'])) { $headers['content-type'] = 'text/plain'; } // I saw a dumb email (it was a spam, though) having two Date: lines. // In this case we take the first date, and discard the rest if(isset($headers[self::HEADER_FIELDS[4]]) && is_array($headers[self::HEADER_FIELDS[4]])) { $headers[self::HEADER_FIELDS[4]] = $headers[self::HEADER_FIELDS[4]][0]; } for($i=0; $i $name) { $name = strtolower($name); if($matches[2][$key][0] == '"') { $split[$name] = substr($matches[2][$key], 1, -1); } else { $split[$name] = $matches[2][$key]; } } return $split; } public static function remove_LF($message = '') { return str_replace("\r", "", $message); //return preg_replace("/\r/", "", $message); } public static function getBoundary($headers = array()) { if(isset($headers['content-type']['boundary'])) { return $headers['content-type']['boundary']; } return ''; } public static function fixMimeBodyPart($headers = array(), $body = '') { if(isset($headers['content-transfer-encoding'])) { if($headers['content-transfer-encoding'] == 'quoted-printable') { $body = quoted_printable_decode($body); } if($headers['content-transfer-encoding'] == 'base64') { $body = base64_decode($body); } } if(isset($headers['content-type']['charset'])) { $body = iconv($headers['content-type']['charset'], 'utf-8' . '//IGNORE', $body); } if(strtolower($headers['content-type']['type']) == 'text/plain') { $body = self::escape_lt_gt_symbols($body); $body = preg_replace("/\n/", "
\n", $body); $body = "\n" . self::printNicely($body); } return $body; } public static function escape_lt_gt_symbols($s = '') { $s = preg_replace("//", ">", $s); return $s; } public static function printNicely($s = '') { $k = 0; $nice = ""; $x = explode(" ", $s); for($i=0; $i 70){ $nice .= "\n"; $k = 0; } } return $nice; } }