Ver Mensaje Individual
  #6 (permalink)  
Antiguo 23/08/2015, 20:41
sharedcode
 
Fecha de Ingreso: agosto-2015
Ubicación: Santiago
Mensajes: 5
Antigüedad: 8 años, 8 meses
Puntos: 0
Respuesta: Almacenar archivo XML recibido por Email

Aquí tienen la solución para obtener los adjuntos de un email.

Código PHP:
Ver original
  1. <?php
  2. #
  3. $hostname = '{mail.cocacola.cl:993/imap/ssl/novalidate-cert}INBOX';
  4. $username = '[email protected]';
  5. $password = 'suclavedelcorreo';
  6. $bandeja_entrada="xml/";
  7.  
  8. /* try to connect */
  9. $inbox = imap_open($hostname,$username,$password) or die('ERROR' . imap_last_error());
  10.  
  11. /* grab emails */
  12. $emails = imap_search($inbox, '');
  13.  
  14.  
  15.  
  16. /* if emails are returned, cycle through each... */
  17. if($emails) {
  18.  
  19.   /* begin output var */
  20.   $output = '';
  21.  
  22.   /* put the newest emails on top */
  23.   rsort($emails);
  24.  
  25.  
  26.  
  27.  
  28.     foreach($emails as $email_number) {
  29.  
  30.     /* get information specific to this email */
  31.     $overview = imap_fetc_overview($inbox,$email_number,0);
  32.     $message = imap_fetcbody($inbox,$email_number,2);
  33.     $structure = imap_fethstructure($inbox,$email_number);
  34.  
  35.  
  36.     //pre($overview);
  37.  
  38.  
  39.      $attachments = array();
  40.        if(isset($structure->parts) && count($structure->parts)) {
  41.          for($i = 0; $i < count($structure->parts); $i++) {
  42.            $attachments[$i] = array(
  43.               'is_attachment' => false,
  44.               'filename' => '',
  45.               'name' => '',
  46.               'attachment' => '');
  47.  
  48.            if($structure->parts[$i]->ifdparameters) {
  49.              foreach($structure->parts[$i]->dparameters as $object) {
  50.                if(strtolower($object->attribute) == 'filename') {
  51.                  $attachments[$i]['is_attachment'] = true;
  52.                  $attachments[$i]['filename'] = $object->value;
  53.                }
  54.              }
  55.            }
  56.  
  57.            if($structure->parts[$i]->ifparameters) {
  58.              foreach($structure->parts[$i]->parameters as $object) {
  59.                if(strtolower($object->attribute) == 'name') {
  60.                  $attachments[$i]['is_attachment'] = true;
  61.                  $attachments[$i]['name'] = $object->value;
  62.                }
  63.              }
  64.            }
  65.  
  66.            if($attachments[$i]['is_attachment']) {
  67.              $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
  68.              if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
  69.                $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  70.              }
  71.              elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
  72.                $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  73.              }
  74.            }            
  75.          } // for($i = 0; $i < count($structure->parts); $i++)
  76.        } // if(isset($structure->parts) && count($structure->parts))
  77.  
  78.  
  79.  
  80.  
  81.     if(count($atachments)!=0){
  82.  
  83.  
  84.         foreach($attachments as $at){
  85.  
  86.             if($at[is_attachment]==1){
  87.                 #-------------------------------------------------------------------------- guardar adjuntos
  88.                file_put_contents($bandeja_entrada.$at[filename], $at[attachment]);
  89.  
  90.                 }
  91.             }
  92.  
  93.         }
  94.  
  95.   }
  96.  
  97.  // echo $output;
  98. }
  99.  
  100. /* close the connection */
  101. imap_close($inbox);
  102.  
  103. ?>