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

Enhancements - Attachment processing. #27

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
111 changes: 110 additions & 1 deletion PlancakeEmailParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,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
*
Expand Down Expand Up @@ -292,9 +312,98 @@ public function getPlainBody()
*/
public function getHTMLBody()
{
return $this->getBody(self::HTML);
$body = $this->getBody(self::HTML);

$tmp = explode("</html>",$body);
return $tmp[0]."</html>"; // omit any trailing data.

// 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.

$parts = explode($boundary, $this->emailRawContent);

$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
*
Expand Down
2 changes: 2 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down