使用 php 从 POP3 服务器获取邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3165014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fetching mail from a POP3 server using php
提问by VinothPHP
I am trying to fetch a mail from POP3 (I am using POP3 mail server and I am trying to fetch the mail content and store into a database table for my project.), but I can't find any PHP script for that, all are only for IMAP.
我正在尝试从 POP3 获取邮件(我正在使用 POP3 邮件服务器,我正在尝试获取邮件内容并将其存储到我的项目的数据库表中。),但我找不到任何 PHP 脚本,所有仅用于 IMAP。
Do you know how to fetch mail from a POP3 server?
您知道如何从 POP3 服务器获取邮件吗?
Thanks.
谢谢。
采纳答案by Piskvor left the building
Somewhat surprisingly, PHP's imap librarycan be also used for working with POP3 mailboxes. Most of the advanced IMAP features won't work, of course (e.g. folders or fetching message parts), but the basic POP3 functionality is implemented.
有点令人惊讶的是,PHP 的 imap 库也可以用于处理 POP3 邮箱。当然,大多数高级 IMAP 功能将无法使用(例如文件夹或获取消息部分),但实现了基本的 POP3 功能。
The main difference is the option string that you're passing to imap_open- to quote that page:
主要区别在于您传递给imap_open的选项字符串- 引用该页面:
// To connect to a POP3 server on port 110 on the local server, use:
$mbox = imap_open ("{localhost:110/pop3}INBOX", "user_id", "password");
Other than that, it's fair sailing - you won't need more than imap_open, imap_num_msg, imap_body, imap_delete and imap_close for basic POP3 access.
除此之外,它是公平的航行 - 对于基本的 POP3 访问,您只需要 imap_open、imap_num_msg、imap_body、imap_delete 和 imap_close。
回答by Pekka
PHP's IMAP functionscan deal with both IMAP and POP3 boxes.
PHP 的 IMAP 函数可以处理 IMAP 和 POP3 框。
These functions enable you to operate with the IMAP protocol, as well as the NNTP, POP3 and local mailbox access methods.
Be warned, however, that some IMAP functions will not work correctly with the POP protocol.
这些功能使您能够使用 IMAP 协议以及 NNTP、POP3 和本地邮箱访问方法进行操作。
但是请注意,某些 IMAP 功能将无法与 POP 协议一起正常工作。
there is a User Contributed Notethat provides an interesting snippet. You may want to take a look at it. I can't say anything about its quality but from the surface, it looks okay.
有一个User Contributed Note提供了一个有趣的片段。你可能想看看它。我不能说它的质量,但从表面上看,它看起来不错。
Below, the Contributed Note:
下面是投稿说明:
For all the people coming here praying for:
1) a dead-easy way to read MIME attachments, or
2) a dead-easy way to access POP3 folders
Look no further.
为所有来到这里祈祷的人:
1) 一种非常简单的方式来读取 MIME 附件,或者
2) 一种非常简单的访问 POP3 文件夹的方法
别再看了。
function pop3_login($host,$port,$user,$pass,$folder="INBOX",$ssl=false)
{
$ssl=($ssl==false)?"/novalidate-cert":"";
return (imap_open("{"."$host:$port/pop3$ssl"."}$folder",$user,$pass));
}
function pop3_stat($connection)
{
$check = imap_mailboxmsginfo($connection);
return ((array)$check);
}
function pop3_list($connection,$message="")
{
if ($message)
{
$range=$message;
} else {
$MC = imap_check($connection);
$range = "1:".$MC->Nmsgs;
}
$response = imap_fetch_overview($connection,$range);
foreach ($response as $msg) $result[$msg->msgno]=(array)$msg;
return $result;
}
function pop3_retr($connection,$message)
{
return(imap_fetchheader($connection,$message,FT_PREFETCHTEXT));
}
function pop3_dele($connection,$message)
{
return(imap_delete($connection,$message));
}
function mail_parse_headers($headers)
{
$headers=preg_replace('/\r\n\s+/m', '',$headers);
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)?\r\n/m', $headers, $matches);
foreach ($matches[1] as $key =>$value) $result[$value]=$matches[2][$key];
return($result);
}
function mail_mime_to_array($imap,$mid,$parse_headers=false)
{
$mail = imap_fetchstructure($imap,$mid);
$mail = mail_get_parts($imap,$mid,$mail,0);
if ($parse_headers) $mail[0]["parsed"]=mail_parse_headers($mail[0]["data"]);
return($mail);
}
function mail_get_parts($imap,$mid,$part,$prefix)
{
$attachments=array();
$attachments[$prefix]=mail_decode_part($imap,$mid,$part,$prefix);
if (isset($part->parts)) // multipart
{
$prefix = ($prefix == "0")?"":"$prefix.";
foreach ($part->parts as $number=>$subpart)
$attachments=array_merge($attachments, mail_get_parts($imap,$mid,$subpart,$prefix.($number+1)));
}
return $attachments;
}
function mail_decode_part($connection,$message_number,$part,$prefix)
{
$attachment = array();
if($part->ifdparameters) {
foreach($part->dparameters as $object) {
$attachment[strtolower($object->attribute)]=$object->value;
if(strtolower($object->attribute) == 'filename') {
$attachment['is_attachment'] = true;
$attachment['filename'] = $object->value;
}
}
}
if($part->ifparameters) {
foreach($part->parameters as $object) {
$attachment[strtolower($object->attribute)]=$object->value;
if(strtolower($object->attribute) == 'name') {
$attachment['is_attachment'] = true;
$attachment['name'] = $object->value;
}
}
}
$attachment['data'] = imap_fetchbody($connection, $message_number, $prefix);
if($part->encoding == 3) { // 3 = BASE64
$attachment['data'] = base64_decode($attachment['data']);
}
elseif($part->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachment['data'] = quoted_printable_decode($attachment['data']);
}
return($attachment);
}
回答by Shrishail
you can use pop3 e-mail client class which can Access to e-mail mailboxes using the POP3 protocol. You will get each e-mail body part and can store it in database, even you can retrieve attached files without deleting the original mail in the inbox. For more go to http://www.phpclasses.org/package/2-PHP-Access-to-e-mail-mailboxes-using-the-POP3-protocol.html
您可以使用 pop3 电子邮件客户端类,它可以使用 POP3 协议访问电子邮件邮箱。您将获得每个电子邮件正文部分并将其存储在数据库中,甚至您可以在不删除收件箱中的原始邮件的情况下检索附件。更多信息请访问http://www.phpclasses.org/package/2-PHP-Access-to-e-mail-mailboxes-using-the-POP3-protocol.html
回答by Tomasz Struczyński
IF you have PHP build with IMAP support, it would be easy, see IMAP documentation (especially comments at this page) at http://php.net/manual/en/book.imap.php
如果您有支持 IMAP 的 PHP 构建,那就很容易了,请参阅http://php.net/manual/en/book.imap.php 上的IMAP 文档(尤其是本页的评论)
UPDATE: to clarify my answer - as you see in the comments and function reference, PHP imap_* functions can be used also for pop3.
更新:澄清我的答案 - 正如您在评论和函数参考中看到的,PHP imap_* 函数也可用于 pop3。

