2012-10-26 45 views
0

我在嘗試製作一個腳本,該腳本登錄到我的Gmail帳戶並從SPAM文件夾中獲取所有電子郵件。這是我到目前爲止有:使用Zend Framework獲取垃圾郵件文件夾

<?php 
// Ensure Zend folder is on include_path 
set_include_path(implode(PATH_SEPARATOR, array(
    'C:\xampp\htdocs\zend\library', 
    get_include_path(), 
))); 

// require the ZF autoloader file if you have it in your include path 
require_once 'Zend/Loader/Autoloader.php'; 
// if ZF is not in your path you can specify the full path 
// otherwise if it's in a subdir (most likely if you're on a web hosting) 
// you can do something like this 
//require_once dirname(__FILE__) . '/Zend/Loader/AutoLoader.php'; 

// laod the autoloader so you don't need to require any ZF file 
Zend_Loader_Autoloader::getInstance(); 

// connecting with Imap to gmail 
$mail = new Zend_Mail_Storage_Imap(
    array(
     'host'  => 'imap.gmail.com', 
     'port'  => '993', 
     'ssl'  => true, 
     'user'  => '[email protected]', 
     'password' => 'password', 
    ) 
); 

/* 
// get the message object 
$message = $mail->getMessage(1); 
// output subject of message 
echo $message->subject . "\n"; 
// dump message headers 
Zend_Debug::dump($message->getHeaders()); 

echo $mail->countMessages() . " messages found<br/>"; 
foreach ($mail as $message) 
{ 
    echo "Mail from '{$message->from}': {$message->subject}<br/>"; 
} 
*/ 

/* 
var_dump($mail->getCurrentFolder()); 
echo "<hr>"; 
*/ 

echo "<pre>"; 

foreach($mail->getFolders() as $mailfolder) 
{ 
    foreach($mailfolder as $mailfolder2) 
    { 
     echo "<b>Folder ".$mailfolder2." ... fetching ... </b><br>"; 

      var_dump($mailfolder2); 



    } 

} 

die; 
var_dump($mail->getFolders()); 
var_dump($mail->selectFolder("Gmail/Drafts")); 

/* 
// mbox with folders 
$mail = new Zend_Mail_Storage_Folder_Mbox(array('dirname' => 
                '/')); 

$folders = new RecursiveIteratorIterator($this->mail->getFolders(), 
             RecursiveIteratorIterator::SELF_FIRST); 
echo '<select name="folder">'; 
foreach ($folders as $localName => $folder) { 
    $localName = str_pad('', $folders->getDepth(), '-', STR_PAD_LEFT) . 
       $localName; 
    echo '<option'; 
    if (!$folder->isSelectable()) { 
     echo ' disabled="disabled"'; 
    } 
    echo ' value="' . htmlspecialchars($folder) . '">' 
     . htmlspecialchars($localName) . '</option>'; 
} 
echo '</select>'; 
*/ 

?> 

,我已經成功地每個文件夾(作爲Zend_Mail_Storage_Folder對象)取,我無法弄清楚如何從它的主題/消息...

是獲取所有郵件從收件一個基本的腳本應該是這樣的:

// get the message object 
$message = $mail->getMessage(1); 
// output subject of message 
echo $message->subject . "\n"; 
// dump message headers 
Zend_Debug::dump($message->getHeaders()); 

echo $mail->countMessages() . " messages found<br/>"; 
foreach ($mail as $message) 
{ 
    echo "Mail from '{$message->from}': {$message->subject}<br/>"; 
} 

這基本上是一個面向對象的問題。任何幫助?

這是我得到

http://s17.postimage.org/er2v6yjcf/ceva.jpg

回答

0

試試這個輸出:

<?php 
header("Content-Type: text/plain; charset=UTF-8"); 
mb_internal_encoding("UTF-8"); 
function __autoload($sClassName) { 
    $sLibFilePath = __DIR__.'/'.str_replace('_', '/', $sClassName) . '.php'; 
    if (is_file($sLibFilePath)) { 
     include_once($sLibFilePath); 
    } else { 
     return; 
    } 
} 
$oImap = new Zend_Mail_Storage_Imap(
    array(
     'host'  => 'imap.gmail.com', 
     'port'  => '993', 
     'ssl'  => true, 
     'user'  => '[email protected]', 
     'password' => '***', 
    ) 
); 
$oImap->selectFolder('[Gmail]/Spam'); 
echo $oImap->countMessages() . " messages found\n"; 
foreach ($oImap as $iTempId => $oMessage) { 
    $iUniqueId = $oImap->getUniqueId($iTempId); 
    echo "[{$iTempId}/{$iUniqueId}] Mail from ".mb_decode_mimeheader($oMessage->from).": ".mb_decode_mimeheader($oMessage->subject)."\n"; 
    if (rand(0,5) == 0) break; //for testing 
} 
+0

非常感謝您的回覆,但我3個月前解決了這個,用不同類型的方法,「FOREACH in FOREACH」,如果你知道我在說什麼,直到我到達[GMAIL] /垃圾郵件,因爲Gmail有3個級別的文件夾... btw,我已經發布了別的東西,也是一封電子郵件相關的問題,檢查它:D – HashGuy

相關問題