From 1b94069762c197d72e47ee6d9050f213e63f0413 Mon Sep 17 00:00:00 2001 From: Tolga Date: Tue, 12 Mar 2013 01:16:52 -0300 Subject: [PATCH 1/3] Update PlancakeEmailParser.php Added three new functions: `getFrom()`, `getFromName()` and `getFromAddress()` They can be used to return the sender's display name and e-mail address. Example: `$emailParser = new PlancakeEmailParser($email); $emailFrom = $emailParser->getFrom(); $emailFromName = $emailParser->getFromName(); $emailFromAddress = $emailParser->getFromAddress();` `$emailFrom`: `"John Doe" ` `$emailFromName`: `John Doe` `$emailFromAddress`: `john.doe@foo.com` --- PlancakeEmailParser.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/PlancakeEmailParser.php b/PlancakeEmailParser.php index bc73d3c..764990d 100644 --- a/PlancakeEmailParser.php +++ b/PlancakeEmailParser.php @@ -43,6 +43,10 @@ class PlancakeEmailParser { const PLAINTEXT = 1; const HTML = 2; + const FROM = 0; + const FROMNAME = 1; + const FROMADDRESS = 2; + /** * * @var boolean @@ -333,5 +337,40 @@ private function isLineStartingWithPrintableChar($line) { return preg_match('/^[A-Za-z]/', $line); } + + /** + * + * @return string + * @throws Exception if a to header is not found or if there is no sender + */ + public function getFrom($returnIndex=self::FROM) + { + if ( (!isset($this->rawFields['from'])) || (!count($this->rawFields['from']))) + { + throw new Exception("Couldn't find the sender of the email"); + } + preg_match('/\"([^\"]+)\" <([^>]+)>/', $this->rawFields['from'], $matches); + return $matches[$returnIndex]; + } + + /** + * + * @return string + * @throws Exception if a to header is not found or if there is no sender + */ + public function getFromName() + { + return $this->getFrom(self::FROMNAME); + } + + /** + * + * @return string + * @throws Exception if a to header is not found or if there is no sender + */ + public function getFromAddress() + { + return $this->getFrom(self::FROMADDRESS); + } } ?> From dcd3a453e56a9eaf9b1136cdd8961213cb3434fb Mon Sep 17 00:00:00 2001 From: "Tolga O." Date: Tue, 12 Mar 2013 20:20:38 -0300 Subject: [PATCH 2/3] Added more regex to from name/address parsing. I found out that some e-mail clients (i.e. iPhone Mail app) does not include quotes in `from` header. The updated code correctly parses the following forms: `"John Doe" John Doe ` --- PlancakeEmailParser.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PlancakeEmailParser.php b/PlancakeEmailParser.php index 764990d..9432442 100644 --- a/PlancakeEmailParser.php +++ b/PlancakeEmailParser.php @@ -349,8 +349,9 @@ public function getFrom($returnIndex=self::FROM) { throw new Exception("Couldn't find the sender of the email"); } - preg_match('/\"([^\"]+)\" <([^>]+)>/', $this->rawFields['from'], $matches); - return $matches[$returnIndex]; + if (!preg_match('/\"([^\"]+)\" <([^>]+)>/', $this->rawFields['from'], $matches)) { + (preg_match('/([^<]+) <([^>]+)>/', $this->rawFields['from'], $matches)); + } } /** From cbf1dc0d96bb03e1162fda09c563dac6ed55e805 Mon Sep 17 00:00:00 2001 From: "Tolga O." Date: Tue, 12 Mar 2013 20:23:38 -0300 Subject: [PATCH 3/3] Update PlancakeEmailParser.php --- PlancakeEmailParser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PlancakeEmailParser.php b/PlancakeEmailParser.php index 9432442..69f29d2 100644 --- a/PlancakeEmailParser.php +++ b/PlancakeEmailParser.php @@ -350,8 +350,8 @@ public function getFrom($returnIndex=self::FROM) throw new Exception("Couldn't find the sender of the email"); } if (!preg_match('/\"([^\"]+)\" <([^>]+)>/', $this->rawFields['from'], $matches)) { - (preg_match('/([^<]+) <([^>]+)>/', $this->rawFields['from'], $matches)); - } + (preg_match('/([^<]+) <([^>]+)>/', $this->rawFields['from'], $matches)); + } } /**