我正在编写一个下载电子邮件并将它们存储在数据库中的脚本,我通常会在这个帐户上收到数千封电子邮件,一旦下载这些邮件就会被删除。
由于偏执狂,我希望我的电子邮件至少有一个月的备份,但我不能把我的主邮箱地址弄得乱七八糟,把它们留在里面。
所以我需要将邮件(通过 php 代码)从一个邮箱移动到另一个邮箱。我想出了这个使用 imap_append() 的解决方案。但是,此解决方案会重新创建电子邮件,并且不会真正移动它。
你有什么建议或替代方法吗?
请记住:它必须在 php 中完成,因为我需要将它集成到我的 readmail 脚本中。
我已经看过这个线程,其中提出了 fetchmail 解决方案
下面是我为此任务编写的代码
<?php
/**
* Conn params
*/
$fromMboxServerPath = "{imap.from.server/notls/imap:143}";
$fromMboxMailboxPath = "INBOX";
$fromMboxMailAddress = "login";
$fromMboxMailPass = "pass";
$toMboxServerPath = "{imap.to.server/notls/imap:143}";
$toMboxMailboxPath = "INBOX";
$toMboxMailAddress = "login";
$toMboxMailPass = "pass";
$fromMboxConnStr = $fromMboxServerPath.$fromMboxMailboxPath;
$toMboxConnStr = $toMboxServerPath.$toMboxMailboxPath;
$fetchStartSeq = 1;
$fetchEndSeq = 10;
function myLog($str)
{
echo "Log [".date('Y-m-d H:i:s')."]: $str\n";
}
myLog("Connecting to mailbox");
function mboxConn($connstr,$addr,$pass)
{
if(!($mbox = @imap_open($connstr, $addr, $pass)))
{
myLog("Error: ".imap_last_error());
die;
}
else
{
myLog("Connected to: $addr $connstr");
return $mbox;
}
}
function mboxCheck($mbox)
{
if(!($mbox_data = imap_check($mbox)))
{
myLog("Error: ".imap_last_error());
die;
}
else
{
myLog("Mailbox check ".$mbox_data->Mailbox." OK");
myLog($mbox_data->Nmsgs." messages present");
return $mbox_data->Nmsgs;
}
}
$fromMbox = mboxConn($fromMboxConnStr, $fromMboxMailAddress, $fromMboxMailPass);
$toMbox = mboxConn($toMboxConnStr, $toMboxMailAddress, $toMboxMailPass);
$fromMboxCount = mboxCheck($fromMbox);
$toMboxCount = mboxCheck($toMbox);
/**
* Loop on mails
*/
$fetchStartUID = imap_uid($fromMbox,$fetchStartSeq);
if ($fromMboxCount < $fetchEndSeq)
{
$fetchEndSeq = $fromMboxCount;
}
$fetchEndUID = imap_uid($fromMbox,$fetchEndSeq);
/**
* Loop on mails
*/
myLog("Do stuff and backup from UID [$fetchStartUID] to UID [$fetchEndUID]");
for ($i=$fetchStartSeq;$i<=$fetchEndSeq;$i++)
{
$pfx = "Msg #$i : ";
$h = imap_header($fromMbox, $i);
$fh = imap_fetchheader($fromMbox, $i);
$fb = imap_body($fromMbox, $i);
$message = $fh.$fb;
$msgUID = imap_uid($fromMbox,$i);
$struct = imap_fetchstructure ($fromMbox, $i);
/**
* We do some logging
*/
myLog($pfx."UID [".$msgUID."] SEQ [".imap_msgno($fromMbox,$msgUID)."] Flags: [". $h->Unseen . $h->Recent . $h->Deleted . $h->Answered . $h->Draft . $h->Flagged."]");
myLog($pfx."From: [". htmlspecialchars($h->fromaddress) . "] To: [".htmlspecialchars($h->toaddress)."]");
myLog($pfx."Subject: [$h->subject]");
/**
* Here you do whaterver you need with your email
*/
/**
* Backup email
*/
if (!($ret = imap_append($toMbox,$toMboxServerPath.$toMboxMailboxPath,$message)))
{
myLog("Error: ".imap_last_error());
die;
}
else
{
myLog("everything ok, mail [$fetchStartUID:$fetchEndUID] downloaded and moved in $newMailboxNameMOVE");
}
}
/**
* End
*/
imap_close($fromMbox);
imap_close($toMbox);
myLog("Connection closed");
?>