国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
This is the attached HTML file
首頁 後端開發(fā) php教程 PHP郵件專題_PHP教程

PHP郵件專題_PHP教程

Jul 21, 2016 pm 04:08 PM
boss php smtp 類別 郵件


PHP郵件專題 PHP SMTP類? ?

/***********************************
PHP MIMESMTP ver 1.0 Powered by Boss_ch, Unigenius soft ware co. Ltd
All rights reserved, Copyright 2000 ;
本類用PHP透過smtp sock 作業(yè)發(fā)送MIME 類型的郵件,可以傳送
HTML

本版本是針對個(gè)人的發(fā)送,與多人群發(fā)版本不同的是,每發(fā)送到一個(gè)人,就重新進(jìn)行一次編碼,在接收端的用戶看來,只是發(fā)送給他一個(gè)人的。
針對多人群發(fā)的情況,只發(fā)送一次,透過多個(gè)RCPTTO指令傳送到不同的人信箱中,
說明:
請把$hostname 設(shè)為你有權(quán)限的預(yù)設(shè)smtp 伺服器或是在new 時(shí)指定
把$charset 改成你的預(yù)設(shè)字元集
Html 正文中如有圖片,請用絕對路徑的引用"httP://host/path/image.gif";
並連上網(wǎng),以確保程式能讀取到圖片的資料資訊
如果是透過表單提交過來的Html 正文,請先用StripSlashes($html_body)把正文內(nèi)容進(jìn)行預(yù)處理
Html 中用到的樣式表文件,請不要用之類的引用,直接把樣式表定義放在
標(biāo)籤中

轉(zhuǎn)載請保留此版權(quán)信息,Bugs Report : [email]boss_ch@china.com[/email]
*********************************** **/
if(!isset($__smtp_class__)){
$__smtp_class__=1;

class smtp
{
var $hostname="";
var $port=25;
var $connection=0;
var $debug=1;

var $timeout=30;
var $err_str;
var $err_no;

var $autocode=true;
var $charset="??????";
var $subject="";
var $body="";
var $attach="";
var $temp_text_body;
var $temp_html_body;
var $temp_body_images;

var $bound_begin="=====powered_by_boss_chen_";
Function smtp($server="smtp.china.com",$port=25,$time_out=20)
{$this- >hostname=$server;
$this->port=$port;
$this->timeout=$time_out;
return true;
}

Function outdebug($ message)
{
echo htmlspecialchars($message)."
n";
}


function command($command,$return_lenth=1,$return_codeth=1,$return_code '2')
{
if ($this->connection==0)
{
$this->err_str="沒有連接到任何伺服器,請檢查網(wǎng)路連線";
return false;
}
if ($this->debug)
$this->outdebug(">>> $command");
if (!fputs($this->connection ,"$command rn"))
{
$this->err_str="無法傳送指令".$command;
return false;
}
else
{
{
$resp=fgets($this->connection,256);
if($this->debug)
$this->outdebug("$resp");
if (substr($resp ,0,$return_lenth)!=??$return_code)
{
$this->err_str=$command." 指令伺服器回傳無效:".$resp;
return false;
}
else
return true;
}
}


Function open()
{
if($this->hostname=="")
{$this->err_str="無效的主機(jī)名稱!!";
return false;
}

if ($this->debug) echo "$this->hostname,$ this->port,&$err_no, &$err_str, $this->timeout
";
if (!$this->connection=fsockopen($this->hostname,$this->port,& $err_no, &$err_str, $this->timeout))
{
$this->err_str="連接到SMTP 伺服器失敗,錯誤訊息:".$err_str."錯誤號碼:".$err_no ;
return false;
}
else
{
$resp=fgets($this->connection,256);
if($this->debug)
$this->outdebug("$resp");
if (substr($resp,0,1)!="2")
{$this->err_str="伺服器傳回?zé)o效的資訊:" .$resp."請檢查SMTP伺服器是否正確";
return false;
}
return true;
}
}


Function Close()
{
if($this->connection!=0)
{
fclose($this->connection);
$this->connection=0;
}
}

Function Buildhead($from_name,$to_name,$from_mail,$to_mail,$subject)
{
if (empty($from_name))
$from_name=$from_mail ;
if (empty($to_name)) $to_name=$to_mail;
$this->subject="From: =?$this->charset?B?".base64_encode($from_name)."? =rn";
$this->subject.="To: =?$this->charset?B?".base64_encode($to_name)."?=rn";
$subject=ereg_replace("n","",$subject);
$this->subject.="Subject: =?$this->charset?B?".base64_encode($subject)." ?=rn";
if ($this->debug) echo nl2br(htmlspecialchars($this->subject));
return true;
}


函數(shù) parse_html_body($html_body=null)
{
$passed="";
$image_count=0;
$this->temp_body_>$image_count=0;
$this->temp_body_images=array() ;
while (eregi("] )src[[:space:]]*=[[:space:]]*([^ ] )",$html_body,$reg))
{

$pos=@strpos($html_body,$reg[0]);
$passed.=substr($html_body,0,$pos);
$html_body= substr($html_body,$pos strlen($reg[0]));
$image_tag=$reg[2];
$image_att=$reg[1];
$tag_len= strlen($ image_tag);
if ($image_tag[0]=="'" 或$image_tag[0]=='"')
$image_tag=substr($image_tag,1);
if (substr ($image_tag,strlen($imgage_tag)-1,1)=="'" 或substr($image_tag,strlen($imgage_tag)-1,1)=='"')
$image_tag= substr($ image_tag,0,strlen($imgage_tag)-1);
//回顯$image_tag."
";
$cid=md5(uniqid(rand()));
$cid =substr($cid,0,15)."@unigenius.com";
$passed.="PHP郵件專題_PHP教程$end_pos=@strpos($html_body,'>');
$passed.=substr($html_body,0,$end_pos);
$html_body=substr($html_body,$end_pos);
//把圖片資料讀取存出來一個(gè)資料;

$img_file_con=fopen($image_tag,"r");
unset($image_data);
while ($tem_buffer= AddSlashes (fread($img_file_con,16777216)))
$image_data.=$tem_buffer;
fclose($img_file_con);
$image_exe_name=substr($img_file_con);
$image_exe_name=substr($image_tag,strpos. ,3);
switch (strtolower($image_exe_name))
{
case "jpg":
case "jpeg":
$content_type="image/jpeg ";
break;
case "gif":
$content_type="image/gif";
break;
case "png":
$content_type="image/x -png";
break;
case "tif":
$content_type="image/tif";
break;
預(yù)設(shè)值:
$content_type="image/";
break;
}

$this->temp_body_images[$image_count][name]=basename($image_tag);
$this->temp_body_images[$image_count][type ]= $content_type;
$this->temp_body_images[$image_count][cid]=$cid;
$this->temp_body_images[$image_count][data]=$image_data;
$image_count ;
}
$this->temp_html_body=$passed.$html_body;
回傳true;

}

function build_content($bound_level=0,$text_body ,$html_body,$ hava_att=false)
{
if ($html_body)
{
if (eregi("{
$bound_level ;
if ($text_body)
{
$this->body .="內(nèi)容類型:multipart/lated;
type="multipart/alternative";
tboundary="";
$this->body.=$this ->bound_begin.$bound_level.$ this ->bound_end.""rnrn";
}
else
{
$this->body.="Content-Type: multipart/相關(guān);
tboundary="";
$this->body.=$this->bound_begin.$bound_level.$this->bound_end.""rnrn";

} // 判斷是否為text 正文、 html 正文有沒有,須有不同的MIME 頭
if (!$hava_att) $this->body.="這是一條MIME 格式的多部分訊息.rnrn";
// 正文標(biāo)識,如果已經(jīng)有附件的編碼,則在正文中需要這個(gè)
$this->body.="--".$this->bound_begin.$bound_level. $this->bound_end."rn";
$this->parse_html_body($html_body);
if ($text_body)
{
$this->body.="內(nèi)容-類型:多部分/替代;
tboundary="";
$bound_level ;
$this->body.=$this->bound_begin.$bound_level.$this->bound_end。 ??>$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."rn";
$this->body .="Content-Type: text/ plain;rn";
$this->body.="tcharset="$this->charset"rn";
$this->body.= "內(nèi)容傳輸編碼:base64rn";
$ this->body.="rn".chunk_split(base64_encode(StripSlashes($text_body)))."rn";
$this->body .="--".$this->bound_begin.$bound_level。 $this->bound_end."rn";
$this->body.="Content-Type: text/html;rn" ;
$this->body.="tcharset="$this->字元集"rn";
$this->body.="Content-Transfer-Encoding: base64rn";
$this->body.="rn".chunk_split(base64_encode(StripSlashes($this-> temp_html_body) )))."rn";
$this->body.="--".$this ->bound_begin.$bound_level.$this->bound_end."--rnrn";
$ bound_level- -;
}
else
{
$this->body .="--".$this->bound_begin.$bound_level.$this->bound_end."rn";
$this->body.="Content-Type: text/html;rn" ;
$this->body.="tcharset="$this->charset"rn";
$this -> body.="內(nèi)容傳輸編碼:base64rn";
$this->body.="rn".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."rn";} //正文編碼,有或沒有文字部分,編成不同的格式。
for ($i=0;$itemp_body_images);$i )
{
$this->body.="--".$this-> ;bound_begin .$bound_level.$this->bound_end."rn";
$this->body.="Content-Type:".$this->temp_body_images[$i][類型]." ;
名稱="";
$this->body.=$this->temp_body_images[$i][name].""rn";
$this->body.= "內(nèi)容傳輸編碼:base64rn";
$this->body.="Content-ID: temp_body_images[$i][cid].">rn";
$this->body.="rn".chunk_split(base64_encode(StripSlashes($this->temp_body_images[$i][data])))."rn";
}
$ this- >body.="--".$this->bound_begin.$bound_level.$this->bound_end."--rnrn";
$bound_level--;
}
else // 有或沒有圖片,上面有圖片的處理,下面是沒有圖片的處理
{
$this->temp_html_body=$html_body;
if ($text_body)
{
$bound_level ;
$this->body.="Content-Type: multipart/alternative;
tboundary="";
$this->body.=$this->bound_begin.$bound_level.$this-> bound_end.""rnrn";

if (!$hava_att) $this->body.="rn這是一條MIME 格式的多部分訊息。 ="--".$this->bound_begin.$bound_level.$this->bound_end."rn";
$this->body.= "內(nèi)容類型:text/plain;rn";
$ this->body.="tcharset="$this->charset"rn";
$this->body.="內(nèi)容-傳輸編碼:base64rn";
$this->body.=" rn ".chunk_split(base64_encode(StripSlashes($text_body)))."rn";
$this->body.= "--".$this->bound_begin.$bound_level.$this->bound_end." rn ";
$this->body.="Content-Type: text/html;rn";
$this->body.="tcharset="$this->charset"rn";
$this->body.="內(nèi)容傳輸編碼:base64rn";
$this ->body.="rn".chunk_split(base64_encode(StripSlashes($this->temp_html_body))))."rn" ;
$this->body.="--".$this-> ;bound_begin.$bound_level.$this->bound_end."--rnrn";
$bound_level--;
}
else
{
$this->body.= "Content-Type: text/html;rn";
$this->body.="tcharset="$this->charset" rn";
$this->body.="內(nèi)容-傳輸編碼:base64rn";
$this->body.="rn".chunk_split(base64_encode(StripSlashes($this->temp_html_body))) ."rn ";
}//正文編碼,有或沒有文字部分,編成不同的格式。

}// end else
}
else // 如果沒有html 正文,只有text 正文
{
$this->body.="Content-Type: text/ plain;
tcharset="$this->charset"rn";
$this->body.="Content-Transfer-Encoding: base64rn";
$this->body.="rn" .chunk_split(base64_encode(StripSlashes($text_body)))."rn";
}
}// end function default


Function Buildbody($text_bodydbody($text_body,$. null,$att=null)
{
$this->body="MIME-Version: 1.0rn";
if (null==$att or (@count($att)==0 )) //如果沒有附件,查看正文的型別;
{
$encode_level=0;
$this->build_content($encode_level,$text_body,$html_body);
}//如果沒有附件,
// ***************************************** ***************
else //如果有附件,
{
$bound_level=0;
$this->body.="Content- Type: multipart/mixed;
tboundary="";
$bound_level ;

$this->body.=$this->bound_begin.$bound_level.$this->bound_end."""" rnrn";
$this->body.="This is a multi-part message in MIME format.rnrn";
$this->body.="--".$this->bound_begin.$ bound_level.$this->bound_end."rn";
$this->build_content($bound_level,$text_body,$html_body,true);// 編入正文部分

$num=count( $att);
for ($i=0;$i{
$file_name=$att[$i][name];
$file_source=$ att[$i][source];
$file_type=$att[$i][type];
$file_size=$att[$i][size];

if (file_exists ($file_source))
{
$file_data=addslashes(fread($fp=fopen($file_source,"r"), filesize($file_source)));
$file_data=chunk_split(base64_encode( StripSlashes($file_data)));
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."rn";
$this->body .="Content-Type: $file_type;rntname="$file_name"rnContent-Transfer-Encoding: base64rn";
$this->body.="Content-Disposition: attachment;rntfilename="$file_name"rnrn" ;
$this->body.=$file_data."rn";
}
}//end for

$this->body.="--".$this ->bound_begin.$bound_level.$this->bound_end."--rnrn";
}// end else

if ($this->debug) echo nl2br(htmlspecialchars($this->身體));

return true;
}


function send($from_name,$to_name,$from_mail,$to_mail,$subject,$text_body=false,$text_body html_body=false,$att=false)
{

if (empty($from_mail) or empty($to_mail))
{
$this->err_str="沒有指定正確的郵件地址:發(fā)送人:".$from_mail."接收人:".$to_mail;
return false;
}

if (gettype($to_mail)!="array")
$to_mail=split(",",$to_mail);//如果不是數(shù)組,轉(zhuǎn)換成數(shù)組,即使只有一個(gè)發(fā)送物件;
if (gettype($to_name)!="array")
$to_name=split(",",$to_name);//如果不是數(shù)組,轉(zhuǎn)換成數(shù)組,即使只有一個(gè)發(fā)送物件;

$this->Buildbody($text_body,$html_body,$att );
// 所有信件的內(nèi)容都是一樣的,可以只編一次,而對於不同的收信人,需要不同的Head


if (!$this->open() ) return false;
if (!$this->command("HELO $this->hostname",3,"250")) return false;
// 與伺服器建立連結(jié)
if (! $this->open()) return false;
if (!$this->command("HELO $this->hostname",3,"250")) return false;

for ( $i=0;$i{
$this->Buildhead($from_name,$to_name[$i],$from_mail,$to_mail[$i], $subject);
if (!$this->command("RSET",3,"250")) return false;
if (!$this->command("MAIL FROM:".$from_mail ,3,"250")) return false;
if (!$this->command("RCPT TO:".$to_mail[$i],3,"250")) return false;
if (!$this->command("DATA",3,"354")) return false;
// 準(zhǔn)備寄電子郵件
if ($this->debug) $this->outdebug("sending subject ;");
if (!fputs($this->connection,$this->subject)) {$this->err_str="發(fā)送郵件頭時(shí)發(fā)生錯誤! ";return false;}
if ($this->debug) $this->outdebug("sending body;");
if (!fputs($this->connection,$this->body) ) {$this->err_str="發(fā)送正文時(shí)出錯! ";return false;}
if (!fputs($this->connection,".rn")) {$this->err_str="發(fā)送正文時(shí)發(fā)生錯誤! ";return false;}//正文發(fā)送完畢,退出;
$resp=fgets($this->connection,256);
if($this->debug)
$this->outdebug ("$resp");
if (substr($resp,0,1)!="2")
{
$this->err_str="發(fā)送完後,伺服器沒有回應(yīng)! !";
return false;
}
// 寄信
}
if (!$this->command("QUIT",3,"221")) return false;
$this->close();
return true;
}

}//end class define
}//end if(!isset($__smtp_class__))
}//end if(!isset($__smtp_class__))
?>


PHP SMTP類別使用方法? ?

include("smtp.php");

$mail = new smtp("localhost") ;

$mail->debug = 0;

$sender_name = "root";
$sender_mail = "root@localhost";
$to_name = "mm";
$to_mail = "[email]mm@your.com[/email]";
$subject = "i miss u";
$content = "i miss u much.";
$att[0]["name"] = "miss.jpg";
$att[0]["source"] = "/path/to/your/miss.jpg";
$att[ 0]["type"] = "image/jpeg";
$att[0]["size"] = "10kb";
$extra_subject = "To:".$to_mail." 主題:" .$subject;

if (!$mail->send($sender_name,$to_name,$sender_mail,$to_mail,$extra_subject,$content,false,$att))
$err = 1 ;
?>




傳回檔案的MIME類型的函數(shù)guessMIMEType()? ?
function guessMIMEType($filename) {
TY/GUESS MIMEME ??>$filename = basename($filename);
if(strrchr($filename,".") == false) {
return("application/octet-stream");
}

$ext = strrchr($filename,".");
switch($ext) {
case ".gif":
return "image/gif";
break;
case ".gz":
return "application/x-gzip";
case ".htm":
case ".html":
return "text/html";
break;
case ".jpg":
return "image/jpeg";
break;
case ".tar":
return "application/x-tar";
break;
case ".txt":
return "text/plain";
break;
case ".zip":
return "application/zip";
break;
default:
return "application/octet-stream";
break;
}
}


偵測Email位址的函數(shù)IsValidEmail()Is?mail()Email
function IsValidEmail($Email) {
if(!ereg("^[[:alnum:]_.] @([[:alnum:]_] .) [[:alnum:]][[: alnum:]][[:alnum:]]?$",$Email, $regs)){
$IsValidEmail = false;
}
else
{
$IsValidEmail = true ;
}
return $IsValidEmail;
}




一個(gè)MIME MAIL類,請大家注意如何建構(gòu)一個(gè)郵件體? ?

class mime_mail
{
var $parts;
var $to;
var $from;
var $headers;
var $subject;
var $body;

/*
* void mime_mail()
* class constructor
*/
function mime_mail()
{
$this-parts = array() ;
$this->to = "";
$this->from = "";
$this->subject = "";
$this->body = "";
$this->headers = "";
}

/*
* void add_attachment(string message, [string name], [string ctype])
* Add an attachment to the mail object
*/
function add_attachment($message, $name = "", $ctype = "application/octet-stream")
{
$this->parts[] = array (
"ctype" => $ctype,
"message" => $message,
"encode" => $encode,
"name" => $name
);
}

/*
* void build_message(array part=
* Build message parts of an multipart mail
*/ $message = $part["message"];
$message = chunk_split(base64_encode($message));
$encoding = "base64";
return "Content-Type: " .$part["ctype"].
($part["name"]?";ntname="".$part["name"].""" : "").
"nContent- Transfer-Encoding: $encoding".
"nContent-Disposition: attachment".
($part["name"]?";ntfilename="".$part["name"].""" : "").
"nn$messagen";
}

/*
* void build_multipart()
* Build a multipart mail
*/
*/ build_multipart()
{
$boundary = "b".md5(uniqid(time()));
$multipart = "Content-Type: multipart/mixed;ntboundary="$boundary"nnThis is a MIME encoded message.nn--$boundary";

for($i = sizeof($this->parts)-1; $i >= 0; $i--)
{
$multipart .= "n".$this->build_message($this->parts[$i])."--$boundary";
}
return $multipart.= "--n ";
}

/*
* void send()
* Send the mail (last class-function to be called)
*/
function send()
{
$addtionheaders = "";
if (!empty($this->from))
$addtionheaders .= "From: ".$this->from."n";
if (!empty($this->headers))
$addtionheaders .= $this->headers."n";

if (!empty($this->body))
$this->add_attachment($this->body, "", "text/plain");

$addtionheaders .= "MIME-Version: 1.0n".$this->build_multipart( );

$fp = fopen("mail.txt","w");
fputs($fp,$addtionheaders);
fclose($fp);
mail( $this->to, $this->subject, "", $addtionheaders);
}
}; // 課程結(jié)束

/*
* 用法範(fàn)例
*

$attachment = fread(fopen("test.jpg", "r"), filesize("測試.jpg"));

$mail = new mime_mail();
$mail->from = "[email]foo@bar.com[/email]";
$mail ->headers = "錯誤-收件者: [email]foo@bar.com[/email]";
$mail->to = "[email]bar@foo.com[/email]";
$mail->subject = "測試...";
$mail->body = "這只是一個(gè)測試。";
$mail->add_attachment("$attachment", "test.jpg ", "image/jpeg");
$mail->send();

*/
?>


myimap類,讀取郵件? ?
//////////// /////////////////////////////// ///
//起源由Alpha.Z (5/21/ 2000)
//由Belltree 修改(11/01/2000)
////////////////////////////////////////// ////// /
class myimap
{
var $username="";
var $userpwd="";
var $hostname="";
var $port=0;
var $connection=0; // 是否連接
var $state="DISCONNECTED"; // 連線狀態(tài)
var $greeting="";
var $must_update=0;
var $inStream=0;
var $num_msg_parts = 0;
var $attach;
var $num_of_attach = 0;

function open()
{
if
function open()
{
if this->port==110)
$this->inStream=imap_open("{ ".$this->主機(jī)名稱."/pop3:110}收件匣",$this->使用者名稱,$this ->userpwd);
else
$this->inStream=imap_open(" {".$this->主機(jī)名稱.":143}INBOX",$this->使用者名稱,$this->userpwd );

if ($this->inStream)
{
//echo "使用者:$this->使用者名稱的信箱連線成功。
";
return $inStream;
}
else
{
echo "使用者:$this->使用者名稱的信箱連線失敗。
";
return 0;
}
}

函數(shù)close()
{
if(imap_close($this->inStream))
{
//echo "


已經(jīng)與伺服器$this->hostname 中斷連線。 ";
return 1;
}
else
{
//echo "
與伺服器$this->主機(jī)名稱斷開連線失敗。 ";
return 0;
}
}

function DeleteMail($msg_no){
if (@imap_delete($this->inStream,$msg_no))
回傳true;
else
回傳false;
}

function ExpungeMail(){
if (@imap_expunge($this->inStream))
回傳true;
else
回傳false;
}


function CheckMailbox($page_size,$page)
{
$mboxinfo=imap__size,$page)
{
$mboxinfo ;
//$mboxinfo=imap_check( $this->inStream);
if ($mboxinfo)
if ($mboxinfo->Nmsgs>0)
{
//echo "您的信箱:".$mboxinfo-> ;信箱."
";
echo $this->使用者名稱."@wells.com的收件匣裡共有郵件數(shù):".$mboxinfo-> Nmsgs."
n ";
echo "未讀取郵件數(shù):".$mboxinfo->未讀取。
echo "總佔(zhàn)用空間:".$mboxinfo->Size."位元組
n";
$last_page = ceil($mboxinfo->Nmsgs/$page_size);
$cur_page = $page 1;
$num_page = $last_page;
echo "第".$cur_page."頁,共".$last_page."頁。n";
}
else
{
echo "您的信箱裡沒有郵件。

n";}else{echo '錯誤:無法取得包裹箱的資訊。';
return 0;
}
echo "n";
echo " 狀態(tài) < ;td>寄件者 td>主題 時(shí)間 大小 n";
$sortby="SORTDATE";
$sort_reverse=1 ;
$sorted = imap_sort($this->inStream, $sortby, $sort_reverse, SE_UID);

for ($ i=0;$iNmsgs;$i )
{
if (($i>=$page*$page_size) and ($i$msg_no = @imap_msgno($this->inStream, $ sorted[$i]);
$msgHeader = @imap_header($this->inStream, $msg_no);
/ /日期

if (isset($msgHeader->date))
{
$date = $msgHeader->date;

if (ord($date) > 64)
$date = substr($date, 5);

$date = substr($date, 5);

$datepart = split(" ",$date);
$date = $datepart[0]." " .$datepart[1]." ".$datepart[2]." ".$datepart[3 ];
/*
while (strstr(' ', $date))
{
$date = str_replace(' ', ' ', $date);
}
*/
}

if (isset($msgHeader->from[0]))
{
$from = $msgHeader->from[0];
if ( isset($from->個(gè)人))
{
$frm = trim($this- >decode_mime_string($from->個(gè)人));
if (isset($from->郵箱) && isset ($from->主機(jī)))
{
$frm_add = $from ->郵箱。 '@'。 $from->主機(jī);
}
}
else
if (isset($from->郵箱) && isset($from->主機(jī)))
{
$frm = $from->郵箱。 '@'。 $from->host;
}
else
if (isset($msgHeader->fromaddress))
$frm = trim($h->fromaddress);
}
else
if (isset($msgHeader->fromaddress))
$frm = trim($msgHeader->fromaddress);

if (isset($msgHeader->fromaddress) toaddress))
$to = trim($msgHeader->toaddress);
else
$to = "未知";


if (isset($msgHeader->) ;subject))
$sub = trim($this->decode_mime_string($msgHeader->subject));
if ($sub == "")
$sub = "無主題" ;
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) 。 '...';
if ($msgHeader->Unseen == "U")
$newmail = "未讀";
else
$newmail = "已讀取";
echo "n";
echo "$newmailn";
echo ''.$ frm.' '.$sub.' ' .$date.' '.$msg_size.'';
echo "
n";
}}
echo "n";
echo "n";
if ($page == 0)
echo " 第一頁n";
else
echo " 第一頁 n";
if (($prev_page = $page-1) echo "前一頁n";
else
echo " 前一頁 n";

if (($next_page = $page 1) >= $last_page)
echo "後一頁n";
else
echo "後一頁 a>> ;n";
$last_page --;
if ( $last_page echo "最末頁n";
else
echo "最末頁n";
echo " n";
}

functiondecode_mime_string ( $string)
{
$pos = strpos($string, '=?');
if (!is_int($pos) )
{
return $string;
}

$preceding = substr($string, 0, $pos); // 儲存任何前面的文字

$ search = substr($string, $pos 2, 75); // mime 標(biāo)頭規(guī)範(fàn)規(guī)定這是最長的單一編碼字
$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)
{

switch ($encoding)
{
{
switch ($encoding)
{
{
switch ($encoding)
{
{
switch ($encoding)
{
{
switch ($encoding)
{
{
switch ($encoding)
{
{
switch ($encoding)
{
case 'Q':
case 'q':
$encoded_text = str_replace('_', ' ', $encoded_text);
$encoded_text = str_replace('=', '%', $ $編碼文字);
$decoded = urldecode($encoded_text);
break;

case 'B':
case 'b':
$decoded = urldecode(base64 $編碼文字));
break;

預(yù)設(shè):
$decoded = '=?' 。 $this->decode_mime_string($rest);
}


函數(shù)display_toaddress ($user, $server, $from)
{
return is_int(strpos) ($from ) , $this->get_barefrom($user, $server)));
}

函數(shù)get_barefrom($user, $server)
{
$barefrom = " $user@ $real_server";

return $barefrom;
}

函數(shù)get_struct($msg_num)
{
$struct=imap_fetchstruct($this->StreamStream $ msg_innum );
//echo gettype($struct);
return $struct;
}


函數(shù)proc_struct($msg_part, $part_no, $msg_num)
{ ??> {
if ($msg_part->ifdisposition)
{
// 看看它是否有支出,我唯一,這個(gè)將用於附件
/ / 無論如何,讓我們檢查一下
if ($msg_part->disposition == "attachment")
{
$att_name = "unknown";
for ($lcv = 0; $lcv 參數(shù)); $lcv )
{
$param = $msg_part->參數(shù)[$lcv];

if ($param->屬性 == "姓名")
{
$ att_name = $param->value;
break;
}
}

$att_name = $this->decode_mime_string($ att_name);

$att_path = $ this->使用者名稱."\".$att_name;

$this->attach[$this->num_of_attach]=$att_name; //登記附件清單
$this->num_of_attach ; //登記附件數(shù)量
/*
$att_path = $this->username."\".$this->decode_mime_string($att_name);
if ($this->attach="" )
$this->attach = $att_name;
else
$this->attach .= ";".$att_name;
*/
if (! is_dir($this- >使用者名稱))
mkdir($this->使用者名稱,0700);
$fp=fopen($att_path,"w");
switch ($msg_part->encoding)
{
情況 3: // base64
fputs($fp,imap_base64 (imap_base64 ( imap_fetchbody($this->inStream,$msg_num,$part_no)));
break;
狀況4: // QP
fputs($fp,imap_qprint(imap_fetchbody($this->; inStream, $msg_num,$part_no)));
break;
預(yù)設(shè):
fputs($fp,imap_fetchbody($this->inStream,$msg_num,$part_no));
break ;
}
fclose($fp);

//if ($msg_part->type=="5"){
// echo "n";
// echo "

n";
//}
} // END IF ATTACHMENT
else //NOT ifdisposition
{
//我猜它除了附件之外還有其他用途? ??>switch ($msg_part->type)
{
情況0:
$mime_type = "text";
break;
情況1:
$mime_type = "multipart " ;
//嘿,為什麼不使用這個(gè)函數(shù)來處理所有部分
//這個(gè)多部分部分
$this->num_msg_parts = count($msg_part-> ;parts);
for ( $i = 0; $i num_msg_parts; $i )
{
if ($part_no != "")
{
$part_no = $part_no."." ;
}
for ($i = 0; $i parts); $i )
{
$ this->proc_struct($msg_part->parts[$ i ], $part_no.($i 1), $msg_num);
}
}
break;
情況2:
$mime_type = "message";
break;
情況3:
$mime_type = "application";
break;
情況4:
$mime_type = "audio" ;
break;
情況5:
$mime_type = "image";
break;
情況6:
$mime_type = "video";
break;
案例7:
$mime_type = "model";
break;
預(yù)設(shè):
$mime_type = "unknown";
// 嗯....
}

$full_mime_type = $mime_type."/"。 msg_part->subtype;
$full_mime_type = strtolower($full_mime_type);


//決定你要做什麼有了這個(gè)部分
//如果你想顯示它,找出它,找出它,找出編碼並回顯

switch ($msg_part->encoding)
{

case 0:
情況1:
if ($this->num_msg_parts == 0 ){
echo ereg_replace("rn","
rn",imap_body($this->inStream,$ msg_num));

}
else{
if ( $ part_no!=""){
echo ereg_replace("rn","
rn",imap_fetchbody($ this->inStream,$msg_num,$part_no));} }break;情況3: //BASE64if ($full_mime_type= ="text/plain"){if ($this->num_msg_parts == 0 ){$content=imap_base64(imap_body($this->inStream,$msg_num));}
else{
$content = imap_base64(imap_fetchbody($this->inStream,$msg_num,$part_no));
$att_path = $this->使用者名稱 。 "\ 文字內(nèi)容.txt";
$fp = fopen($att_path,"w");
fputs($fp,$content);
fclose($fp);
$this ->attach[$this->num_of_attach]="文字內(nèi)容.txt";
$this->num_of_attach ;
}

echo $content;

}

if ($full_mime_type=="text/html"){
$att_path = $this-> ;使用者名稱。 "\超文本內(nèi)容.htm";
$fp = fopen($att_path,"w");
fputs($fp,imap_base64(imap_fetchbody($this->inStream,$msg_num,$part_no) ) );
fclose($fp);
$this->attach[$this->num_of_attach]="超文本內(nèi)容.htm";
$this->num_of_attach ;
}
break;
情況4: //QPRINT
//使用imap_qprint解碼
if ($this->num_msg_parts == 0){
echo ereg_replace("n" ,"
" ,imap_qprint(imap_body($this->inStream,$msg_num)));

}
else{
echo ereg_replace("n","",imap_fqprint(imap_fqbody($ this->inStream,$msg_num,$part_no)));

}
if ($full_mime_type=="text/html"){
$att_path = $this->使用者名稱。 "\qp超文本內(nèi)容.htm";
$fp = fopen($att_path,"w");
fputs($fp,imap_base64(imap_fetchbody($this->inStream,$msg_num,$part_no) )));
fclose($fp);
$this->attach[$this->num_of_attach]="qp超內(nèi)容文字.htm";
$this->num_of_attach ;
}
break;
情況5:
// 不確定這是否需要解碼
echo ereg_replace("n","
",imap_fetchbody($this- >inStream,$msg_num) );
break;
default:
//echo ereg_replace("n","
",imap_fetchbody($this->inStream,$msg_num, $part_no));
break ;
}

}
}


};

?>


myimap 的擴(kuò)展類,及p讀一封郵件的範(fàn)例(mail_read.php)? ?

require("myimap.php");

class myimap_ext extends myimap {

function get_mail_ msg_no){
$msgHeader = @imap_header($this->inStream, $msg_no);
if (isset($msgHeader->subject))
$sub = trim($this->decode_mimi_string( $msgHeader->subject));
if ($sub == "")
$sub = "無主體";
return "轉(zhuǎn)寄:".$sub;
}
function print_attaches(){
for ($i=0;$iattach);$i ){
echo "使用者名稱。 >函數(shù)list_attaches(){
for ($i=0;$iattach);$i ){
if ($i==0)
$attaches = $ this->attach[$i];
else
$attaches .= ";".$this->attach[$i];
}
回傳$attaches;
}
}

$imap=new myimap_ext;
$imap->hostname="localhost";
$imap ->port=143;
$imap->username="name ";
$imap->userpwd="password";

$imap->open();

$mail_struct=$imap->get_struct($msg);
$imap->proc_struct($mail_struct,"",$msg);
if ($imap-> num_of_attach > 0){
$imap->print_attaches();
}
@$imap->print_attaches();
}
@ $imap->close();
?>


myimap 的範(fàn)例(mail_list.php)? ?

require("myimap.php");

require("myimap.php");

$imap=new myimap;
$imap->hostname="localhost ";
$imap->port=143;
$imap->username="name";
$ imap->userpwd="password";

$imap ->open();
if ($page=="") $page=0;
$imap->CheckMailBox(10 ,$page);
@$imap->close ();
?>


如何發(fā)送附件(轉(zhuǎn))? ?
我常聽到這樣一個(gè)問題:「我有一個(gè)從網(wǎng)站發(fā)來的合約。我如何為透過表單發(fā)送的增加一個(gè)附件呢? 」
首先我要說的是要做到這個(gè)沒有什麼簡單的電子郵件的方法。你要很好地理解PHP 或
其他伺服器端的腳本語言。當(dāng)然你還要一個(gè)真正支援PHP的網(wǎng)站的帳號。手冊中搜尋過「附件」函數(shù),那麼結(jié)果可能都沒有(至少在我寫論文的時(shí)間還沒有)。發(fā)送一封附件的電子郵件時(shí),附件是和郵件一起達(dá)到實(shí)用的信箱裡的(例如,如果你給他/她發(fā)了一個(gè)PNG的圖片文件,他/她的信箱裡會包含一個(gè)txt文件(電子郵件)和一個(gè).??png文件(附件))。郵件)後面插入這個(gè)文字區(qū)塊。

以下是一個(gè)附附件(一個(gè)HTML檔案)電子郵件的範(fàn)例。我已經(jīng)標(biāo)註了其中重要的幾行:

Return-Path:
Date: Mon, 22 May 2000 19:17:29 0000
From: Someone
To: Person
Message-id:
Content-type: multipart/mixed; boundary="396d983d6b89a"
Subject: Here's the subject Subject: Here's the subject Content-transfer-encoding: 8bit

This is the body of the email.

--396d983d6b89a
type: text/html; name=attachment.html
Content-disposition: inline; filename=attachment.html
Content-transfer-encoding: 8bit



The attachment


This is the attached HTML file





--396d983d6b89a--



前面的7行是郵件的頭,其中值得注意的是Content-type頭部分。這個(gè)頭告訴郵件程式電子郵件是由一個(gè)以上的部分組成的。不含附件的郵件只有一個(gè)部分:訊息本身。附附件的電子通常至少由兩個(gè)部分組成:訊息和附件。這樣,帶兩個(gè)附件的郵件由三個(gè)部分組成:訊息,第一個(gè)附件和第二個(gè)附件。

附附件的電子郵件的不同部分之間用分界線來分隔。分界線在Content-type頭中定義。郵件的每個(gè)新部分以兩個(gè)連字號(--)和分界線開始。最後一個(gè)分界線後面也有兩個(gè)連字號,表示這個(gè)郵件中沒有其它的部分了。

在每個(gè)分界線後面有一些行,用來告訴郵件程式這個(gè)部分的內(nèi)容的類型。例如,看看上面範(fàn)例中第一個(gè)分界線後面的兩行--以Content-type: text/plain開頭的行。這些行說明後面的部分是ISO-8859-1字元集的純文字。跟在第二個(gè)分界線後的行告訴郵件程式現(xiàn)在的部分是一個(gè)HTML文件,它的名字是"attachment.html"。

Content-disposition這持告訴郵件程式如果可能就以內(nèi)嵌的方式顯示附件?,F(xiàn)在新的郵件程式會在訊息後面顯示HTML的內(nèi)容。如果Content-disposition被設(shè)為attachment,那麼郵件程式就不會顯示HTML檔案的內(nèi)容,而是顯示一個(gè)連接到檔案的圖示(或其它的類似的東西)。收件者要看附件的內(nèi)容,必須點(diǎn)選這個(gè)圖示。一般情況下,如果附件是一些文字(包含HTML),Content-disposition會設(shè)為inline,這是因?yàn)楝F(xiàn)在大部分郵件程式能夠不借助其它瀏覽器而直接顯示附件(文字)的內(nèi)容。如果附件不是文字(例如圖片或其它類似的內(nèi)容),Content-disposition就設(shè)為attachment。

2. 用PHP產(chǎn)生附附件的電子郵件

這裡一個(gè)例子,告訴你如果把一個(gè)定義好的HTML檔加為郵件的附件:

php
# 我們先寫實(shí)際的訊息內(nèi)容
$emailBody = "This is text that goes into the body of the email.";

# 然後我們要作為附件的HTML文件
$attachment = "

The attached file


This is the attached HTML file



";

# 建立在郵件中分隔不同部分的分界線。
# 基本上,分界線可以是任意的字串。
# 但是重要的一點(diǎn)是確定一個(gè)寫郵件的人
# 這會隨意寫出的字串,所以我們用
# uniqid函數(shù)來產(chǎn)生一個(gè)隨機(jī)的字串。
$boundary = uniqid( "");

# 現(xiàn)在我們要建立郵件頭。不要忘了插入
# Content-type頭來說明這個(gè)郵件包含一個(gè)或更多的附件。
$headers = "From: [email]someone@example.com[/email]
Content-type: multipart/mixed; boundary="$boundary"";

# 好,現(xiàn)在我們已經(jīng)有了郵件的所有內(nèi)容。
# 下一件事是修改郵件的主體。
$emailBody = "--$boundary
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8bit

$emailBody

--$boundary
Content-type: text/html; name=attachment.html
Content-disposition: inline; filename=attachment.html
Content-transfer-encoding: 8bit

$attachment

--$boundary--";

# 現(xiàn)在可以把郵件發(fā)出去了
mail( "[email]person@eksempel.dk[/email ]", "The subject", $emailBody, $headers);
?>



3. 把使用者上傳的檔案當(dāng)作附件

你也許會覺得上面的例子難以理解,但下面...。在下面的例子中事情更難了,因?yàn)槲覀円靡粋€(gè)表單讓用戶上傳他們的文件,並把這個(gè)文件作為我們要發(fā)送的郵件的附件。麻煩的是我們不能預(yù)先知道檔案的MIME類型。在前面的範(fàn)例中,我們已經(jīng)知道該它是一個(gè)HTML文件,所以給這個(gè)附件設(shè)定Content-type頭是很簡單的。在下面的例子中,MIME類型可能是任意的,因?yàn)槭褂谜呖赡軙蟼饕粋€(gè)HTML文件,一個(gè)PNG文件,一個(gè)vCard文件,或者其它的東西。讓我們來看看範(fàn)例:

# 現(xiàn)在我們來產(chǎn)生表單。在產(chǎn)生可以上傳檔案的表單時(shí),
# 不要忘了把
標(biāo)籤的"enctype"屬性設(shè)為"multipart/form-data".
echo "
n";
echo "
n";
echo "
n";
echo "
n";
echo "
n";
echo "
n";
echo "n";
echo "
n";

# 如果使用者已經(jīng)按了"Send"按鈕"
if ($send) {
# 定義分界線
$boundary = uniqid( "");

# 產(chǎn)生郵件頭
$headers = "From: $from
Content-type: multipart/mixed ; boundary="$boundary"";

# 決定上傳檔案的MIME類型
if ($attachment_type) $mimeType = $attachment_type;
# 如果瀏覽器沒有指定檔案的MIME類型,
# 我們可以把它設(shè)為"application/unknown".
else $mimeType = "application/unknown";

# 決定檔案的名字
$fileName = $attachment_name
# 開啟檔案
$fp = fopen($attachment, "r");
# 把整個(gè)檔案讀入一個(gè)變數(shù)
$read = fread($fp, filesize($attachment ));

# 好,現(xiàn)在變數(shù)$read中儲存的是包含整個(gè)檔案內(nèi)容的文字區(qū)塊。
# 現(xiàn)在我們要把這個(gè)文字區(qū)塊轉(zhuǎn)換成郵件程式可以讀懂的格式
# 我們用base64方法把它編碼
$read = base64_encode($read);

#現(xiàn)在我們有一個(gè)用base64方法編碼的長字串。
# 下一件事是要把這個(gè)長字串切成由每行76個(gè)字元組成的小塊
$read = chunk_split($read);

# 現(xiàn)在我們可以建立郵件的主體
$body = "--$boundary
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8bit

$body

--$boundary
Content-type: $mimeType; name=$fileName
Content-disposition: attachment; filename=$fileName
Content-transfer-encoding: base64

$read

--$boundary--";

# 寄信
mail($to, $subject, $body, $headers);
}
?>


這就是全部。如果你不能很好地理解上面的例子,我的建議是給你自己發(fā)送幾個(gè)附件的郵件,然後仔細(xì)研究郵件的原始碼。

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314797.htmlTechArticlePHP郵件專題PHP SMTP類?/**************** ******************* PHP MIMESMTP ver 1.0 Powered by Boss_ch, Unigenius soft ware co. Ltd All rights reserved, Copyright 2000 ; 類用...
本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

我如何了解最新的PHP開發(fā)和最佳實(shí)踐? 我如何了解最新的PHP開發(fā)和最佳實(shí)踐? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

什麼是PHP,為什麼它用於Web開發(fā)? 什麼是PHP,為什麼它用於Web開發(fā)? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

如何設(shè)置PHP時(shí)區(qū)? 如何設(shè)置PHP時(shí)區(qū)? Jun 25, 2025 am 01:00 AM

tosetTherightTimeZoneInphp,restate_default_timezone_set()functionAtthestArtofyourscriptWithavalIdidentIdentifiersuchas'america/new_york'.1.usedate_default_default_timezone_set_set()

我如何驗(yàn)證PHP中的用戶輸入以確保其符合某些標(biāo)準(zhǔn)? 我如何驗(yàn)證PHP中的用戶輸入以確保其符合某些標(biāo)準(zhǔn)? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

什麼是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? 什麼是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? Jun 22, 2025 am 01:03 AM

thephpfunctionserize()andunSerialize()redustoconvertComplexdatStructDestoresToroStoroStoroSandaBackagagain.1.Serialize()

如何將PHP代碼嵌入HTML文件中? 如何將PHP代碼嵌入HTML文件中? Jun 22, 2025 am 01:00 AM

可以將PHP代碼嵌入HTML文件中,但需確保文件以.php為擴(kuò)展名,以便服務(wù)器能正確解析。使用標(biāo)準(zhǔn)的標(biāo)籤包裹PHP代碼,可在HTML中任意位置插入動態(tài)內(nèi)容。此外,可在同一文件中多次切換PHP與HTML,實(shí)現(xiàn)條件渲染等動態(tài)功能。務(wù)必注意服務(wù)器配置及語法正確性,避免因短標(biāo)籤、引號錯誤或遺漏結(jié)束標(biāo)籤導(dǎo)致問題。

編寫清潔和可維護(hù)的PHP代碼的最佳實(shí)踐是什麼? 編寫清潔和可維護(hù)的PHP代碼的最佳實(shí)踐是什麼? Jun 24, 2025 am 12:53 AM

寫乾淨(jìng)、易維護(hù)的PHP代碼關(guān)鍵在於清晰命名、遵循標(biāo)準(zhǔn)、合理結(jié)構(gòu)、善用註釋和可測試性。 1.使用明確的變量、函數(shù)和類名,如$userData和calculateTotalPrice();2.遵循PSR-12標(biāo)準(zhǔn)統(tǒng)一代碼風(fēng)格;3.按職責(zé)拆分代碼結(jié)構(gòu),使用MVC或Laravel式目錄組織;4.避免麵條式代碼,將邏輯拆分為單一職責(zé)的小函數(shù);5.在關(guān)鍵處添加註釋並撰寫接口文檔,明確參數(shù)、返回值和異常;6.提高可測試性,採用依賴注入、減少全局狀態(tài)和靜態(tài)方法。這些做法提升代碼質(zhì)量、協(xié)作效率和後期維護(hù)便利性。

如何使用PHP執(zhí)行SQL查詢? 如何使用PHP執(zhí)行SQL查詢? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles