-1

I've tried this PHP code to extract email addresses from eml file. but it show me on browser like this:

Array ( [0] => [email protected] [1] => [email protected] [8] => [email protected] [16] => [email protected] [23] => [email protected] [26] => [email protected] [33] => [email protected] [35] => [email protected] [64] => [email protected] [67] => [email protected] [68] => [email protected] [87] => [email protected] [94] => [email protected] [97] => [email protected] [104] => [email protected] )` 

My PHP code is:

<?php $emails = array(); foreach(rglob("*.eml") as $eml){ $emlContent = file_get_contents($eml); preg_match_all('/([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6})/i', $emlContent, $matches, PREG_PATTERN_ORDER); for ($i = 0; $i < count($matches[1]); $i++) { $emails[] .= $matches[1][$i]; } } $emails = array_unique($emails); print_r($emails); function rglob($pattern='*', $flags = 0, $path=''){ $paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT); $files=glob($path.$pattern, $flags); foreach ($paths as $path) { $files=array_merge($files,rglob($pattern, $flags, $path)); } return $files; } ?> 

Now I want to extract only all the sender email addresses into an excel file. I've searched over the internet but did not got any solution.

Hope someone will help me to solve the problem. Thanks in advance

2 Answers 2

1

Try adding this header information to your function, this will start a download:

 header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=emails.xls"); header("Pragma: no-cache"); header("Expires: 0"); foreach($emails as $email){ echo $email; } 
Sign up to request clarification or add additional context in comments.

2 Comments

yes, it starting download. but I need to insert only the email addresses into the xls file. just need to extract the emails. @kisaragi
Just echo the values in a foreach loop.
0

Have you tried parsing the header inside the eml file?

The header ends when two consecutive line ends occur in a file.

For example:

list($header, body) = explode("\n\n", file_get_contents("mail.eml")); $headers = http_parse_headers ($header); var_dump(headers); 

Related: How do I get just the text content from a multipart email?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.