php邮箱服务器源码,使用php从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('/rns+/m', '',$headers);

preg_match_all('/([^: ]+): (.+?(?:rns(?:.+?))*)?rn/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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 PHP使用 POP3 模块收取邮件的示例代码: ```php <?php /* POP3服务器信息 */ $pop3_server = "pop3.example.com"; $username = "your-email@example.com"; $password = "your-password"; /* 连接到 POP3 服务器 */ $pop3 = fsockopen("ssl://".$pop3_server, 995, $errno, $errstr, 30); /* 检查连接是否成功 */ if (!$pop3) { die("POP3 连接失败!"); } /* 接收 POP3 服务器返回的欢迎信息 */ $response = fgets($pop3, 1024); /* 发送登录命令 */ fputs($pop3, "USER $username\r\n"); $response = fgets($pop3, 1024); fputs($pop3, "PASS $password\r\n"); $response = fgets($pop3, 1024); /* 发送获取邮件列表命令 */ fputs($pop3, "LIST\r\n"); $response = fgets($pop3, 1024); /* 解析邮件列表 */ $emails = explode("\r\n", $response); array_shift($emails); array_pop($emails); /* 循环处理每一封邮件 */ foreach ($emails as $email) { /* 解析邮件大小和编号 */ list($number, $size) = explode(" ", $email); /* 发送获取邮件命令 */ fputs($pop3, "RETR $number\r\n"); /* 接收邮件内容 */ $content = ""; while (!feof($pop3)) { $content .= fgets($pop3, 1024); } /* 解析邮件信息 */ $header = ""; $body = ""; $is_header = true; $lines = explode("\r\n", $content); foreach ($lines as $line) { if ($is_header && strpos($line, ":") !== false) { $header .= $line."\r\n"; } else { $is_header = false; $body .= $line."\r\n"; } } /* 获取发件人信息 */ preg_match("/From: (.*)\r\n/i", $header, $matches); $from = $matches[1]; /* 获取收件人信息 */ preg_match("/To: (.*)\r\n/i", $header, $matches); $to = $matches[1]; /* 获取邮件主题 */ preg_match("/Subject: (.*)\r\n/i", $header, $matches); $subject = $matches[1]; /* 获取邮件日期 */ preg_match("/Date: (.*)\r\n/i", $header, $matches); $date = date("Y-m-d H:i:s", strtotime($matches[1])); /* 输出邮件信息 */ echo "主题:$subject<br>"; echo "发件人:$from<br>"; echo "收件人:$to<br>"; echo "日期:$date<br>"; echo "正文:<br>$body<br><br>"; } /* 发送退出命令 */ fputs($pop3, "QUIT\r\n"); /* 关闭 POP3 连接 */ fclose($pop3); ?> ``` 以上代码中,首先通过 `fsockopen()` 函数连接到 POP3 服务器,并发送登录命令进行身份验证。然后发送获取邮件列表命令,解析邮件大小和编号,并循环处理每一封邮件。对于每一封邮件,发送获取邮件命令,并解析邮件内容和邮件头信息,包括发件人、收件人、主题等。最后输出邮件信息,并发送退出命令关闭连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值