PHP获取超过20000个imap电子邮件 [英] PHP fetch over 20000 imap emails

查看:159
本文介绍了PHP获取超过20000个imap电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将多个邮箱导出到数据库。我当前的脚本将连接IMAP并且只是循环所有消息。虽然较大的邮箱不会工作,它会减慢甚至停止。

I'm trying to export several mailboxes to an database. My current script will connect IMAP and just loop all messages. Though with larger mailboxes this won't work and it will slow down or even stop.

这个想法是每天运行脚本复制所有不是在数据库中还没有到数据库。获取大量电子邮件的最佳方式(20k个邮件分布在大约40 - 50个文件夹中)。

The idea is to run the script daily to "copy" all messages who are not in the database yet to the database. Whats the best way to fetch big amounts of e-mails (20k mails spread over about 40 - 50 folders).

最终,这将需要从单个服务器每天扫描数百甚至数千个帐户(因此想象数据量)。它会将邮件(uid和主题)存储到数据库中,并创建一个将存储在dataserver上的包(因此也需要提取附件)。

Eventually this will need to work from a single server to scan hundreds or even thousands accounts daily (so imagine the amounts of data). It will store the mail (uid and subject) into the database and create a package which will be stored on the dataserver (so it also needs to fetch the attachments).

推荐答案

因此,您要通过IMAP执行电子邮件备份。有专业的软件工具可以做到这一点。

So you want to perform email backup via IMAP. There are professional software tools that do this.

让我们从简单的开始:从收件箱文件夹下载一个特定用户的电子邮件。这要求您(a)使用用户凭据登录,(b)选择INBOX文件夹,并且(c)下载消息(假设您已经知道其UID,即55)。您在IMAP中执行以下操作(仅请求 - 未显示响应):

Let's start from something simple: downloading an email for one specific user from the inbox folder. This requires you to (a) login with the user's credentials, (b) select the INBOX folder, and (c) download the message (let's assume that you already know its UID, which is 55). You do this in IMAP as follows (requests only - responses not shown):

01 LOGIN username password
02 SELECT INBOX
03 UID FETCH 55 BODY[]

特定文件夹中的每封邮件 UID 。这是从不更改的邮件的唯一标识符,不能由该文件夹中的任何其他邮件使用 。新邮件的UID必须高于先前的邮件。

Each message in a particular folder is given a UID. This is a unique identifier for the message that never changes - it cannot be used by any other message in that folder. New messages must have a higher UID than previous ones. This makes it a useful tool to determine whether you already downloaded the message previously.

下一步:让我们看一下在INBOX文件夹中下载所有新邮件。让我们假设你是第一次下载消息,并且INBOX当前具有UID为54,55和57的消息。您可以使用以下命令立即下载这些消息:

Next step: let us now look at downloading all new messages in the INBOX folder. Let's assume that you're downloading messages for the first time, and the INBOX currently has messages with UIDs 54, 55 and 57. You can download these messages all at once using a command such as:

03 UID FETCH 54,55,57 BODY[]

(如果有很多要下载的文件,你可能需要批量打断它(例如每次30个)。然后,你存储到目前为止你下载的最大的UID。下一次,您可以检查UID高于如下:

(You might want to break this up in batches (e.g. 30 at a time) if there are a lot to download.) After doing that, you store the highest UID you downloaded so far. Next time, you can check for UIDs higher than that as follows:

04 UID FETCH 58:* UID

这将从58开始检索UID的邮件的UID。如果你得到结果,那么你下载那些,并再次存储UID。等等。

That will retrieve the UID (only) for messages with a UID from 58 onwards. If you get results, then you download those, and again store the UID. And so on.

有一个catch。只要文件夹的UIDVALIDITY属性(包含在对SELECT命令的响应中)不更改,消息的UID就是有效的。如果此更改因任何原因,该文件夹失效,您需要再次下载该文件夹中的所有邮件。

There is one catch. The UIDs of a message are valid so long as the folder's UIDVALIDITY attribute (included in the response to the SELECT command) does not change. If this changes for whatever reason, the folder is invalidated, and you need to download all messages in that folder all over again.

最后,您想将其扩展到工作为所有用户的所有文件夹。要获取特定用户的所有文件夹,请使用IMAP LIST命令:

Finally, you want to extend this to work for all folders for all users. In order to get all folders for a particular user, you use the IMAP LIST command:

05 LIST "" "*"

您需要事先知道用户的凭据并循环。

You will need to know the credentials for the users beforehand and loop over them.

这是IMAP背后你需要做的理论。在PHP中实现它是一个练习。

This is the IMAP theory behind what you need to do. Implementing it in PHP is left as an exercise.

这篇关于PHP获取超过20000个imap电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