我正在尝试制作一个脚本来登录我的 Gmail 帐户并从垃圾邮件文件夹中获取所有电子邮件。这是我到目前为止所拥有的:
<?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' => 'myemail@gmail.com',
'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/>";
}
这基本上是一个 OOP 问题。有什么帮助吗?
这是我得到的输出