2016-01-13 156 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?