本文是接續此篇文章 透過cPanel+IMAP&POP3實作10分鐘時效的電子信箱

根據上述文章設定好cPanel後,我們可以將寄到@fishyes.tw的未知收件人信件都轉寄到10mail@fishyes.tw中,接著要實作PHP+IMAP來讀取信件,不採用POP協定是因為IMAP的優點適合用在此種類型(Webmail)的收信方式,加上PHP已經有內建函數支援IMAP,使用IMAP會比POP來的方便許多。

為了方便撰寫文章,我只列出重點部分的程式加以說明,真正要實際上線使用,還需要加上些流程控制之類的程式,這部分就請自行寫吧!

先設定一下主機資訊

//======設置主機資訊
//imap主機:填上正確的連線主機
$mail_server="mail.YOURHOST.net";
//imap連接字串:請依照您的主機設定作修正
$mail_link="{{$mail_server}:143/imap/notls}" ;
//主機
$mail_host="YOURHOST.net";
//收信帳號:用來存放臨時信件的帳號
$mail_user="10mail@YOURHOST.net";
//收信密碼
$mail_pass="PASSWORD";
//保留時間(秒)
$mail_time="600"; //10分鐘

第一步:產生亂數的臨時信箱

$mail_get_user=RandStr(10); //亂數選取長度10的臨時帳號
$mail_get_mail=$mail_get_user."@".$mail_host; //組合出MAIL
$mail_get_time=time(); //取得目前時間
$mail_duetime=$mail_get_time+$mail_time; //取得到期時間(目前時間+600秒)

//將這些資訊寫入SESSION中
$_SESSION["mail_user"]=$mail_get_user;
$_SESSION["mail_mail"]=$mail_get_mail;
$_SESSION["mail_duetime"]=$mail_get_duetime;

function RandStr

function RandStr($num=10){
	$ps="";
	while(strlen($ps)<$num){
		switch(rand(1,2)){
		case 1:
			$ps=$ps.chr(rand(97,122));
			break;
		case 2:
			$ps=$ps.chr(rand(48,57));
			break;
		}
	}
	return $ps;
}

第二步:當使用者重新整理網頁後,檢查10mail@YOURHOST.net找出正確的信件

//開啟信箱imap_open
$mbox = imap_open($mail_link,$mail_user,$mail_pass);

echo '<h2>收信夾</h2>';
echo '<table name="MessageList">';
echo "<tr>";
echo "<td>寄件者</td>";
echo "<td>標題</td>";
echo "<td>日期</td>";
echo "</tr>";
//imap_search搜尋信箱中,收件者為$_SESSION["mail_mail"]的信件
$mails=imap_search($mbox,'TO '.$_SESSION["mail_mail"]);

if ($mails == false) {
	echo "</table>";
} else {
	//找到信件
	while (list ($key,$val) = each ($mails)) {
		//列出每一筆資料
		$mail_list = imap_headerinfo($mbox, $val);
		$tmp_subject = HTMLFormatSender(decode_mime_string($mail_list->subject,"UTF-8"));
		$tmp_subject=($tmp_subject=="")?"無標題":$tmp_subject;
		echo "<tr>";
		echo "<td>".HTMLFormatSender(decode_mime_string($mail_list->fromaddress,"UTF-8"))."</td>";
		echo "<a href='mail.php?mid=".imap_uid($mbox,$val)."'>".$tmp_subject."</a>";
		echo "<td>".HTMLFormatSender($mail_list->MailDate)."</td>";
		echo "</tr>";
	}
	echo "</table>";
}
//關閉信箱
imap_close($mbox);

一般而言,信件的主旨都會經過編碼,這裡的function decode_mime_string是將標題轉換為一般文字,並編碼為UTF-8

//<,>轉換
function HTMLFormatSender($sender) {
	$sender = eregi_replace(""", "", $sender);
	$sender = eregi_replace("<", "<", $sender);
	$sender = eregi_replace(">", ">", $sender);
	return $sender;
}
//qp轉換
function decode_qp($text) {
	$text = quoted_printable_decode($text);
	$text = ereg_replace("=r", "r", $text);
	return $text;
}
//MIME轉換
function decode_mime_string($string,$code_set) {
	if(($pos = strpos($string,"=?")) === false) return $string;
		while(!($pos === false)) {
			$newresult .= substr($string,0,$pos);
			$string = substr($string,$pos+2,strlen($string));
			$intpos = strpos($string,"?");
			$charset = substr($string,0,$intpos);
			if (strtolower($charset)=="gb2312")$charset="gbk";
			$enctype = strtolower(substr($string,$intpos+1,1));
			$string = substr($string,$intpos+3,strlen($string));
			$endpos = strpos($string,"?=");
			$mystring = substr($string,0,$endpos);
			$string = substr($string,$endpos+2,strlen($string));
			if($enctype == "q") {
				$mystring = str_replace("_"," ",$mystring);
				$mystring = decode_qp($mystring);
			} else if ($enctype == "b")
				$mystring = base64_decode($mystring);
			$mystring=mb_convert_encoding($mystring,$code_set,$charset);
			$newresult .= $mystring;
			$pos = strpos($string,"=?");
		}
	return $newresult.$string;
}

到目前為止,已經可以讀取出信件列表了!