整理系列-20161118- PHP 邮件接收功能实现「部分资源mark」
来源:互联网 发布:数学专业程序员 编辑:程序博客网 时间:2024/05/23 23:56
摘自
伟大的互联网
参考及代码
- 邮件接收类 这个没试过,关键是忘记自己存bookmark了。。
下面的亲测可用:
- POP3
<?php class SocketPOPClient { var $strMessage = ''; var $intErrorNum = 0; var $bolDebug = false; var $strEmail = ''; var $strPasswd = ''; var $strHost = ''; var $intPort = 110; var $intConnSecond = 30; var $intBuffSize = 8192; var $resHandler = NULL; var $bolIsLogin = false; var $strRequest = ''; var $strResponse = ''; var $arrRequest = array(); var $arrResponse = array(); //--------------- // 基础操作 //--------------- //构造函数 function SocketPOPClient($strLoginEmail, $strLoginPasswd, $strPopHost='', $intPort='') { $this->strEmail = trim(strtolower($strLoginEmail)); $this->strPasswd = trim($strLoginPasswd); $this->strHost = trim(strtolower($strPopHost)); if ($this->strEmail=='' || $this->strPasswd=='') { $this->setMessage('Email address or Passwd is empty', 1001); return false; } if (!PReg_match("/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/i", $this->strEmail)) { $this->setMessage('Email address invalid', 1002); return false; } if ($this->strHost=='') { $this->strHost = substr(strrchr($this->strEmail, "@"), 1); } if ($intPort!='') { $this->intPort = $intPort; } $this->connectHost(); } //连接服务器 function connectHost() { if ($this->bolDebug) { echo "Connection ".$this->strHost." ...\r\n"; } if (!$this->getIsConnect()) { if ($this->strHost=='' || $this->intPort=='') { $this->setMessage('POP3 host or Port is empty', 1003); return false; } $this->resHandler = @fsockopen($this->strHost, $this->intPort, &$this->intErrorNum, &$this->strMessage, $this->intConnSecond); if (!$this->resHandler) { $strErrMsg = 'Connection POP3 host: '.$this->strHost.' failed'; $intErrNum = 2001; $this->setMessage($strErrMsg, $intErrNum); return false; } $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } } return true; } //关闭连接 function closeHost() { if ($this->resHandler) { fclose($this->resHandler); } return true; } //发送指令 function sendCommand($strCommand) { if ($this->bolDebug) { if (!preg_match("/PASS/", $strCommand)) { echo "Send Command: ".$strCommand."\r\n"; } else { echo "Send Command: PASS ******\r\n"; } } if (!$this->getIsConnect()) { return false; } if (trim($strCommand)=='') { $this->setMessage('Request command is empty', 1004); return false; } $this->strRequest = $strCommand."\r\n"; $this->arrRequest[] = $strCommand; fputs($this->resHandler, $this->strRequest); return true; } //提取响应信息第一行 function getLineResponse() { if (!$this->getIsConnect()) { return false; } $this->strResponse = fgets($this->resHandler, $this->intBuffSize); $this->arrResponse[] = $this->strResponse; return $this->strResponse; } //提取若干响应信息,$intReturnType是返回值类型, 1为字符串, 2为数组 function getRespMessage($intReturnType) { if (!$this->getIsConnect()) { return false; } if ($intReturnType == 1) { $strAllResponse = ''; while(!feof($this->resHandler)) { $strLineResponse = $this->getLineResponse(); if (preg_match("/^\+OK/", $strLineResponse)) { continue; } if (trim($strLineResponse)=='.') { break; } $strAllResponse .= $strLineResponse; } return $strAllResponse; } else { $arrAllResponse = array(); while(!feof($this->resHandler)) { $strLineResponse = $this->getLineResponse(); if (preg_match("/^\+OK/", $strLineResponse)) { continue; } if (trim($strLineResponse)=='.') { break; } $arrAllResponse[] = $strLineResponse; } return $arrAllResponse; } } //提取请求是否成功 function getRestIsSucceed($strRespMessage='') { if (trim($responseMessage)=='') { if ($this->strResponse=='') { $this->getLineResponse(); } $strRespMessage = $this->strResponse; } if (trim($strRespMessage)=='') { $this->setMessage('Response message is empty', 2003); return false; } if (!preg_match("/^\+OK/", $strRespMessage)) { $this->setMessage($strRespMessage, 2000); return false; } return true; } //获取是否已连接 function getIsConnect() { if (!$this->resHandler) { $this->setMessage("Nonexistent availability connection handler", 2002); return false; } return true; } //设置消息 function setMessage($strMessage, $intErrorNum) { if (trim($strMessage)=='' || $intErrorNum=='') { return false; } $this->strMessage = $strMessage; $this->intErrorNum = $intErrorNum; return true; } //获取消息 function getMessage() { return $this->strMessage; } //获取错误号 function getErrorNum() { return $this->intErrorNum; } //获取请求信息 function getRequest() { return $this->strRequest; } //获取响应信息 function getResponse() { return $this->strResponse; } //--------------- // 邮件原子操作 //--------------- //登录邮箱 function popLogin() { if (!$this->getIsConnect()) { return false; } $this->sendCommand("USER ".$this->strEmail); $this->getLineResponse(); $bolUserRight = $this->getRestIsSucceed(); $this->sendCommand("PASS ".$this->strPasswd); $this->getLineResponse(); $bolPassRight = $this->getRestIsSucceed(); if (!$bolUserRight || !$bolPassRight) { $this->setMessage($this->strResponse, 2004); return false; } $this->bolIsLogin = true; return true; } //退出登录 function popLogout() { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand("QUIT"); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return true; } //获取是否在线 function getIsOnline() { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand("NOOP"); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return true; } //获取邮件数量和字节数(返回数组) function getMailSum($intReturnType=2) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand("STAT"); $strLineResponse = $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } if ($intReturnType==1) { return $this->strResponse; } else { $arrResponse = explode(" ", $this->strResponse); if (!is_array($arrResponse) || count($arrResponse)<=0) { $this->setMessage('STAT command response message is error', 2006); return false; } return array($arrResponse[1], $arrResponse[2]); } } //获取指定邮件得session Id function getMailSessId($intMailId, $intReturnType=2) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } if (!$intMailId = intval($intMailId)) { $this->setMessage('Mail message id invalid', 1005); return false; } $this->sendCommand("UIDL ". $intMailId); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } if ($intReturnType == 1) { return $this->strResponse; } else { $arrResponse = explode(" ", $this->strResponse); if (!is_array($arrResponse) || count($arrResponse)<=0) { $this->setMessage('UIDL command response message is error', 2006); return false; } return array($arrResponse[1], $arrResponse[2]); } } //取得某个邮件的大小 function getMailSize($intMailId, $intReturnType=2) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand("LIST ".$intMailId); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } if ($intReturnType == 1) { return $this->strResponse; } else { $arrMessage = explode(' ', $this->strResponse); return array($arrMessage[1], $arrMessage[2]); } } //获取邮件基本列表数组 function getMailBaseList($intReturnType=2) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand("LIST"); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return $this->getRespMessage($intReturnType); } //获取指定邮件所有信息,intReturnType是返回值类型,1是字符串,2是数组 function getMailMessage($intMailId, $intReturnType=1) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } if (!$intMailId = intval($intMailId)) { $this->setMessage('Mail message id invalid', 1005); return false; } $this->sendCommand("RETR ". $intMailId); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return $this->getRespMessage($intReturnType); } //获取某邮件前指定行, $intReturnType 返回值类型,1是字符串,2是数组 function getMailTopMessage($intMailId, $intTopLines=10, $intReturnType=1) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } if (!$intMailId=intval($intMailId) || !$intTopLines=int($intTopLines)) { $this->setMessage('Mail message id or Top lines number invalid', 1005); return false; } $this->sendCommand("TOP ". $intMailId ." ". $intTopLines); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return $this->getRespMessage($intReturnType); } //删除邮件 function delMail($intMailId) { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } if (!$intMailId=intval($intMailId)) { $this->setMessage('Mail message id invalid', 1005); return false; } $this->sendCommand("DELE ".$intMailId); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return true; } //重置被删除得邮件标记为未删除 function resetDeleMail() { if (!$this->getIsConnect() && $this->bolIsLogin) { return false; } $this->sendCommand("RSET"); $this->getLineResponse(); if (!$this->getRestIsSucceed()) { return false; } return true; } //--------------- // 调试操作 //--------------- //输出对象信息 function printObject() { print_r($this); exit; } //输出错误信息 function printError() { echo "[Error Msg] : $strMessage <br>\n"; echo "[Error Num] : $intErrorNum <br>\n"; exit; } //输出主机信息 function printHost() { echo "[Host] : $this->strHost <br>\n"; echo "[Port] : $this->intPort <br>\n"; echo "[Email] : $this->strEmail <br>\n"; echo "[Passwd] : ******** <br>\n"; exit; } //输出连接信息 function printConnect() { echo "[Connect] : $this->resHandler <br>\n"; echo "[Request] : $this->strRequest <br>\n"; echo "[Response] : $this->strResponse <br>\n"; exit; } }//测试代码 //例如:$o = SocketPOP3Client('邮箱地址', '密码', 'POP3服务器', 'POP3端口')set_time_limit(0); $o = new SocketPOPClient('xxx@163.com', 'xxx', 'pop.163.com', '110'); $result = $o->popLogin(); //$o->printHost();var_dump($result);echo "1";print_r($o->getMailBaseList()); print_r($o->getMailSum(1)); print_r($o->getMailTopMessage(2, 2, 2)); $o->popLogout(); $o->closeHost(); $o->printObject(); ?>
- IMAP 这个找不到从哪儿来的了–。。
<?php//by Alpha.Z//05/21/2000class myimap{ var $username=""; var $userpwd=""; var $hostname=""; var $port=0; var $connection=0;//是否连接 var $state="DISCONNECTED";//连接状态 var $greeting=""; var $must_update=0; var $inStream=0; Function open() { if ($this->port==110) $this->inStream=imap_open("{{$this->hostname}:110}inbox",$this->username,$this->userpwd); else $this->inStream=imap_open("{{$this->hostname}:143}inbox",$this->username,$this->userpwd); if ($this->inStream) { echo "用户:$this->username 的信箱连接成功。<br>"; return $inStream; } else { echo "用户:$this->username 的信箱连接失败。<br>"; return 0; } } Function close() { if(imap_close($this->inStream)) { echo "<hr>已经与服务器 $this->hostname 断开连接。"; return 1; } else { echo "<hr>与服务器 $this->hostname 断开连接失败。"; return 0; } } Function CheckMailbox() { $mboxinfo=@imap_mailboxmsginfo($this->inStream); //$mboxinfo=imap_check($this->inStream); if ($mboxinfo) if ($mboxinfo->Nmsgs>0) { echo "您的收件箱里共有邮件数:".$mboxinfo->Nmsgs."<br>"; echo "未读邮件数:".$mboxinfo->Unread."<br>"; //echo "新邮件数:".$mboxinfo->Rescent."<br>"; echo "总共占用空间:".$mboxinfo->Size."字节<br>"; echo "最新邮件日期:".$mboxinfo->Date."<br><hr>"; } else { echo "您的信箱里没有邮件。<br><hr>"; } else { echo '<font color="RED">错误:无法获取收件箱的信息。</font>'; } echo '<table border="1">'; $sortby="SORTDATE"; $sort_reverse=1; $sorted = imap_sort($this->inStream, $sortby, $sort_reverse, SE_UID); for ($i=1;$i<=$mboxinfo->Nmsgs;$i++) { $msgHeader = @imap_header($this->inStream, imap_msgno($this->inStream, $sorted[$i-1])); //日期 if (isset($msgHeader->date)) { $date = $msgHeader->date; if (ord($date) > 64) $date = substr($date, 5); while (strstr('', $date)) { $date = str_replace('', ' ', $date); } } if (isset($msgHeader->from[0])) { $from = $msgHeader->from[0]; if (isset($from->personal)) { $frm = trim($from->personal); } else if (isset($from->mailbox) && isset($from->host)) { $frm = $from->mailbox . '@' . $from->host; } else if (isset($msgHeader->fromaddress)) $frm = trim($h->fromaddress); } else if (isset($msgHeader->fromaddress)) $frm = trim($msgHeader->fromaddress); if (isset($msgHeader->toaddress)) $to = trim($msgHeader->toaddress); else $to = "未知"; if (isset($msgHeader->subject)) $sub = trim($this->decode_mime_string($msgHeader->subject)); if (isset($msgHeader->Size)) $msg_size = ($msgHeader->Size > 1024) ? sprintf("%.0f kb", $msgHeader->Size / 1024) : $msgHeader->Size; if (strlen($frm) > 50) $frm = substr($frm, 0, 50) . '...'; if (strlen($sub) > 50) $sub = substr($sub, 0, 50) . '...'; echo '<tr>'; echo '<td align="center"><input type="checkbox" name="check"></td><td>'.$frm.'</td><td><a href="showbody_imap.php?usr='.$this->username.'&pwd='.$this->userpwd.'&msg='.$i.'">'.$sub.'</a></td><td>'.$date.'</td><td>'.$msg_size.'</td>'; echo '</tr>'; } echo "</table>"; } Function decode_mime_string ($string) { $pos = strpos($string, '=?'); if (!is_int($pos)) { return $string; } $preceding = substr($string, 0, $pos); // save any preceding text $search = substr($string, $pos+2, 75); // the mime header spec says this is the longest a single encoded word can be $d1 = strpos($search, '?'); if (!is_int($d1)) { return $string; } $charset = substr($string, $pos+2, $d1); $search = substr($search, $d1+1); $d2 = strpos($search, '?'); if (!is_int($d2)) { return $string; } $encoding = substr($search, 0, $d2); $search = substr($search, $d2+1); $end = strpos($search, '?='); if (!is_int($end)) { return $string; } $encoded_text = substr($search, 0, $end); $rest = substr($string, (strlen($preceding . $charset . $encoding . $encoded_text)+6)); switch ($encoding) { case 'Q': case 'q': $encoded_text = str_replace('_', '%20', $encoded_text); $encoded_text = str_replace('=', '%', $encoded_text); $decoded = urldecode($encoded_text); break; case 'B': case 'b': $decoded = urldecode(base64_decode($encoded_text)); break; default: $decoded = '=?' . $charset . '?' . $encoding . '?' . $encoded_text . '?='; break; } return $preceding . $decoded . decode_mime_string($rest); } Function display_toaddress ($user, $server, $from) { return is_int(strpos($from, $this->get_barefrom($user, $server))); } Function get_barefrom($user, $server) { $barefrom = "$user@$real_server"; return $barefrom; } Function get_structure($msg_num) { $structure=imap_fetchstructure($this->inStream,$msg_num); //echo gettype($structure); return $structure; } Function proc_structure($msg_part, $part_no, $msg_num) { if ($msg_part->ifdisposition) { // See if it has a disposition // The only thing I know of that this // would be used for would be an attachment // Lets check anyway if ($msg_part->disposition == "ATTACHMENT") { // If it is an attachment, then we let people download it // First see if they sent a filename $att_name = "unknown"; for ($lcv = 0; $lcv <count($msg_part->parameters); $lcv++) { $param = $msg_part->parameters[$lcv]; if ($param->attribute == "NAME") { $att_name = $param->value; break; } } // You could give a link to download the attachment here.... echo '<a href="'.$att_name.'">'.$att_name.'</a><br>'; $fp=fopen(".$att_name","w+"); fputs($fp,imap_base64(imap_fetchbody($this->inStream,$msg_num,$part_no))); fclose($fp); } else { // I guess it is used for something besides attachments???? } } else { // Not an attachment, lets see what this part is... switch ($msg_part->type) { case TYPETEXT: $mime_type = "text"; break; case TYPEMULTIPART: $mime_type = "multipart"; // Hey, why not use this function to deal with all the parts // of this multipart part :) for ($i = 0; $i <count($msg_part->parts); $i++) { if ($part_no != "") { $part_no = $part_no."."; } for ($i = 0; $i <count($msg_part->parts); $i++) { $this->proc_structure($msg_part->parts[$i], $part_no.($i + 1), $msg_num); } } break; case TYPEMESSAGE: $mime_type = "message"; break; case TYPEAPPLICATION: $mime_type = "application"; break; case TYPEAUDIO: $mime_type = "audio"; break; case TYPEIMAGE: $mime_type = "image"; break; case TYPEVIDEO: $mime_type = "video"; break; case TYPEMODEL: $mime_type = "model"; break; default: $mime_type = "unknown"; // hmmm.... } $full_mime_type = $mime_type."/".$msg_part->subtype; //echo $full_mime_type.'<hr>'; // Decide what you what to do with this part // If you want to show it, figure out the encoding and echo away switch ($msg_part->encoding) { case ENCBASE64: // use imap_base64 to decode $fp=fopen(".$att_name","w+"); fputs($fp,imap_base64(imap_fetchbody($this->inStream,$msg_num,$part_no))); fclose($fp); break; case ENCQUOTEDPRINTABLE: // use imap_qprint to decode //echo ereg_replace("n","<br>",imap_qprint(imap_fetchbody($this->inStream,$msg_num,$part_no))); echo '<pre>'.imap_qprint(imap_fetchbody($this->inStream,$msg_num,$part_no)).'</pre>'; break; case ENCOTHER: // not sure if this needs decoding at all break; default: } } }}$o = new myimap();$o->username="***@163.com";$o->userpwd="***";$o->hostname="imap.163.com";$o->open();?>
0 0
- 整理系列-20161118- PHP 邮件接收功能实现「部分资源mark」
- 果断Mark!国外程序员收集整理的PHP资源大全
- 果断Mark!国外程序员收集整理的PHP资源大全
- 果断Mark!国外程序员收集整理的PHP资源大全
- 果断Mark!国外程序员收集整理的PHP资源大全
- 果断Mark!国外程序员收集整理的PHP资源大全
- 果断Mark!国外程序员收集整理的PHP资源大全
- 果断Mark!国外程序员收集整理的PHP资源大全
- PHP发送邮件功能实现
- php实现邮件发送功能.
- 邮件功能相关资源
- JSP实现接收邮件
- javamail实现邮件接收
- 部分程序员资源整理
- php 跳转分页 (部分功能实现)
- 学习注水-20161113-php-imap收取邮件知识点mark一下「待解一个已知的bug」
- php imap gmail接收邮件。
- php 邮件接收代码,接收邮件处理主题和内容
- C——结构体
- getResourceAsStream路径问题
- 工作血的总结!给客户提供的SDK少花点时间
- Android基础之9-Patch(9.PNG)与padding
- 终端录屏工具: ttygif
- 整理系列-20161118- PHP 邮件接收功能实现「部分资源mark」
- hdu1535 Invitation Cards(spfa+反向建图)
- 原型和继承
- 20161118
- 413_生成二维码
- Introduction to Programming with c++ 13-3 文件结束符的一点小问题
- 常用vim命令
- nginx php-fpm安装配置
- tcp/ip简介------tcp/ip背景介绍