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 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');