barbushin imap-php 获取附件如何在服务器上存储附件

barbushin imap-php get attachments how to store attachments on server

我正在使用 class 来解析和查看电子邮件,效果很好: https://github.com/barbushin/php-imap

现在我正在尝试将附件保存到服务器,但我以前从未使用过电子邮件附件,所以不确定它是如何工作的。

这是我回显电子邮件的代码:

header('Content-Type: text/html; charset=utf-8');
require_once('php_imap/IncomingMail.php');
require_once('php_imap/Mailbox.php');
//require_once('PHPMailer/class.phpmailer.php');

use PhpImap\Mailbox as ImapMailbox;
use PhpImap\IncomingMail;
use PhpImap\IncomingMailAttachment;

$mailbox_name = "x@gmail.com";
$mailboxPassword = "xxxxxxxxxxxx";

// Accessing the mailbox
$mailbox = new PhpImap\Mailbox('{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', $mailbox_name, $mailboxPassword);
$mails = array();

$mailsIds = $mailbox->searchMailBox('UNSEEN');
if(!$mailsIds) {
    die('Mailbox is empty');
}

foreach ($mailsIds as $mailId){
    $mail = $mailbox->getMail($mailId);
    echo "---------------------------Start of Email ---------------------------" . "<br />";
    echo $mail->date . "<br />";
    echo "From: " . $mail->fromName .  " - " . $mail->fromAddress . "<br />";
    if($mail->cc){
       echo "CC: " . $mail->cc . "<br />";
    }
    echo "Subject: " . $mail->subject . "<br /><br />";
    if($mail->textHtml == NULL){
        echo $mail->textPlain;
    }else{
        echo $mail->textHtml;
    }
    echo "<br /><br />";
    echo "---------------------------End of Email ---------------------------";
    echo "<br /><br /><br /><br /><br /><br />";

    echo "attachments: ";
    echo "<PRE>";
    var_dump($mail->getAttachments());
    echo "</PRE>";
}

现在邮件内容正常,附件回显如下:

attachments:

array(3) {
  ["image002.jpg@01D0838E.40768C10"]=>
  object(PhpImap\IncomingMailAttachment)#21 (3) {
    ["id"]=>
    string(30) "image002.jpg@01D0838E.40768C10"
    ["name"]=>
    string(12) "image002.jpg"
    ["filePath"]=>
    NULL
  }
  ["image004.png@01D0838E.40768C10"]=>
  object(PhpImap\IncomingMailAttachment)#12 (3) {
    ["id"]=>
    string(30) "image004.png@01D0838E.40768C10"
    ["name"]=>
    string(12) "image004.png"
    ["filePath"]=>
    NULL
  }
  ["image005.jpg@01D0838E.40768C10"]=>
  object(PhpImap\IncomingMailAttachment)#32 (3) {
    ["id"]=>
    string(30) "image005.jpg@01D0838E.40768C10"
    ["name"]=>
    string(12) "image005.jpg"
    ["filePath"]=>
    NULL
  }
}

由于路径为空,我不确定我的脚本是否存在问题,或者这是否正常但没有补丁我如何将图像保存到我的服务器以便我可以在电子邮件中显示它们。我做了很多谷歌搜索,但我认为它可能是一个不同的应用程序 class 并且我真的找不到任何文档。我苦苦挣扎的是我不明白电子邮件附件的概念。

如果附件以 $_POST 形式发送,我可以将它们从临时文件夹移动到永久文件夹,因为我在 $_files 信息中有路径值,但是这里我只有图像 ID 和图像名称,所以我不确定图像实际所在的位置。关于如何获取附件信息并将文件保存到服务器以便我可以将它们显示给用户的任何想法?

阅读源码:需要指定存放附件的目录。

class Mailbox {
# ....
public function __construct($imapPath, $login, $password, 
    $attachmentsDir = null, $serverEncoding = 'UTF-8')

如果是空附件will not be saved locally,则没有filePath

这对我有用:

private $mailbox; //ImapStream

public function __construct()
{
    $this->mailbox = new ImapMailbox('{host}INBOX', 'user', 'pass', __DIR__);
    $this->mailbox->setAttachmentsDir('tmp/');
}

public function getListOfMails()
{
    $mailsIds = $this->mailbox->searchMailbox('ALL');
    if(!$mailsIds) {
        die('Mailbox is empty');
    }

    $mails = $this->mailbox->getMailsInfo($mailsIds);
    $mails = array_reverse($mails);

    return $mails;
}

public function getFullMail($mailId)
{
    $mail = $this->mailbox->getMail($mailId);
    echo $mail->replaceInternalLinks('tmp/);
}