PHPÓʼþרÌâ

5年以前  |  阅读数:772 次  |  编程语言:PHP 

PHPÓʼþרÌa PHP SMTPÀa
<?
/***
PHP MIMESMTP ver 1.0 Powered by Boss_ch£¬ Unigenius soft ware co. Ltd
All rights reserved, Copyright 2000 ;
±¾ÀaÓÃPHPͨ¹ýsmtp sock ²Ù×÷¢ËÍMIME ÀaÐ͵ÄÓʼþ£¬¿ÉÒÔ¢ËÍ
HTML ¸ñʽµÄÕýÎÄ¡¢¸½¼þ£¬²ÉÓÃbase64 ±aÂe
±¾°ae±¾ÊÇÕe¶Ô¸oÈ˵ĢËÍ£¬Óe¶aÈËȺ¢°ae±¾²»Í¬µÄÊÇ£¬Ã¿¢Ë͵½Ò»¸oÈË£¬¾ÍÖØнøÐÐÒ»´Î±aÂe£¬ÔÚ½ÓÊն˵ÄÓû§¿´À´£¬Ö»ÊÇ¢Ë͸øËuÒ»¸oÈ˵ġ£
Õe¶Ô¶aÈËȺ¢µÄÇe¿o£¬Ö»¢ËÍÒ»´Î£¬Í¨¹ý¶a¸oRCPTTOÃuÁi¢Ë͵½²»Í¬µÄÈËÐÅÏaÖУ¬
˵Ã÷£º
Çe°Ñ$hostname ÉeΪÄaÓÐȨÏÞµÄĬÈÏsmtp
þÎñÆ÷»oÊÇÔÚnew ʱָ¶¨
°Ñ$charset ¸Ä³ÉÄaµÄĬÈÏ×Öu¼¯
Html ÕýÎÄÖÐÈçÓÐͼƬ£¬ÇeÓþø¶ÔÂ
¾¶µÄÒýÓÃ"httP://host/path/image.gif";
²¢Á¬ÉÏÍø£¬ÒÔ±£Ö¤³ÌÐoÄܶÁÈ¡µ½Í¼Æ¬µÄÊý¾ÝÐÅÏ¢
Èç¹uÊÇͨ¹ý±iµ¥Ìa½»¹ýÀ´µÄHtml ÕýÎÄ£¬ÇeÏÈÓÃStripSlashes($html_body)°ÑÕýÎÄÄÚÈݽøÐÐÔ¤´¦Ài
Html ÖÐÓõ½µÄÑuʽ±iÎļþ£¬Çe²»ÒªÓÃÖ®ÀaµÄÒýÓã¬Ö±½Ó°ÑÑuʽ±i¶¨Òa*ÅÔÚ

±eÇ(C)ÖÐ

תÔØÇe±£Áo´Ë°aeȨÐÅÏ¢£¬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_bosschen";
var $bound_end="046484063883=====";

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_code='2')
{
if ($this->connection==0)
{
$this->err_str="ûÓÐÁ¬½Óµ½ÈκÎþÎñÆ÷£¬Çe¼i²eÍøÂçÁ¬½Ó";
return false;
}
if ($this->debug)
$this->outdebug(">>> $command");
if (!fputs($this->connection,"$command \r\n"))
{
$this->err_str="ÎÞ
¨¢ËÍÃuÁi".$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." ÃuÁi
þÎñÆ÷*µ»ØÎÞЧ:".$resp;
return false;
}
else
return true;
}
}

Function open()
{
if($this->hostname=="")
{$this->err_str="ÎÞЧµÄÖ÷»uÃu!!";
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 þÎñÆ÷ʧ°Ü,´iÎoÐÅÏ¢£º".$err_str."´iÎoºÅ£º".$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="
þÎñÆ÷µ»ØÎÞЧµÄÐÅÏ¢£º".$resp."Çe¼i²eSMTPþÎñÆ÷ÊÇñÕýÈ";
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)."?=<$from_mail>\r\n";
$this->subject.="To: =?$this->charset?B?".base64_encode($to_name)."?=<$to_mail>\r\n";
$subject=ereg_replace("\n","",$subject);
$this->subject.="Subject: =?$this->charset?B?".base64_encode($subject)."?=\r\n";
if ($this->debug) echo nl2br(htmlspecialchars($this->subject));
return true;
}

Function parse_html_body($html_body=null)
{
$passed="";
$image_count=0;
$this->temp_body_images=array();
while (eregi("\<img([^>]+)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]=="'" or $image_tag[0]=='"')
$image_tag=substr($image_tag,1);
if (substr($image_tag,strlen($imgage_tag)-1,1)=="'" or substr($image_tag,strlen($imgage_tag)-1,1)=='"')
$image_tag=substr($image_tag,0,strlen($imgage_tag)-1);
//echo $image_tag."
";
$cid=md5(uniqid(rand()));
$cid=substr($cid,0,15)."@unigenius.com";
$passed.="<img ".$image_att."src=\"cid:".$cid."\"";
$end_pos=@strpos($html_body,'>');
$passed.=substr($html_body,0,$end_pos);
$html_body=substr($html_body,$end_pos);
// °ÑͼƬÊý¾Ý¶Á³oÀ´±£´aeµ½Ò»¸oÊý¾Ý£»

$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($image_tag,strrpos($image_tag,'.')+1,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;
default:
$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;
return true;

}

function build_content($bound_level=0,$text_body,$html_body,$hava_att=false)
{
if ($html_body)
{
if (eregi("\<img[[:space:]]+src[[:space:]]=[[:space:]]*([^ ]+)",$html_body,$reg))
{
$bound_level++;
if ($text_body)
{
$this->body.="Content-Type: multipart/related;
type=\"multipart/alternative\";
\tboundary=\"";
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
}
else
{
$this->body.="Content-Type: multipart/related;
\tboundary=\"";
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";

}// ¶ÔÓÚÊÇñ text ÕýÎÄ ¡¢ htmlÕýÎÄ ÓÐûÓУ¬ÐeÓв»Í¬µÄ MIME Í
if (!$hava_att) $this->body.="This is a multi-part message in MIME format.\r\n\r\n";
// ÕýÎıeʶ£¬Èç¹uÊÇÒѾ­Óи½¼þµÄ±aÂe£¬ÔoÔÚÕýÎÄ Öв»ÐeÒªÕaÒ»¾a
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->parse_html_body($html_body);
if ($text_body)
{
$this->body.="Content-Type: multipart/alternative;
\tboundary=\"";
$bound_level++;
$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/plain;\r\n";
$this->body.="\tcharset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/html;\r\n";
$this->body.="\tcharset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
$bound_level--;
}
else
{
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/html;\r\n";
$this->body.="\tcharset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
}//ÕýÎıaÂe£¬ÓлoûÓÐtext ²¿*Ö£¬±a³É²»Í¬µÄ¸ñʽ¡£
for ($i=0;$i<count($this->temp_body_images);$i++)
{
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type:".$this->temp_body_images[$i][type].";
name=\"";
$this->body.=$this->temp_body_images[$i][name]."\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="Content-ID: <".$this->temp_body_images[$i][cid].">\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_body_images[$i][data])))."\r\n";
}
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
$bound_level--;
}
else // ÓлoûÓÐͼƬ£¬ÒÔÉÏÊÇÓÐͼƬµÄ´¦Ài£¬ÏÂÃaeÊÇûÓÐͼƬµÄ´¦Ài
{
$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."\"\r\n\r\n";

if (!$hava_att) $this->body.="\r\nThis is a multi-part message in MIME format.\r\n\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/plain;\r\n";
$this->body.="\tcharset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->body.="Content-Type: text/html;\r\n";
$this->body.="\tcharset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
$bound_level--;
}
else
{
$this->body.="Content-Type: text/html;\r\n";
$this->body.="\tcharset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
}//ÕýÎıaÂe£¬ÓлoûÓÐtext ²¿*Ö£¬±a³É²»Í¬µÄ¸ñʽ¡£

}// end else
}
else // Èç¹uûÓÐhtml ÕýÎÄ£¬Ö»ÓÐtext ÕýÎÄ
{
$this->body.="Content-Type: text/plain;
\tcharset=\"$this->charset\"\r\n";
$this->body.="Content-Transfer-Encoding: base64\r\n";
$this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
}
}// end function default

Function Buildbody($text_body=null,$html_body=null,$att=null)
{
$this->body="MIME-Version: 1.0\r\n";
if (null==$att or (@count($att)==0)) //Èç¹uûÓи½¼þ£¬²e¿´ÕýÎĵÄÀaÐÍ£»
{
$encode_level=0;
$this->build_content($encode_level,$text_body,$html_body);
}// Èç¹uûÓи½¼þ£¬
// ****
else //Èç¹uÓи½¼þ£¬
{
$bound_level=0;
$this->body.="Content-Type: multipart/mixed;
\tboundary=\"";
$bound_level++;

$this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
$this->body.="This is a multi-part message in MIME format.\r\n\r\n";
$this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
$this->build_content($bound_level,$text_body,$html_body,true);// ±aÈeÕýÎIJ¿*Ö

$num=count($att);
for ($i=0;$i<$num;$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."\r\n";
$this->body.="Content-Type: $file_type;\r\n\tname=\"$file_name\"\r\nContent-Transfer-Encoding: base64\r\n";
$this->body.="Content-Disposition: attachment;\r\n\tfilename=\"$file_name\"\r\n\r\n";
$this->body.=$file_data."\r\n";
}
}//end for

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

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

return true;
}

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

if (empty($from_mail) or empty($to_mail))
{
$this->err_str="ûÓÐÖ¸¶¨ÕýȵÄÓʼþµØÖ:*¢ËÍÈË£º".$from_mail."½ÓÊÕÈË£º".$to_mail;
return false;
}

if (gettype($to_mail)!="array")
$to_mail=split(",",$to_mail);//Èç¹u²»ÊÇÊý×e£¬×ª»»³ÉÊý×e£¬ÄÄÅÂÖ»ÓÐÒ»¸o¢ËͶÔÏo;
if (gettype($to_name)!="array")
$to_name=split(",",$to_name);//Èç¹u²»ÊÇÊý×e£¬×ª»»³ÉÊý×e£¬ÄÄÅÂÖ»ÓÐÒ»¸o
¢ËͶÔÏo;

$this->Buildbody($text_body,$html_body,$att);
// ËuÓÐÐżþµÄÄÚÈÝÊÇÒ»ÑuµÄ£¬¿ÉÒÔÖ»±aÒ»´Î£¬¶ø¶ÔÓÚ²»Í¬µÄÊÕÐÅÈË£¬ÐeÒª²»Í¬µÄHead

if (!$this->open()) return false;
if (!$this->command("HELO $this->hostname",3,"250")) return false;
// Óe*þÎñÆ÷½¨Á¢Á´½Ó
if (!$this->open()) return false;
if (!$this->command("HELO $this->hostname",3,"250")) return false;

for ($i=0;$i<count($to_mail);$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;
// ×¼±¸¢ËÍÓʼþ
if ($this->debug) $this->outdebug("sending subject;");
if (!fputs($this->connection,$this->subject)) {$this->err_str="
¢ËÍÓʼþÍʱ³o´i£¡";return false;}
if ($this->debug) $this->outdebug("sending body;");
if (!fputs($this->connection,$this->body)) {$this->err_str="
¢ËÍÕýÎÄʱ³o´i£¡";return false;}
if (!fputs($this->connection,".\r\n")) {$this->err_str="¢ËÍÕýÎÄʱ³o´i£¡";return false;}//ÕýÎÄ¢ËÍÍe±Ï£¬Í˳o£»
$resp=fgets($this->connection,256);
if($this->debug)
$this->outdebug("$resp");
if (substr($resp,0,1)!="2")
{
$this->err_str="¢ËÍÍeºo£¬þÎñÆ÷ûÓÐÏiÓ¦£¡£¡";
return false;
}
// *¢ËÍÓʼþ
}
if (!$this->command("QUIT",3,"221")) return false;
$this->close();
return true;
}

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

PHP SMTPÀaʹÓý¨
<?
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." Ö÷Ìa:".$subject;

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

*µ»ØÎļþµÄMIMEÀaÐ͵ĺ¯ÊýguessMIMEType()
function guessMIMEType($filename) {
//GUESS MIME TYPE
$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;
}
}

¼i²aEmailµØÖ*µÄº¯ÊýIsValidEmail()
function IsValidEmail($Email) {
if(!ereg("^[[:alnum:]\\.]+@([[:alnum:]\]+\.)+[[:alnum:]][[:alnum:]][[:alnum:]]?$",$Email, $regs)){
$IsValidEmail = false;
}
else
{
$IsValidEmail = true;
}
return $IsValidEmail;
}

Ò»¸oMIME MAILÀa£¬Çe´o¼Ò×¢ÒaÈçºÎ¹¹ÔiÒ»¸oÓʼþÌa
<?
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
    */
    function build_message($part)
    {
    $message = $part["message"];
    $message = chunk_split(base64_encode($message));
    $encoding = "base64";
    return "Content-Type: ".$part["ctype"].
    ($part["name"]?";\n\tname=\"".$part["name"]."\"" : "").
    "\nContent-Transfer-Encoding: $encoding".
    "\nContent-Disposition: attachment".
    ($part["name"]?";\n\tfilename=\"".$part["name"]."\"" : "").
    "\n\n$message\n";
    }

