如何使用IMAP& amp;保存电子邮件中的图像附件PHP?

时间:2010-02-18 21:08:52

标签: php email imap email-client

我已成功编写连接到我的邮件服务器的脚本,并检索所有新邮件的标题和正文。我想进一步检测是否存在附件(仅限图像),如果是,请下载到服务器。

如何使用PHP& amp; IMAP?

提前致谢

3 个答案:

答案 0 :(得分:1)

它有点旧,this article解释了如何做到这一点。

答案 1 :(得分:1)

KimNyholm发布了一组imap客户端方法,这些方法包含了您要求的目标: https://github.com/KimNyholm/ubuntu-web-development/blob/master/php/imapClient.php

他之所以编写此代码,是因为缺乏处理php imap消息的完整教程和代码示例,正如他在此处解释的http://kimnyholm.com/a-simple-imap-mail-reader-client/并将其一些代码基于drupal库。

我附上了执行您提到的步骤的方法的摘录,我希望它能解决问题,即使我发现它不是最近的也是如此:

检查附件和其中的图像:

// ATTACHMENT
  // Any part with a filename is an attachment,
  // so an attached text file (type 0) is not mistaken as the message.
  if(isset($parameter['filename']) || isset($parameter['name'])) {
    $filename = ($parameter['filename'])? $parameter['filename'] : $parameter['name'];
    $filename=iconv_mime_decode($filename, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
    $id = isset($part->id) ? $part->id : '' ;
    $attachments[] = array('inline' => false, 'filename' => $filename, 'part' => $partNo, 'data' => $data, 'id' => $id);
  }
  if ($type==TYPEIMAGE){
    $info=fetchImageInfo($mailbox, $emailNumber, $partNo);
    $attachments[] = array('inline' => true, 'filename' => $info['filename'], 'part' => $partNo, 'data' => $data, 'id' => $info['id']);
  }

他在这里将数据保存到临时目录并下载:

function EmailAttachmentsSave(&$mail){
  $html = '';
  $attachments=$mail->attachments;
  $msgNo=trim($mail->headerInfo->Msgno);
  foreach ($attachments as $attachment) {
    $partNo=$attachment['part'];
    $tmpDir= "imapClient/$msgNo/$partNo";
    $dirExists= is_dir($tmpDir);
    if (!$dirExists){
      $dirExists= mkdir($tmpDir, 0777, true) ;
    }
    $fileName=$attachment['filename'];
    $tmpName = "$tmpDir/$fileName";
    $saved = $dirExists && file_put_contents($tmpName, $attachment['data']);
    $tmpName=htmlentities($tmpName);
    $fileName=htmlentities($fileName);
    if (!$attachment['inline']){
      $html .= '<span><a href="' . $tmpName . '">' . $fileName . '</a> </span>';
    }
    $cid =$attachment['id'];
    if (isset($cid)){
      $mail->htmlText=EmailEmbeddedLinkReplace($mail->htmlText,$cid,$tmpName);
    }
  }
  return $html ;
}

function EmailPrint($mail){
  $headerInfo=$mail->headerInfo;
  $html = '<h4>' . htmlentities($headerInfo->subject) . '</h4>';
  $html .= '<p>From: ' . htmlentities($headerInfo->fromaddress) . '</p>';
  $html .= '<p>To: ' . htmlentities($headerInfo->toaddress) . '</p>';
  $html .= '<div style="background: lightgrey">' . (empty($mail->htmlText) ? ('<p>' . $mail->plainText . '</p>') : $mail->htmlText) . '</div>';
  return $html ;
}

function EmailDownload($host, $user, $password){
  $html = '<head> <meta charset="UTF-8"> </head>';
  $html .= '<h3>Simple imap client</h3>';
  $mails=EmailGetMany($host, $user, $password);
  $count=count($mails);
  $html .= "<p>$user has $count mails at $host.</p>";
  foreach ($mails as $mail){
      $html .= '<hr>';
      $html .= EmailAttachmentsSave($mail);
      $html .= EmailPrint($mail);
  }
  return $html ;
}

答案 2 :(得分:0)

附件将是MIME encoded - 解析和检查内容相当简单。

图片将具有图片MIME内容类型。