Código PHP para extraer correos electrónicos de carpeta de enviados, su código fuente y los metadatos obrantes en el servidor para dicho correo.
<?php /* Modo de uso: Para relevar los correos electrónicos del año 2012 c:\Imap\> php HotmailRecibidosSaveEml 2012 Inbox */ $username = 'juanpf@hotmail.com'; $password = '***********'; $folder = 'SENT'; // 'SENT' 'INBOX' $usernameTime = $username.date('H-i-s', time()); if(isset($argv[1])) { if($argv[1]=="all") { $anno = date("Y"); } else { $anno = $argv[1]; } } else { $anno = date("Y"); } $annoPrevio = $anno - 1; if(isset($argv[2])) { $folder = $argv[2]; } else die (" ingrese el segundo parámetro, ej. xx.php 2020 Inbox "); if($argv[1]=="all") { $searchString = ' BEFORE "31-Dec-'.$anno.'" '; } else $searchString = ' SINCE "31-Dec-'.$annoPrevio.'" BEFORE "31-Dec-'.$anno.'" '; // $searchString = 'SINCE "1-Jun-2020" BEFORE "31-Aug-2020" '; // 'TO "aherocomercial@gmail.com" '; // 'SINCE "31-Dec-2018" BEFORE "31-Jan-2019" ';// 'BCC "info@cemasrl.com.ar" '; echo "Carpeta: ".$folder." Filtro: ".$searchString; $server = '{imap-mail.outlook.com:993/imap/ssl/novalidate-cert}'.$folder; echo $server; $mailbox = imap_open($server, $username, $password) or die('Cannot connect to Mail Server: ' . imap_last_error()); try{ mkdir ("D:\\Imap\\$folder$username"); } catch(Exception $e) {echo "carpeta ya creada...";} $search = imap_search($mailbox, $searchString, SE_UID); // $searchString: SINCE "01-Jan-2016" BEFORE "01-Jan-2018" $txt= "Msgno;InternalDateNo;From;To;Date;InternalDate;Subject;UID;size;answered<BR>"; echo $txt; $myfile = file_put_contents("$folder$usernameTime.txt", $txt.PHP_EOL , FILE_APPEND | LOCK_EX); foreach ($search as $msg_id) { //Pega informações da mensagem $overview = imap_fetch_overview($mailbox, $msg_id, FT_UID); $val=$overview[0]; $msg=$val->msgno; //$from=$val->from; //$to = $val->to; $date=$val->date; $subj=iconv_mime_decode($val->subject); $uid = $val->uid; $message_id = $val->message_id; $size = $val->size ; $answered = $val->answered ; $header = imap_headerinfo ( $mailbox, $msg); $from = $header->from[0]->mailbox ."@". $header->from[0]->host; $to = $header->to[0]->mailbox ."@". $header->to[0]->host; $internalDate = date('Y-m-d H:i:s O',$header->udate); $internalDateNo = $header->udate; $txt = "$msg;$internalDateNo;$from;$to;$date;$internalDate;$subj;$uid;$size;$answered<BR>"; echo $txt; $myfile = file_put_contents("$folder$usernameTime.txt", $txt.PHP_EOL , FILE_APPEND | LOCK_EX); $headers = imap_fetchheader($mailbox, $msg, FT_PREFETCHTEXT); $body = imap_body($mailbox, $msg); file_put_contents("D:\\Imap\\$folder$username\\$msg.eml", $headers . $body); } imap_close($mailbox); ?>
Comentarios
Publicar un comentario