Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getFrom() method #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions PlancakeEmailParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down