douna2917 2016-01-13 18:08
浏览 156
已采纳

如何使用php从eml文件中仅将电子邮件地址提取到excel文件中

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

Array ( [0] => jessy87421@gmail.com
        [1] => gmail.com@gmail.com 
        [8] => tkm.ab234@gmail.com 
       [16] => rjsajal99@gmail.com 
       [23] => mrwsamson@hotmail.com 
       [26] => p7896546@gmail.com 
       [33] => COL401-EAS320FA71AB8D0DB70A86C0BDAACA0@phx.gbl 
       [35] => Mrwsamson@hotmail.com [64] => stancojim@yahoo.com 
       [67] => 284187.29155.bm@omp1028.mail.bf1.yahoo.com [68] => 1452613452.95435.YahooMailMobile@web161005.mail.bf1.yahoo.com 
       [87] => prantomollick9911@gmail.com 
       [94] => quakenbush_87@hotmail.com 
       [97] => k018707707@gmail.com 
      [104] => BLU403-EAS1665B11DD307579691E9A1A96CA0@phx.gbl 
   )`

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条回答 默认 最新

  • dow56114 2016-01-13 18:19
    关注

    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;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?