PHP Conference Japan 2024

IMAP、POP3和NNTP

添加笔记

用户贡献笔记 1条笔记

51
Wil Barath
14年前
对于所有来这里祈祷的人

1) 一种极其简单的读取 MIME 附件的方法,或者
2) 一种极其简单的访问 POP3 文件夹的方法

不用再找了。

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

[由 php DOT net 的 danbrown 编辑:包含 2010 年 6 月 9 日 "mn26826" 修复的错误修正,该修正修复了用户函数 pop3_stat() 中对 imap_mailboxmsginfo() 传递的参数 $imap 的错误引用。本意应该是 $connection。]

[由 php DOT net 的 visualmind 编辑:包含 2010 年 8 月 17 日 "elias-jobview" 修复的错误修正,该修正修复了 pop3_list 函数中缺少 "return $result" 的错误。]

[由 php DOT net 的 danbrown 编辑:包含 2010 年 9 月 9 日 "chrismeistre" 修复的错误修正,该修正修复了 pop3_list() 函数中错误引用 $mbox(应为 $connection)的错误。]
To Top