From d9a683b82c3b6a40b17cb17d850a87b87390835e Mon Sep 17 00:00:00 2001 From: timination Date: Mon, 22 Jun 2015 22:41:48 +0100 Subject: [PATCH 1/2] Update PlancakeEmailParser.php Added getFrom() method to retrieve 'from' email address from headers. Throws new exception if no sender can be found. --- PlancakeEmailParser.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/PlancakeEmailParser.php b/PlancakeEmailParser.php index 59e6cee..19f458d 100644 --- a/PlancakeEmailParser.php +++ b/PlancakeEmailParser.php @@ -141,6 +141,31 @@ public function getSubject() return $ret; } + /** + * + * @return string (in UTF-8 format) + * @throws Exception if a subject header is not found + */ + public function getFrom() + { + if (!isset($this->rawFields['from'])) + { + throw new Exception("Couldn't find the sender of the email"); + } + + $ret = ''; + + if ($this->isImapExtensionAvailable) { + foreach (imap_mime_header_decode($this->rawFields['from']) as $h) { // subject can span into several lines + $charset = ($h->charset == 'default') ? 'US-ASCII' : $h->charset; + $ret .= iconv($charset, "UTF-8//TRANSLIT", $h->text); + } + } else { + $ret = utf8_encode(iconv_mime_decode($this->rawFields['from'])); + } + + return $ret; + } /** * * @return array From a8e2935a7878fa00fc2336d58008aed3bb50de09 Mon Sep 17 00:00:00 2001 From: timination Date: Mon, 22 Jun 2015 22:43:29 +0100 Subject: [PATCH 2/2] Update README.txt --- README.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/README.txt b/README.txt index 7776202..e862777 100644 --- a/README.txt +++ b/README.txt @@ -15,6 +15,7 @@ $emailParser = new PlancakeEmailParser(file_get_contents($emailPath)); $emailTo = $emailParser->getTo(); $emailSubject = $emailParser->getSubject(); $emailCc = $emailParser->getCc(); +$emailFrom = $emailParser->getFrom(); // ... or you can use the 'general purpose' method getHeader() $emailDeliveredToHeader = $emailParser->getHeader('Delivered-To');