/*

  • void build_multipart()
  • Build a multipart mail
    */
    function build_multipart()
    {
    $boundary = "b".md5(uniqid(time()));
    $multipart = "Content-Type: multipart/mixed;\n\tboundary=\"$boundary\"\n\nThis is a MIME encoded message.\n\n--$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.0\n".$this->build_multipart();

$fp = fopen("mail.txt","w");
fputs($fp,$addtionheaders);
fclose($fp);
mail($this->to, $this->subject, "", $addtionheaders);
}
}; // end of class

/*

  • Example usage

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

$mail = new mime_mail();
$mail->from = "[email]foo@bar.com[/email]";
$mail->headers = "Errors-To: [email]foo@bar.com[/email]";
$mail->to = "[email]bar@foo.com[/email]";
$mail->subject = "Testing...";
$mail->body = "This is just a test.";
$mail->add_attachment("$attachment", "test.jpg", "image/jpeg");
$mail->send();

*/
?>

myimapÀa£¬¶ÁÈ¡Óʼþ
<?php
//////////////////////////////////////////////
//Origin by Alpha.Z (5/21/2000)
//Modified By Belltree <[email]belltree@163.com[/email]> (11/01/2000)
///////////////////////////////////////////////
class 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;
var $num_msg_parts = 0;
var $attach;
var $num_of_attach = 0;

