2016-01-13 165 views
-1

我试过这个PHP代码从eml文件中提取电子邮件地址。但它表明我的浏览器是这样的:我怎样才能提取电子邮件地址从一个Excel文件从eml文件使用php

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] 
    )` 

我的PHP代码:

<?php 
    $emails = array(); 

    foreach(rglob("*.eml") as $eml){ 
    $emlContent = file_get_contents($eml); 
    preg_match_all('/([A-Z0-9._%+-][email protected][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; 
} 

?> 

现在我想只提取所有发件人的电子邮件地址到Excel文件。我在互联网上搜索,但没有得到任何解决方案。

希望有人能帮助我解决问题。提前 感谢

回答

1

尝试添加此头信息的功能,这将启动一个下载:

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; 
    } 
+0

是的,它开始下载。但我只需要将电子邮件地址插入到xls文件中。只需要提取电子邮件。 @kisaragi – sohag513

+1

只需在'foreach'循环中回显值。 – Kisaragi

+0

它正在工作......谢谢你 – sohag513

0

您是否尝试过解析的eml文件中的头?

当文件中出现两个连续行结束时,标题结束。

例如:

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

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