Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save attach from imap server

Status
Not open for further replies.

andregv

Programmer
Nov 26, 2010
2
BR
thread434-1574115

Could someone post the final version of this function?
 
it's all there.

just substitute the imap_savebody line for the file_put_contents line.

implement checking for duplicate file names if you want/need
 
I did, but I always find a error when opennig the saved file.

my code:

Code:
<?php
  header('Content-type: text/html; charset=utf-8');
  header('Cache-Control: no-store, no-cache, must-revalidate');
  header('Cache-Control: post-check=0, pre-check=0', false);
  header('Expires: Sat, 01 Jan 2000 00:00:00 GMT');
  header('Pragma: no-cache');

  function extract_attachments($connection, $message_number) {
    $attachments = array();
    $structure = imap_fetchstructure($connection, $message_number);
    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']) {
          $file = getcwd() .DIRECTORY_SEPARATOR. $attachments[$i]['filename'];
          $attachments[$i]['attachment'] = imap_fetchbody($connection, $message_number, $i + 1);

          file_put_contents($file, $attachments[$i]['attachment']);
        }
      }
    }
    return $attachments;
  }
  $servidor = 'mbox.domain.com';
  $usuario = 'user=soulcomunicacao.com.br';
  $senha = "pass1234";

  $mbox = imap_open('{' . $servidor . '/pop3}INBOX', $usuario, $senha);
  $erro[] = imap_last_error();
  if (!empty($erro[0])) {
    echo $erro[0];
    exit;
  } else {
    $n_messages = imap_num_msg($mbox);
    echo "<p>You got $n_messages message(s)</p>";
    echo "<br /><br />";
    echo '<table border="1" width="100%">';
    echo '<tr>';
    echo '<td align="center" width="150"><strong>Date</strong></td>';
    echo '<td><strong>Sender</strong></td>';
    echo '<td><strong>Subject</strong></td>';
    echo '</tr>';
    for ($i = 1; $i <= imap_num_msg($mbox); $i++) {
      $headers = imap_header($mbox, $i);
      $subject = imap_utf8($headers->subject);
      $message_id = $headers->message_id;
      $toaddress = $headers->toaddress;
      $from = $headers->from;
      $sender = $from[0]->personal;
      $email_sender = $from[0]->mailbox;
      $server_sender = $from[0]->host;
      $date = $headers->date;
      $date = strtotime($date);
      $date = date("d/m/Y H:i:s", $date);
      echo '<tr>';
      echo '<td align="center">' . $date . '</td>';
      echo '<td>' . $email_sender . '@' . $server_sender . '</td>';
      echo '<td><a href="index.php?id=' . $i . '">' . $subject . '</a></td><td>';
      echo '</tr>';
    }
    echo '</table>';
    echo '<br />';
    if (isset($_GET['id'])) {
      $id = $_GET['id'];
      $message = imap_fetchbody($mbox, $id, 1);
      echo nl2br(imap_utf8($message));
      $struct = imap_fetchstructure($mbox, $id, FT_UID);
      extract_attachments($mbox, $id);
    }
    imap_close($mbox);
  }
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top