function open()
{
if ($this->port==110)
$this->inStream=imap_open("{".$this->hostname."/pop3: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 µÄÐÅÏaÁ¬½Ó³É¹¦¡£
";
return $inStream;
}
else
{
echo "Óû§£º$this->username µÄÐÅÏaÁ¬½Óʧ°Ü¡£
";
return 0;
}
}

Function close()
{
if(imap_close($this->inStream))
{
//echo "


ÒѾ­ÓeþÎñÆ÷ $this->hostname ¶Ï¿ªÁ¬½Ó¡£";
return 1;
}
else
{
//echo "
Óe
þÎñÆ÷ $this->hostname ¶Ï¿ªÁ¬½Óʧ°Ü¡£";
return 0;
}
}

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

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

function CheckMailbox($page_size,$page)
{
$mboxinfo=@imap_mailboxmsginfo($this->inStream);
//$mboxinfo=imap_check($this->inStream);
if ($mboxinfo)
if ($mboxinfo->Nmsgs>0)
{
//echo "ÄuÓÊÏa£º".$mboxinfo->Mailbox."
";
echo $this->username."@wells.comµÄÊÕ¼þÏaÀi¹²ÓÐÓʼþÊý£º".$mboxinfo->Nmsgs."
\n";
echo "δ¶ÁÓʼþÊý£º".$mboxinfo->Unread."";
echo "ÐÂÓʼþÊý£º".$mboxinfo->Recent." ";
echo "×ܹ²Õ¼Óÿռa£º".$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 "ÄuµÄÐÅÏaÀiûÓÐÓʼþ¡£


\n";
}
else
{
echo '´iÎo£ºÎÞ¨»ñÈ¡ÊÕ¼þÏaµÄÐÅÏ¢¡£';
return 0;
}
echo "<table border=1 width=100% cellpadding=2 cellspacing=0 bordercolorlight=#000080 bordercolordark=#ffffff style=\"font:9pt Tahoma,ËÎÌa\">\n";
echo "״̬
¢¼þÈËÖ÷Ìaʱ¼a´oС\n";
$sortby="SORTDATE";
$sort_reverse=1;
$sorted = imap_sort($this->inStream, $sortby, $sort_reverse, SE_UID);

for ($i=0;$i<$mboxinfo->Nmsgs;$i++)
{
if (($i>=$page$page_size) and ($i<$page$page_size+$page_size)){
$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);

$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->personal))
{
$frm = trim($this->decode_mime_string($from->personal));
if (isset($from->mailbox) && isset($from->host))
{
$frm_add = $from->mailbox . '@' . $from->host;
}
}
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 ($sub == "")
$sub = "ÎÞÖ÷Ìa";
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 "$newmail<td align=\"center\">\n";
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) < 0)
echo "\n";
else
echo "\n";

