Created
October 14, 2019 19:15
-
-
Save leonidaswander/d1906cec6b5ff194b9dd275d8ab6743b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Leitor emails Imap com download de anexo | |
* extension=php_imap.dll | |
*/ | |
/* realiza a conexão com as suas credenciais */ | |
$mailbox = '{meuhost.com.br:993/imap/ssl}INBOX'; | |
$username = 'emailt@meuhost.com.br'; | |
$password = 'MinhaSenha'; | |
$pasta_salvar_anexo = "email_anexo"; //baixará o anexo e salvará nesta pasta | |
$inbox = imap_open($mailbox,$username,$password) or die('Cannot connect to email: ' . imap_last_error()); | |
$emails = imap_search($inbox,'UNSEEN'); //Exibe apenas os emails não lidos: - https://www.php.net/manual/pt_BR/function.imap-search.php | |
rsort($emails); | |
foreach($emails as $mail) { | |
$headerInfo = imap_headerinfo($inbox,$mail); | |
echo 'Assunto: ' .iconv_mime_decode($headerInfo->subject).'<br/>'; | |
echo 'Para: ' .$headerInfo->toaddress.'<br/>'; | |
echo 'Data: ' .date("d/m/Y H:i:s", strtotime($headerInfo->date)).'<br/>'; | |
echo 'De: ' .iconv_mime_decode($headerInfo->fromaddress).'<br/>'; | |
echo 'Responder:' .iconv_mime_decode($headerInfo->reply_toaddress).'<br/>'; | |
$structure = imap_fetchstructure($inbox,$mail); | |
$attachments = array(); | |
////////ANEXO | |
/* checa se tem anexos... */ | |
if(isset($structure->parts) && count($structure->parts)) | |
{ | |
for($i = 0; $i < count($structure->parts); $i++) | |
{ | |
$attachments[$i] = array( | |
'is_attachment' => false, | |
'filename' => '', | |
'name' => '', | |
'attachment' => '' | |
); | |
if($structure->parts[$i]->ifdparameters) | |
{ | |
foreach($structure->parts[$i]->dparameters as $object) | |
{ | |
if(strtolower($object->attribute) == 'filename') | |
{ | |
$attachments[$i]['is_attachment'] = true; | |
$attachments[$i]['filename'] = $object->value; | |
} | |
} | |
} | |
if($structure->parts[$i]->ifparameters) | |
{ | |
foreach($structure->parts[$i]->parameters as $object) | |
{ | |
if(strtolower($object->attribute) == 'name') | |
{ | |
$attachments[$i]['is_attachment'] = true; | |
$attachments[$i]['name'] = $object->value; | |
} | |
} | |
} | |
if($attachments[$i]['is_attachment']) | |
{ | |
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $mail, $i+1); | |
/* 4 = QUOTED-PRINTABLE encoding */ | |
if($structure->parts[$i]->encoding == 3) | |
{ | |
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); | |
} | |
/* 3 = BASE64 encoding */ | |
elseif($structure->parts[$i]->encoding == 4) | |
{ | |
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); | |
} | |
} | |
} | |
} | |
/* salvando o anexo */ | |
foreach($attachments as $attachment) | |
{ | |
if($attachment['is_attachment'] == 1) | |
{ | |
$filename = $attachment['name']; | |
if(empty($filename)) $filename = $attachment['filename']; | |
if(empty($filename)) $filename = time() . ".dat"; | |
if(!is_dir($pasta_salvar_anexo)) | |
{ | |
mkdir($pasta_salvar_anexo); | |
} | |
$fp = fopen("./". $pasta_salvar_anexo ."/". $filename, "w+"); | |
fwrite($fp, $attachment['attachment']); | |
fclose($fp); | |
} | |
} | |
////////ANEXO | |
echo '<hr>'; | |
} | |
// fechar conexão | |
imap_expunge($inbox); | |
imap_close($inbox); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment