我正在尝试创建一个登录我的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问题。有什么帮助吗?
这是我得到的输出
答案 0 :(得分: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' => 'myemail@gmail.com',
'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
}