使用 php 从 POP3 服务器获取邮件

2022-08-30 12:28:08

我正在尝试从POP3获取邮件(我正在使用POP3邮件服务器,我正在尝试获取邮件内容并将其存储到我的项目的数据库表中),但我找不到任何PHP脚本,所有这些都仅适用于IMAP。

您知道如何从POP3服务器获取邮件吗?

谢谢。


答案 1

有点令人惊讶的是,PHP的imap库也可用于处理POP3邮箱。当然,大多数高级IMAP功能都不起作用(例如文件夹或获取邮件部分),但基本的POP3功能已经实现。

主要区别在于您要传递给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");

除此之外,这是公平的航行 - 您不需要超过imap_open,imap_num_msg,imap_body,imap_delete和imap_close即可获得基本的POP3访问。


答案 2

PHP的IMAP函数可以同时处理IMAP和POP3框。

这些功能使您能够使用 IMAP 协议以及 NNTP、POP3 和本地邮箱访问方法进行操作。

但是,请注意,某些 IMAP 函数将无法与 POP 协议一起正常工作。

有一个用户贡献的注释,它提供了一个有趣的片段。你可能想看看它。我不能说它的质量,但从表面上看,它看起来还不错。


下面,贡献说明:

感谢所有来到这里为之祷告的人:

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);
}

推荐