if (($next_page = $page + 1) >= $last_page)
echo "

\n";
else
echo "\n";
$last_page --;
if ( $last_page < $next_page)
echo "\n";
else
echo "\n";
echo "
µÚÒ»Ò³<a href=\"mail_list.php?page=0\">µÚһҳǰһҳ<a href=\"mail_list.php?page=$prev_page\">Ç°Ò»Ò³ ºoÒ»Ò³<a href=\"mail_list.php?page=$next_page\">ºoÒ»Ò³×iÄ(C)Ò³<a href=\"mail_list.php?page=$last_page\">×iÄ(C)Ò³
\n";
}

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 = strreplace('', '%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 . $this->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")
{
$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;
}
}

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

$att_path = $this->username."\\".$att_name;

$this->attach[$this->num_of_attach]=$att_name; //µÇ¼Ç¸½¼þÁбi
$this->num_of_attach ++; //µÇ¼Ç¸½¼þÊýÁ¿
/
$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->username))
mkdir($this->username,0700);
$fp=fopen($att_path,"w");
switch ($msg_part->encoding)
{
case 3: // base64
fputs($fp,imap_base64(imap_fetchbody($this->inStream,$msg_num,$part_no)));
break;
case 4: // QP
fputs($fp,imap_qprint(imap_fetchbody($this->inStream,$msg_num,$part_no)));
break;
default:
fputs($fp,imap_fetchbody($this->inStream,$msg_num,$part_no));
break;
}
fclose($fp);

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


\n";
// echo "<img src=\"$att_path\" align=center>

