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

发布于 2024-08-22 11:23:40 字数 127 浏览 6 评论 0原文

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

如何使用 PHP 和 PHP 来解决这个问题? IMAP?

提前致谢

I've successfully written script that connects to my mail server and retrieves the headers and bodies of all new messages. I want to take it one step further to detect if attachment exists (images only), if so, download to server.

How to go about this using PHP & IMAP?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2024-08-29 11:23:40

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 ;
}

KimNyholm published an imap client set of methods that enclose the goals you ask:
https://github.com/KimNyholm/ubuntu-web-development/blob/master/php/imapClient.php

He wrote this code because of the lack of full tutorials and code examples to process php imap messages, as he explains here: http://kimnyholm.com/a-simple-imap-mail-reader-client/ and based some of his code in drupal libraries.

I enclose an extract of the methods that perform the steps you menction, I hope it solves the question even though I see it's not exactly recent:

Check for attachments and images amongst them:

// 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']);
  }

Here he saves the data to a tempdir and download it:

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 ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文