php - imap - 移动电子邮件在antoher帐户 [英] php - imap - moving emails on antoher account
问题描述
我正在编写一个下载电子邮件并将其存储在数据库中的脚本,通常会在下载邮件时收到数以千计的电子邮件。
作为paranoic,我想要至少有一个月的电子邮件备份,但我不能混淆我的主邮箱地址,留下他们在那里。
所以我需要移动邮件(通过php代码)从一个邮箱到另一个邮箱。我想出了使用imap_append()的这个解决方案。这个解决方案可以重新创建电子邮件,而不是真的移动它。
你有什么建议还是替代方法?
记住:它必须在php中完成,因为我需要将它整合到我的readmail脚本中。
我已经看到这个线程了href =https://stackoverflow.com/questions/57547/imap-forwarder>提出了fetchmail解决方案
这里是代码I为这个任务写了
<?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;
函数myLog($ str)
{
echoLog [.date('Ymd H:i:s')。]:$ str\\\
;
}
myLog(连接到邮箱);
函数mboxConn($ connstr,$ addr,$ pass)
{
if(!($ mbox = @imap_open($ connstr,$ addr,$ pass)))
{
myLog(Error:.imap_last_error());
死
}
else
{
myLog(Connected to:$ addr $ connstr);
return $ mbox;
}
}
函数mboxCheck($ mbox)
{
if(!($ mbox_data = imap_check($ mbox)))
{
myLog(Error:.imap_last_error());
死
}
else
{
myLog(邮箱检查$ mbox_data->邮箱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);
/ **
*循环邮件
* /
$ fetchStartUID = imap_uid($ fromMbox,$ fetchStartSeq);
if($ fromMboxCount< $ fetchEndSeq)
{
$ fetchEndSeq = $ fromMboxCount;
}
$ fetchEndUID = imap_uid($ fromMbox,$ fetchEndSeq);
/ **
*循环邮件
* /
myLog(从UID [$ fetchStartUID]到UID [$ fetchEndUID]);
($ 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);
/ **
*我们做一些记录
* /
myLog($ pfx。UID [。$ msgUID。] SEQ [.imap_msgno($ fromMbox,$ msgUID)]标志:[。$ h-> Unseen。$ h->最近$ h->已删除$ h->已回答$ h- > Draft。$ h->已举报。
myLog($ pfx。From:[。htmlspecialchars($ h-> fromaddress)。] To:[.htmlspecialchars($ h-> toaddress)。]);
myLog($ pfx。主题:[$ h-> subject]);
/ **
*您可以通过电子邮件发送您需要的邮件
* /
/ **
*备份电子邮件
* /
if(!($ ret = imap_append($ toMbox,$ toMboxServerPath。$ toMboxMailboxPath,$ message)))
{
myLog(Error:.imap_last_error ));
死
}
else
{
myLog(everything ok,mail [$ fetchStartUID:$ fetchEndUID] download and move in $ newMailboxNameMOVE);
}
}
/ **
*结束
* /
imap_close($ fromMbox);
imap_close($ toMbox);
myLog(Connection closed);
?>
我不知道任何其他解决方案,如PHP。
但是,对于您的代码和测试,您应该使用:
$ fromMboxServerPath ={imap.from.server / notls / imap / readonly:143};在imap_append()中,您应该从emailheader中给出日期,// ReadOnly
请参阅PHP手册: http://php.net/manual/en/function.imap -append.php
之后,您将在目标IMAP服务器中拥有1to1的邮件副本。
I am working on a script that downloads emails and stores them in a db, I usually receive thousands of emails on this account, once downloaded the mails are deleted.
Being paranoic, I want to have at least one month backup of my emails, but I cannot clutter my main mailbox address leaving them in there.
So i need to move the mails (via php code) from one mailbox to another. I came up with this solution that uses imap_append(). This solution, however recreates the email, and does not really move it.
Do you have any suggestions or alternative ways of doing this?
Remember: it must be done in php, because I need to integrate it in my readmail script.
I have already seen this thread where a fetchmail solution was proposed
Here follows the code I wrote for this task
<?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");
?>
I don't know any other solution like PHP.
But for your code and testing you should use:
$fromMboxServerPath = "{imap.from.server/notls/imap/readonly:143}"; //ReadOnly
in imap_append() you should give the date from emailheader. see PHP Manual: http://php.net/manual/en/function.imap-append.php
after that you will have a 1to1 copy of your mail in the target IMAP-Server.
这篇关于php - imap - 移动电子邮件在antoher帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!