barbushin imap php获取附件如何在服务器上存储附件
我正在使用一个用于解析和查看电子邮件的类,它非常有用: 现在我正试图将附件保存到服务器上,但我以前从未处理过电子邮件附件,所以不确定它是如何工作的 这是我回发电子邮件的代码:barbushin imap php获取附件如何在服务器上存储附件,php,imap,email-attachments,gmail-imap,Php,Imap,Email Attachments,Gmail Imap,我正在使用一个用于解析和查看电子邮件的类,它非常有用: 现在我正试图将附件保存到服务器上,但我以前从未处理过电子邮件附件,所以不确定它是如何工作的 这是我回发电子邮件的代码: header('Content-Type: text/html; charset=utf-8'); require_once('php_imap/IncomingMail.php'); require_once('php_imap/Mailbox.php'); //require_once('PHPMailer/clas
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>";
}
由于路径为空,我不确定我的脚本是否存在问题,或者这是否正常,但如果没有补丁,如何将图像保存到服务器,以便在电子邮件中显示它们。我在谷歌上搜索了很多,但我认为这个类可能是一个不同的应用程序,而且我找不到任何文档。我正在挣扎的是我不理解电子邮件附件的概念
对于以$\u POST形式发送的附件,我可以将它们从临时文件夹移动到永久文件夹,因为我在$\u文件信息中有路径值,但是在这里我只有一个图像id和图像名称,所以我不确定图像的实际位置。关于如何获取附件信息并将文件保存到服务器以便向用户显示它们,您有什么想法吗?阅读源代码:您需要指定一个目录来存储附件
class Mailbox {
# ....
public function __construct($imapPath, $login, $password,
$attachmentsDir = null, $serverEncoding = 'UTF-8')
如果附件为空,则无文件路径
这对我有效:
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/);
}
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/);
}