0
我正在使用IMAP/PHP編寫郵件客戶端。但是,我怎樣才能搜索與附件相關的郵件。 imap_search()沒有選項使用php的Imap郵件
我正在使用IMAP/PHP編寫郵件客戶端。但是,我怎樣才能搜索與附件相關的郵件。 imap_search()沒有選項使用php的Imap郵件
如您所見,IMAP SEARCH command specification沒有搜索包含附件的消息的選項,它不是PHP怪癖。因此,您必須獲取特定郵箱中的可用郵件列表,遍歷它們並檢查單個郵件是否具有附件。
在IMAP/PHP有一個選項imap_fetchstructure它給郵件的一部分類型
$structure = imap_fetchstructure($inbox, $messageNumber);
$flattenedParts = $this->flattenParts($structure->parts);
foreach($flattenedParts as $partNumber => $part)
{
switch($part->type)
{
case 0:
// the HTML or plain text part of the email
// now do something with the message, e.g. render it
break;
case 1:
// multi-part headers, can ignore
break;
case 2:
// attached message headers, can ignore
break;
case 3: // application
case 4: // audio
break;
case 5: // image
//break;
case 6: // video
case 7: // other
echo "Mail contain attachment ";
// don't know what it is
}
break;
}
}
function flattenParts ($messageParts,$flattenedParts=array(),$prefix='', $index = 1, $fullPrefix=true)
{
foreach($messageParts as $part) {
$flattenedParts[$prefix.$index] = $part;
if(isset($part->parts)) {
if($part->type == 2) {
$flattenedParts = $this->flattenParts($part->parts, $flattenedParts, $prefix.$index.'.', 0, false);
}
elseif($fullPrefix) {
$flattenedParts = $this->flattenParts($part->parts, $flattenedParts, $prefix.$index.'.');
}
else {
$flattenedParts = $this->flattenParts($part->parts, $flattenedParts, $prefix);
}
unset($flattenedParts[$prefix.$index]->parts);
}
$index++;
}
return $flattenedParts;
}