From 14d76851a0aaa6c0db714c63ccfb3fab0b32da7f Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 16 Apr 2015 15:13:01 -0700 Subject: [PATCH 1/2] getHTMLBody() now only returns HTML and not attachment data. getAttachments() method added. (including inline attachments) --- PlancakeEmailParser.php | 110 +++++++++++++++++++++++++++++++++++++++- README.txt | 2 + 2 files changed, 111 insertions(+), 1 deletion(-) diff --git a/PlancakeEmailParser.php b/PlancakeEmailParser.php index 59e6cee..cc5fb00 100644 --- a/PlancakeEmailParser.php +++ b/PlancakeEmailParser.php @@ -55,6 +55,13 @@ class PlancakeEmailParser { */ private $emailRawContent; + + /** + * + * @var string + */ + private $rawBodyContent; + /** * * @var associative array @@ -275,6 +282,12 @@ public function getBody($returnType=self::PLAINTEXT) } } + if($returnType == self::HTML) // prevent doing the same job twice. + { + $this->rawBodyContent = $body; + } + + return $body; } @@ -292,9 +305,104 @@ public function getPlainBody() */ public function getHTMLBody() { - return $this->getBody(self::HTML); + $body = $this->getBody(self::HTML); + + $tmp = explode("",$body); + return $tmp[0].""; // omit any attachments. + +// return $body; } + + /** + * Detect and return an array of attachments and their data. + * @return array|bool + */ + public function getAttachments() + { + + if(preg_match('/multipart\/mixed;\s*?boundary=(.*)/i', $this->rawFields['content-type'], $match)) + { + $boundary = trim($match[1],'"'); // Thunderbird uses quotes, apple mail doesn't. + + if(empty($this->rawBodyContent)) + { + $this->rawBodyContent = $this->getBody(self::HTML, true); + } + + $parts = explode($boundary, $this->rawBodyContent); + + $attach = array(); + + foreach($parts as $part) + { + if($data = $this->processParts($part)) + { + $attach[] = $data; + } + } + + return $attach; + } + + + return false; + + } + + + /** + * Process a single attachment. + * @param $part + * @return array|bool + */ + private function processParts($part) + { + if(empty($part) || ($part == '--')) + { + return false; + } + + list($head,$attach) = explode("\n\n",$part,2); + + $ret = array(); + + $repl = array('filename=', '"'); + + $h = explode("\n",$head); + + foreach($h as $line) + { + $line = trim($line); + if(strpos($line, ': ')!==false) + { + list($key,$val) = explode(': ',$line,2); + list($value,$junk) = explode(";",$val); + $key = strtolower($key); + $ret[$key] = rtrim($value,"\n\t\r;"); + } + elseif(strpos($line, 'filename=')!==false) + { + $ret['filename'] = str_replace($repl, '', $line); // clean up without using regexp. + } + + } + + + if(!empty($ret['content-type']) && ($ret['content-disposition'] == 'inline' || $ret['content-disposition'] == 'attachment')) + { + $ret['content'] = trim($attach); + $ret['content'] = rtrim($ret['content'],'-\n'); + + return $ret; + } + + + return false; + + } + + /** * N.B.: if the header doesn't exist an empty string is returned * diff --git a/README.txt b/README.txt index 7776202..3e3574a 100644 --- a/README.txt +++ b/README.txt @@ -20,6 +20,8 @@ $emailDeliveredToHeader = $emailParser->getHeader('Delivered-To'); $emailBody = $emailParser->getPlainBody(); +$emailAttachments = $emailParser->getAttachments(); + For more info: https://github.com/plancake/official-library-php-email-parser From c78092a0d8ef4f3234979fd66f34b9ae7faf8102 Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 16 Apr 2015 18:07:02 -0700 Subject: [PATCH 2/2] added getFrom() method and improved attachment detection. --- PlancakeEmailParser.php | 43 +++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/PlancakeEmailParser.php b/PlancakeEmailParser.php index cc5fb00..fc24c4e 100644 --- a/PlancakeEmailParser.php +++ b/PlancakeEmailParser.php @@ -55,13 +55,6 @@ class PlancakeEmailParser { */ private $emailRawContent; - - /** - * - * @var string - */ - private $rawBodyContent; - /** * * @var associative array @@ -176,6 +169,26 @@ public function getTo() return explode(',', $this->rawFields['to']); } + + /** + * @return string + */ + function getFrom() + { + + $email = trim($this->rawFields['from'],' >'); + + if(strpos($email,'<') !== false) + { + list($junk,$address) = explode("<",$email); + return $address; + } + + return $email; + } + + + /** * return string - UTF8 encoded * @@ -282,12 +295,6 @@ public function getBody($returnType=self::PLAINTEXT) } } - if($returnType == self::HTML) // prevent doing the same job twice. - { - $this->rawBodyContent = $body; - } - - return $body; } @@ -308,7 +315,7 @@ public function getHTMLBody() $body = $this->getBody(self::HTML); $tmp = explode("",$body); - return $tmp[0].""; // omit any attachments. + return $tmp[0].""; // omit any trailing data. // return $body; } @@ -325,12 +332,7 @@ public function getAttachments() { $boundary = trim($match[1],'"'); // Thunderbird uses quotes, apple mail doesn't. - if(empty($this->rawBodyContent)) - { - $this->rawBodyContent = $this->getBody(self::HTML, true); - } - - $parts = explode($boundary, $this->rawBodyContent); + $parts = explode($boundary, $this->emailRawContent); $attach = array(); @@ -345,7 +347,6 @@ public function getAttachments() return $attach; } - return false; }