\n";
//}
} // END IF ATTACHMENT
else //NOT ifdisposition
{
// I guess it is used for something besides attachments????
}
}
else
{
// Not an attachment, lets see what this part is...
switch ($msg_part->type)
{
case 0:
$mime_type = "text";
break;
case 1:
$mime_type = "multipart";
// Hey, why not use this function to deal with all the parts
// of this multipart part
$this->num_msg_parts = count($msg_part->parts);
for ($i = 0; $i < $this->num_msg_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 2:
$mime_type = "message";
break;
case 3:
$mime_type = "application";
break;
case 4:
$mime_type = "audio";
break;
case 5:
$mime_type = "image";
break;
case 6:
$mime_type = "video";
break;
case 7:
$mime_type = "model";
break;
default:
$mime_type = "unknown";
// hmmm....
}

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

// 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 0:
case 1:
if ($this->num_msg_parts == 0){
echo ereg_replace("\r\n","
\r\n",imap_body($this->inStream,$msg_num));

}
else{
if ($part_no!=""){
echo ereg_replace("\r\n","
\r\n",imap_fetchbody($this->inStream,$msg_num,$part_no));

}
}
break;
case 3: //BASE64

if ($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->username . "\\Îı¾ÄÚÈÝ.txt";
$fp = fopen($att_path,"w");
fputs($fp,$content);
fclose($fp);
$this->attach[$this->num_of_attach]="Îı¾ÄÚÈÝ.txt";
$this->num_of_attach++;
}

echo $content;

}

if ($full_mime_type=="text/html"){
$att_path = $this->username . "\\³¬Îı¾ÄÚÈÝ.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]="³¬Îı¾ÄÚÈÝ.htm";
$this->num_of_attach++;
}
break;
case 4: //QPRINT
// use imap_qprint to decode
if ($this->num_msg_parts == 0){
echo ereg_replace("\n","
",imap_qprint(imap_body($this->inStream,$msg_num)));

}
else{
echo ereg_replace("\n","
",imap_qprint(imap_fetchbody($this->inStream,$msg_num,$part_no)));

}
if ($full_mime_type=="text/html"){
$att_path = $this->username . "\\qp³¬Îı¾ÄÚÈÝ.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³¬Îı¾ÄÚÈÝ.htm";
$this->num_of_attach++;
}
break;
case 5:
// not sure if this needs decoding at all
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µÄÀ(C)Õ¹Àa£¬¼°¶ÁÈ¡Ò»*aÓʼþµÄÀý×Ó(mail_read.php)
<?
require("myimap.php");

class myimap_ext extends myimap {

function get_mail_subject($msg_no){
$msgHeader = @imap_header($this->inStream, $msg_no);
if (isset($msgHeader->subject))
$sub = trim($this->decode_mime_string($msgHeader->subject));
if ($sub == "")
$sub = "ÎÞÖ÷Ìa";
return "Fw:".$sub;
}
function print_attaches(){
for ($i=0;$i<count($this->attach);$i++){
echo "<a target=_blank href=\"".$this->username."\\".$this->attach[$i]."\">".$this->attach[$i]."
";
}
}
function list_attaches(){
for ($i=0;$i<count($this->attach);$i++){
if ($i==0)
$attaches = $this->attach[$i];
else
$attaches .= ";".$this->attach[$i];
}
return $attaches;
}
}

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

$imap->open();

$mail_structure=$imap->get_structure($msg);
$imap->proc_structure($mail_structure,"",$msg);
if ($imap->num_of_attach > 0){
$imap->print_attaches();
}
@$imap->close();
?>

myimapµÄÀý×Ó(mail_list.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();
?>

ÈçºÎ¢Ë͸½¼þ£¨×ª£(C)
ÎÒ¾­³£Ìýµ½ÕaÑuÒ»¸oÎÊÌa£º"ÎÒÓÐÒ»¸o´ÓÍøÕ¾
¢À´µÄºÏͬ¡£ÎÒÈçºÎ¸øͨ¹ý±iµ¥¢Ë͵ĵç×ÓÓʼþÔo¼ÓÒ»¸o¸½¼þÄØ£¿"
Ê×ÏÈÎÒҪ˵µÄÊÇÒª×oµ½Õa¸oûÓÐʲô¼oµ¥µÄ°i
¨¡£ÄaÒªºÜºÃµÄÀi½aPHP»o
ÆaËuµÄþÎñÆ÷¶ËµÄ½Å±¾ÓiÑÔ¡£µ±È»Äa»¹ÒªÒ»¸oÕaeÕýÖ§³ÖPHPµÄÍøÕ¾µÄÕ˺š£Èç¹uÂu×aÁËÕa¸oÇ°Ìa£¬ÔÚÄa¶ÁÍeÁ˱¾Õºo¾Í¿ÉÒÔÓÃPHP¢ËÍ´ø¸½¼þµÄµç×ÓÓʼþÁË¡£

1. ¸½¼þÊÇÈçºÎ¹¤×÷µÄ

Èç¹uÄaÔø¾­ÔÚPHPµÄÊÖ²aÖÐËÑË÷¹ý"¸½¼þ"º¯Êý£¬ÄÇô½a¹u¿ÉÄÜÊÇʲô¶¼Ã»ÓУ¨ÖÁÉÙÔÚÎÒд±¾ÎĵÄʱ¼a»¹Ã»ÓУ(C)¡£ºoÀ´Äa¾ÍÒª»¨ºÜ¶aʱ¼aÀ´Á˽aÕa*½ÃaeµÄ֪ʶ¡£

ÄaÒ²Ði»aÏeµ±Äa¸øij¸oÈË¢ËÍÒ»a´ø¸½¼þµÄµç×ÓÓʼþʱ£¬¸½¼þÊǺÍÓʼþÒ»Æðŵ½ÊÕ¼þÈ˵ÄÐÅÏaÀiµÄ£¨±ÈÈ磬Èç¹uÄa¸øËu/Ëý¢ÁËÒ»¸oPNGµÄͼƬÎļþ£¬Ëu/ËýµÄÐÅÏaÀi»a°uº¬Ò»¸otxtÎļþ£¨µç×ÓÓʼþ£(C)ºÍÒ»¸o.pngÎļþ£¨¸½¼þ£(C)£(C)¡£µ«Õa²»ÊÇËuµÄ¹¤×÷Ô­Ài¡£µ±Äa¼ÓÈeÒ»¸o¸½¼þʱ£¬ÄaµÄÓʼþ³ÌÐo°Ñ¸½¼þת»»³É´¿Îı¾Îļþ£¬²¢ÔÚÄaдµÄÄÚÈÝ£¨Êµ¼ÊµÄµç×ÓÓʼþ£(C)ºoÃae²aÈeÕa¸oÎı¾¿e¡£Õa¸o£¬µ±Äa°ÑËuÓеĶ«Î÷*¢³oÀ´ºo£¬ÊÕ¼þÈ˵ÄÐÅÏaÀiÖ»ÓÐÒ»¸o´¿Îı¾Îļþ--Ò»¸oͬʱ°uº¬¸½¼þºÍʵ¼Êµç×ÓÓʼþÄÚÈݵÄÎļþ¡£

ÏÂÃaeÊÇÒ»¸o´ø¸½¼þ£¨Ò»¸oHTMLÎļþ£(C)µç×ÓÓʼþµÄÀý×Ó¡£ÎÒÒѾ­±e×¢ÁËÆaÖÐÖØÒªµÄ¼¸ÐУº

Return-Path: <[email]someone@example.com[/email]>
Date: Mon, 22 May 2000 19:17:29 +0000
From: Someone <[email]someone@example.com[/email]>
To: Person <[email]person@eksempel.dk[/email]>
Message-id: <[email]83729KI93LI9214@example.com[/email]>
Content-type: multipart/mixed; boundary="396d983d6b89a"
Subject: Here's the subject

--396d983d6b89a
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 8bit

This is the body of the email.

--396d983d6b89a
Content-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--

Ç°ÃaeµÄ7ÐÐÊÇÓʼþµÄÍ£¬ÆaÖÐÖµµÃ×¢ÒaµÄÊÇContent-typeͲ¿Ö¡£Õa¸o͸aeËßÓʼþ³ÌÐoµç×ÓÓʼþÊÇÓÉÒ»¸oÒÔÉϵIJ¿Ö×e³ÉµÄ¡£²»º¬¸½¼þµÄÓʼþÖ»ÓÐÒ»¸o²¿Ö£ºÏuÏ¢±¾Éi¡£´ø¸½¼þµÄµç×Óͨ³£ÖÁÉÙÓÉÁ½²¿Ö×e³É£ºÏuÏ¢ºÍ¸½¼þ¡£ÕaÑu£¬´øÁ½¸o¸½¼þµÄÓʼþÓÉÈý²¿Ö×e³É£ºÏuÏ¢£¬µÚÒ»¸o¸½¼þºÍµÚ¶þ¸o¸½¼þ¡£

´ø¸½¼þµÄµç×ÓÓʼþµÄ²»Í¬²¿ÖÖ®¼aÓÃÖ½çÏßÀ´Ö¸o¡£Ö½çÏßÔÚContent-typeÍÖж¨Òa¡£ÓʼþµÄÿ¸oв¿ÖÒÔÁ½¸oÁ¬×ֺţ¨--£(C)ºÍÖ½çÏß¿ªÊ¼¡£×iºoÒ»¸oÖ½çÏߺoÒ²ÓÐÁ½¸oÁ¬×ֺţ¬±iʾÕa¸oÓʼþÖÐûÓÐÆaËuµÄ²¿*ÖÁË¡£

ÔÚÿ¸oÖ½çÏߺoÓÐÒ»Ð(C)ÐУ¬ÓÃÀ´¸aeËßÓʼþ³ÌÐoÕa¸o²¿ÖµÄÄÚÈݵÄÀaÐÍ¡£±ÈÈ磬¿´¿´ÉÏÃaeÀý×ÓÖеÚÒ»¸oÖ½çÏߺoÃaeµÄÁ½ÐÐ--ÒÔContent-type: text/plain¿ªÍµÄÐС£ÕaÐ(C)ÐÐ˵Ã÷ºoÃaeµÄ²¿ÖÊÇISO-8859-1×Öu¼¯µÄ´¿Îı¾¡£¸uÔÚµÚ¶þ¸oÖ½çÏߺoµÄÐиaeËßÓʼþ³ÌÐoÏÖÔڵIJ¿ÖÊÇÒ»¸oHTMLÎļþ£¬ËuµÄÃu×ÖÊÇ"attachment.html"¡£

Content-dispositionÕa³Ö¸aeËßÓʼþ³ÌÐoÈç¹u¿ÉÄܾÍÒÔÄÚǶµÄ½Ê½ÏÔʾ¸½¼þ¡£ÏÖÔÚеÄÓʼþ³ÌÐo»aÔÚÏuÏ¢ºoÏÔʾHTMLµÄÄÚÈÝ¡£Èç¹uContent-disposition±»ÉeΪattachment£¬ÄÇôÓʼþ³ÌÐo¾Í²»»aÏÔʾHTMLÎļþµÄÄÚÈÝ£¬¶øÊÇÏÔʾһ¸oÁ¬½Óµ½ÎļþµÄͼ±e£¨»oÆaËuµÄÀaËƵĶ«Î÷£(C)¡£ÊÕ¼þÈËÒª¿´¸½¼þµÄÄÚÈÝ£¬±ØÐeµa»÷Õa¸oͼ±e¡£Ò»°aÇe¿oÏ£¬Èç¹u¸½¼þÊÇÒ»Ð(C)Îı¾£¨°uº¬HTML£(C)£¬Content-disposition»a±»ÉeΪinline£¬ÕaÊÇÒoΪÏÖÔÚ´o²¿ÖÓʼþ³ÌÐoÄܹ»²»½eÖuÆaËua¯ÀÀÆ÷¶øÖ±½ÓÏÔʾ¸½¼þ£¨Îı¾£(C)µÄÄÚÈÝ¡£Èç¹u¸½¼þ²»ÊÇÎı¾£¨±ÈÈçͼƬ»oÆaËuÀaËƵÄÄÚÈÝ£(C)£¬Content-disposition¾ÍÉeΪattachment¡£

2. ÓÃPHPÉu³É´ø¸½¼þµÄµç×ÓÓʼþ

ÕaÀiÒ»¸oÀý×Ó£¬¸aeËßÄaÈç¹u°ÑÒ»¸o¶¨ÒaºÃµÄHTMLÎļþ¼ÓΪÓʼþµÄ¸½¼þ£º

<?php

ÎÒÃÇÊ×ÏÈдʵ¼ÊµÄÏuÏ¢ÄÚÈÝ

$emailBody = "This is text that goes into the body of the email.";

È»ºoÎÒÃÇÒª×÷Ϊ¸½¼þµÄHTMLÎļþ

$attachment = "

The attached file

This is the attached HTML file

";

½¨Á¢ÔÚÓʼþÖÐÖ¸o²»Í¬²¿ÖµÄ*Ö½çÏß¡£

»u±¾ÉÏ£¬Ö½çÏß¿ÉÒÔÊÇÈÎÒaµÄ×Öu´®¡£

µ«ÊÇÖØÒªµÄÒ»µaÊÇÈ*¶¨Ò»¸oдÓʼþµÄÈË

Õa»aËaeÒaд³oµÄ×Ö*u´®£¬ËuÒÔÎÒÃÇÓÃ

uniqidº¯ÊýÀ´²uÉuÒ»¸oËae»uµÄ×Ö*u´®¡£

$boundary = uniqid( "");

ÏÖÔÚÎÒÃÇÒª½¨Á¢ÓʼþÍ*¡£²»ÒªÍuÁ˲aÈe

Content-typeÍ*À´ËµÃ÷Õa¸oÓʼþ°uº¬Ò»¸o»o¸u¶aµÄ¸½¼þ¡£

$headers = "From: [email]someone@example.com[/email]
Content-type: multipart/mixed; boundary=\"$boundary\"";

ºÃ£¬ÏÖÔÚÎÒÃÇÒѾ­ÓÐÁËÓʼþµÄËuÓÐÄÚÈÝ¡£

ÏÂÒ»¼þÊÂÊÇÐÞ¸ÄÓʼþµÄÖ÷Ìa¡£

$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--";

ÏÖÔÚ¿ÉÒÔ°ÑÓʼþ*¢³oÈ¥ÁË

mail( "[email]person@eksempel.dk[/email]", "The subject", $emailBody, $headers);
?>

3. °ÑÓû§ÉÏ´«µÄÎļþ×÷Ϊ¸½¼þ

ÄaÒ²Ði»a¾oµÃÉÏÃaeµÄÀý×ÓÄÑÒÔÀi½a£¬µ«ÏÂÃae...¡£ÔÚÏÂÃaeµÄÀý×ÓÖÐÊÂÇe¸uÄÑÁË£¬ÒoΪÎÒÃÇÒªÓÃÒ»¸o±iµ¥ÈÃÓû§ÉÏ´«ËuÃǵÄÎļþ£¬²¢°ÑÕa¸oÎļþ×÷ΪÎÒÃÇÒª¢µÄÓʼþµÄ¸½¼þ¡£Âe³µÄÊÇÎÒÃDz»ÄÜÔ¤ÏÈÖªµÀÎļþµÄMIMEÀaÐÍ¡£ÔÚÇ°ÃaeµÄÀý×ÓÖУ¬ÎÒÃÇÒѾ­ÖªµÀ¸ÃËuÊÇÒ»¸oHTMLÎļþ£¬ËuÒÔ¸øÕa¸o¸½¼þÉeÖÃContent-typeÍ*ÊǺܼoµ¥µÄ¡£ÔÚÏÂÃaeµÄÀý×ÓÖУ¬MIMEÀaÐÍ¿ÉÄÜÊÇÈÎÒaµÄ£¬ÒoΪÓû§¿ÉÄÜ»aÉÏ´«Ò»¸oHTMLÎļþ£¬Ò»¸oPNGÎļþ£¬Ò»¸ovCardÎļþ£¬»oÕßÆaËuµÄ¶«Î÷¡£ÈÃÎÒÃÇÀ´¿´¿´Àý×Ó£º

<?php

ÏÖÔÚÎÒÃÇÀ´Éu³É±iµ¥¡£ÔÚ²uÉu¿ÉÒÔÉÏ´«ÎļþµÄ±iµ¥Ê±£¬

²»ÒªÍuÁË°Ñ
±eÇ(C)µÄ"enctype"ÊoÐÔÉeΪ"multipart/form-data".

echo "\n";
echo "
\n";
echo "
\n";
echo "
\n";
echo "
\n";
echo "
\n";
echo "\n";
echo "\n";

Èç¹uÓû§ÒѾ­°´ÁË"Send"°´Å¥"

if ($send) {

¶¨Òa*Ö½çÏß

$boundary = uniqid( "");

Éu³ÉÓʼþÍ*

$headers = "From: $from
Content-type: multipart/mixed; boundary=\"$boundary\"";

È*¶¨ÉÏ´«ÎļþµÄMIMEÀaÐÍ

if ($attachment_type) $mimeType = $attachment_type;

Èç¹ua¯ÀÀÆ÷ûÓÐÖ¸¶¨ÎļþµÄMIMEÀaÐÍ£¬

ÎÒÃÇ¿ÉÒÔ°ÑËuÉeΪ"application/unknown".

else $mimeType = "application/unknown";

È*¶¨ÎļþµÄÃu×Ö

$fileName = $attachment_name;

´o¿ªÎļþ

$fp = fopen($attachment, "r");

°ÑÕu¸oÎļþ¶ÁÈeÒ»¸o±aÁ¿

$read = fread($fp, filesize($attachment));

ºÃ£¬ÏÖÔÚ±aÁ¿$readÖб£´aeµÄÊÇ°uº¬Õu¸oÎļþÄÚÈݵÄÎı¾¿e¡£

ÏÖÔÚÎÒÃÇÒª°ÑÕa¸oÎı¾¿eת»»³ÉÓʼþ³ÌÐo¿ÉÒÔ¶Á¶®µÄ¸ñʽ

ÎÒÃÇÓÃbase64½¨°ÑËu±aÂe

$read = base64_encode($read);

ÏÖÔÚÎÒÃÇÓÐÒ»¸oÓÃbase64½¨±aÂeµÄ³¤×Ö*u´®¡£

ÏÂÒ»¼þÊÂÊÇÒª°ÑÕa¸o³¤×Öu´®ÇгÉÓÉÿÐÐ76¸o×Öu×e³ÉµÄС¿e

$read = chunk_split($read);

ÏÖÔÚÎÒÃÇ¿ÉÒÔ½¨Á¢ÓʼþµÄÖ÷Ìa

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

Õa¾ÍÊÇÈ«²¿ÄÚÈÝ¡£Èç¹uÄa²»ÄܺܺõØÀi½aÉÏÃaeµÄÀý×Ó£¬ÎҵĽ¨ÒeÊǸøÄa×Ô¼º*¢Ëͼ¸¸o´ø¸½¼þµÄÓʼþ£¬È»ºo×ÐϸÑо¿ÓʼþµÄÔ´´uÂe¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
贪吃蛇
文件下载
获取IMEI
双位运算符
发送邮件
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
提取后缀名
年的日历图
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例