简体   繁体   English

如何使用imap扩展程序获取原始电子邮件数据?

[英]How to get raw email data with the imap extension?

I'm looking for a way to download the whole raw email data (including attachment), similar to what you get by clicking "Show Original" in Gmail. 我正在寻找一种方法来下载整个原始电子邮件数据(包括附件),类似于您在Gmail中点击“显示原始内容”所获得的内容。

Currently, I can get the raw header and some parts of the mail body by this code: 目前,我可以通过以下代码获取原始标题和邮件正文的某些部分:

$this->MailBox = imap_open($mailServer, $userName, $password, OP_SILENT);
...
$email->RawEmail = imap_fetchbody($this->MailBox, $msgNo, "0");
$email->RawEmail .= "\n".imap_fetchbody($this->MailBox, $msgNo, "1");

Also I know that changing the 3rd parameter of imap_fetchbody might return the encoded attachment. 另外我知道更改imap_fetchbody的第3个参数可能会返回编码的附件。 Guess I need a loop here to get the raw email part by part, but what's the condition to stop the loop? 猜猜我需要一个循环来获取原始电子邮件的一部分,但是什么是停止循环的条件?

Is there an easy way to get the whole email at once? 是否有一种简单的方法可以立即获得整个电子邮件?

Any help would be appreciated. 任何帮助,将不胜感激。

I came across this question when I was searching for the same issue. 当我在寻找同样的问题时,我遇到了这个问题。 I knew it had to be easier than the solution by hakre and I found the right answer on the page for imap_fetchbody on a post from 5 years ago . 我知道它必须比hakre的解决方案更容易,我在5年前帖子上找到了imap_fetchbody页面上的正确答案。

To get the full raw message just use the following: 要获取完整的原始消息,请使用以下命令:

$imap_stream = imap_open($server, $username, $password);
$raw_full_email = imap_fetchbody($imap_stream, $msg_num, "");

Notice the empty string as third parameter of imap_fetchbody this signifies all parts and sub-parts of the email, including headers and all bodies. 请注意空字符串作为imap_fetchbody第三个参数,这表示电子邮件的所有部分和子部分,包括标题和所有正文。

The answer by hakre is overcomplete, if you don't really care about the structure then you won't need imap_fetchstructure . hakre的答案是过于完整的,如果你真的不关心结构,那么你将不需要imap_fetchstructure

For a simple 'Show Source' you should only need imap_fetchheader and imap_body . 对于简单的“显示源”,您应该只需要imap_fetchheaderimap_body

Example: 例:

$conn = imap_open('{'."$mailServer:$port/pop3}INBOX", $userName, $password, OP_SILENT && OP_READONLY);
$msgs = imap_fetch_overview($conn, '1:5'); /** first 5 messages */
foreach ($msgs as $msg) {
    $source = imap_fetchheader($conn, $msg->msgno) . imap_body($conn, $msg->msgno);
    print $source;
}

My answer is "overcomplete" , see Boy Baukema's answer for a more straight forward "view source" way of doing if you don't need the whole structure of the email. 我的答案是“过度完整” ,如果您不需要电子邮件的整个结构,请参阅Boy Baukema对更直接的“查看源”方式的回答

If you want to fetch all, you need to fetch 如果你想获取所有,你需要获取

As mime-message can have multiple bodies, you need to process each part after the other. 由于mime-message可以有多个主体,因此您需要先处理每个部分。 To reduce load to the server use the FT_PREFETCHTEXT option for imap_fetchheader . 为了降低负载对服务器使用FT_PREFETCHTEXT的选项imap_fetchheader Some example code about imap_fetchstructure in another answer that shows handling the imap connection and iterating over parts of a message already through encapsulation. 关于imap_fetchstructure一些示例代码在另一个答案中显示了处理imap连接并通过封装迭代部分消息。

A common mistake is to use "\\n" or PHP_EOL . 常见的错误是使用"\\n"PHP_EOL

According to RFC RFC821, RFC2060, RFC1939 "\\r\\n" should be used. 根据RFC RFC821,RFC2060,应使用RFC1939 "\\r\\n" While some mailserver auto convert that mistakes, Cyrus does not and throw a nice error. 虽然一些mailserver自动转换出错,但Cyrus并没有抛出错误。

PHP_EOL is a system depending constant, it's "\\n" on Linux, "\\r" on Mac and "\\r\\n" on Windows, for example. PHP_EOL是根据恒定的,它是一个系统的"\\n"在Linux上, "\\r"在Mac和"\\r\\n"在Windows上,例如。

Furthermore imap_fetchheader() contains the trailing "\\r\\n" as expected. 此外, imap_fetchheader()包含预期的尾部"\\r\\n" So the correct example would be: 所以正确的例子是:

 $source = imap_fetchheader($conn, $msg->msgno) . imap_body($conn, $msg->msgno);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM