private function parseMessage($message)
    {
        if (!$message) {
            return false;
        }

        $startLine = null;
        $headers = [];
        $body = '';

        // Iterate over each line in the message, accounting for line endings
        $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
        for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {

            $line = $lines[$i];

            // If two line breaks were encountered, then this is the end of body
            if (empty($line)) {
                if ($i < $totalLines - 1) {
                    $body = implode('', array_slice($lines, $i + 2));
                }
                break;
            }

            // Parse message headers
            if (!$startLine) {
                $startLine = explode(' ', $line, 3);
            } elseif (strpos($line, ':')) {
                $parts = explode(':', $line, 2);
                $key = trim($parts[0]);
                $value = isset($parts[1]) ? trim($parts[1]) : '';
                if (!isset($headers[$key])) {
                    $headers[$key] = $value;
                } elseif (!is_array($headers[$key])) {
                    $headers[$key] = [$headers[$key], $value];
                } else {
                    $headers[$key][] = $value;
                }
            }
        }

        return [
            'start_line' => $startLine,
            'headers'    => $headers,
            'body'       => $body
        ];
    }
private function parseMessage($message)
    {
        if (!$message) {
            return false;
        }

        $startLine = null;
        $headers = [];
        $body = '';

        // Iterate over each line in the message, accounting for line endings
        $lines = preg_split('/(\\r?\\n)/', $message, -1, PREG_SPLIT_DELIM_CAPTURE);
        for ($i = 0, $totalLines = count($lines); $i < $totalLines; $i += 2) {

            $line = $lines[$i];

            // If two line breaks were encountered, then this is the end of body
            if (empty($line)) {
                if ($i < $totalLines - 1) {
                    $body = implode('', array_slice($lines, $i + 2));
                }
                break;
            }

            // Parse message headers
            if (!$startLine) {
                $startLine = explode(' ', $line, 3);
            } elseif (strpos($line, ':')) {
                $parts = explode(':', $line, 2);
                $key = trim($parts[0]);
                $value = isset($parts[1]) ? trim($parts[1]) : '';
                if (!isset($headers[$key])) {
                    $headers[$key] = $value;
                } elseif (!is_array($headers[$key])) {
                    $headers[$key] = [$headers[$key], $value];
                } else {
                    $headers[$key][] = $value;
                }
            }
        }

        return [
            'start_line' => $startLine,
            'headers'    => $headers,
            'body'       => $body
        ];
    }
private function parseMessage ( $ message ) { if ( ! $ message ) { return false ; } $ startLine = null ; $ headers = [ ] ; $ body = '' ; // Iterate over each line in the message, accounting for line endings $ lines = preg_split ( '/(\\r?\\n)/' , $ message , - 1 , PREG_SPLIT_DELIM_CAPTURE ) ; for ( $ i = 0 , $ totalLines = count ( $ lines ) ; $ i < $ totalLines ; $ i += 2 ) { $ line = $ lines [ $ i ] ; // If two line breaks were encountered, then this is the end of body if ( empty ( $ line ) ) { if ( $ i < $ totalLines - 1 ) { $ body = implode ( '' , array_slice ( $ lines , $ i + 2 ) ) ; } break ; } // Parse message headers if ( ! $ startLine ) { $ startLine = explode ( ' ' , $ line , 3 ) ; } elseif ( strpos ( $ line , ':' ) ) { $ parts = explode ( ':' , $ line , 2 ) ; $ key = trim ( $ parts [ 0 ] ) ; $ value = isset ( $ parts [ 1 ] ) ? trim ( $ parts [ 1 ] ) : '' ; if ( ! isset ( $ headers [ $ key ] ) ) { $ headers [ $ key ] = $ value ; } elseif ( ! is_array ( $ headers [ $ key ] ) ) { $ headers [ $ key ] = [ $ headers [ $ key ] , $ value ] ; } else { $ headers [ $ key ] [ ] = $ value ; } } } return [ 'start_line' => $ startLine , 'headers' => $ headers , 'body' => $ body ] ; }
Parse a message into parts

@param string $message Message to parse

@return array|bool
Parse a message into parts