简体   繁体   English

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

[英]How can I extract only email addresses into an excel file from eml file using php

I've tried this PHP code to extract email addresses from eml file.我试过这个 PHP 代码从 eml 文件中提取电子邮件地址。 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代码是:

<?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.现在我只想将所有发件人电子邮件地址提取到一个 excel 文件中。 I've searched over the internet but did not got any solution.我已经在互联网上搜索过,但没有得到任何解决方案。

Hope someone will help me to solve the problem.希望有人能帮我解决问题。 Thanks in advance提前致谢

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

Have you tried parsing the header inside the eml file?您是否尝试过解析 eml 文件中的标头?

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?相关: 如何仅从多部分电子邮件中获取文本内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM