PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/!PHP/???/smtp_old.php

http://mootoolstools.googlecode.com/
PHP | 338 lines | 305 code | 20 blank | 13 comment | 61 complexity | 35856c1bfebb26e940e8a86cc96032a6 MD5 | raw file
  1. <?
  2. /***********************************
  3. PHP MIME?SMTP ver 1.0 Powered by Boss_ch? Unigenius soft ware co. Ltd
  4. All rights reserved, Copyright 2000 ;
  5. ????????????Bugs Report : boss_ch@china.com
  6. *************************************/
  7. if(!isset($__smtp_class__)){
  8. $__smtp_class__=1;
  9. class smtp {
  10. var $hostname="";
  11. var $auth=true;
  12. var $user="";
  13. var $passwd="";
  14. var $port=25;
  15. var $connection=0;
  16. var $debug=1;
  17. var $timeout=30;
  18. var $err_str;
  19. var $err_no;
  20. var $autocode=true;
  21. var $charset="gb2312";
  22. var $subject="";
  23. var $body="";
  24. var $attach="";
  25. var $temp_text_body;
  26. var $temp_html_body;
  27. var $temp_body_images;
  28. var $bound_begin="";
  29. var $bound_end="";
  30. Function smtp($server="smtp.126.com",$auth,$authuser,$authpasswd,$port=25,$time_out=20) {
  31. $this->hostname=$server;
  32. $this->auth = $auth;
  33. $this->user = $authuser;
  34. $this->passwd = $suthpasswd;
  35. $this->port=$port;
  36. $this->timeout=$time_out;
  37. return true;
  38. }
  39. Function outdebug($message) echo htmlspecialchars($message)."<div></div>\n";
  40. function command($command,$return_lenth=1,$return_code='2') {
  41. if ($this->connection==0) {
  42. $this->err_str="??????????????????";
  43. return false;
  44. }
  45. if ($this->debug) $this->outdebug(">>> $command");
  46. if (!fputs($this->connection,"$command \r\n")) {
  47. $this->err_str="??????".$command;
  48. return false;
  49. } else {
  50. $resp=fgets($this->connection,256);
  51. if($this->debug) $this->outdebug("$resp");
  52. if (substr($resp,0,$return_lenth)!=$return_code) {
  53. $this->err_str=$command." ?????????:".$resp;
  54. return false;
  55. } else return true;
  56. }
  57. }
  58. Function open() {
  59. if($this->hostname=="") {
  60. $this->err_str="??????!!";
  61. return false;
  62. }
  63. if ($this->debug) echo "$this->hostname,$this->port,&$err_no, &$err_str, $this->timeout<div></div>";
  64. if (!$this->connection=fsockopen($this->hostname,$this->port,&$err_no, &$err_str, $this->timeout)) {
  65. $this->err_str="????SMTP ?????,?????".$err_str."????".$err_no;
  66. return false;
  67. } else {
  68. $resp=fgets($this->connection,256);
  69. if($this->debug) $this->outdebug("$resp");
  70. if (substr($resp,0,1)!="2") {
  71. $this->err_str="???????????".$resp."???SMTP???????";
  72. return false;
  73. }
  74. return true;
  75. }
  76. }
  77. Function Close() {
  78. if($this->connection!=0) {
  79. fclose($this->connection);
  80. $this->connection=0;
  81. }
  82. }
  83. Function Buildhead($from_name,$to_name,$from_mail,$to_mail,$subject) {
  84. if (empty($from_name)) $from_name=$from_mail;
  85. if (empty($to_name)) $to_name=$to_mail;
  86. $this->subject="From: =".$this->charset.base64_encode($from_name)."=<".$from_mail.">\r\n";
  87. $this->subject.="To: =".$this->charset.base64_encode($to_name)."=<".$to_mail.">\r\n";
  88. $subject=ereg_replace("\n","",$subject);
  89. $this->subject.="Subject: =".$this->charset.base64_encode($subject)."?=\r\n";
  90. if ($this->debug) echo nl2br(htmlspecialchars($this->subject));
  91. return true;
  92. }
  93. Function parse_html_body($html_body=null) {
  94. $passed="";
  95. $image_count=0;
  96. $this->temp_body_images=array();
  97. while (eregi("\<*img([^\>]+)src[[:space:]]*=[[:space:]]*([^ ]+)",$html_body,$reg)) {
  98. $pos=@strpos($html_body,$reg[0]);
  99. $passed.=substr($html_body,0,$pos);
  100. $html_body=substr($html_body,$pos+strlen($reg[0]));
  101. $image_tag=$reg[2];
  102. $image_att=$reg[1];
  103. $tag_len=strlen($image_tag);
  104. if ($image_tag[0]=="'" or $image_tag[0]=='"') $image_tag=substr($image_tag,1);
  105. 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);
  106. $cid=md5(uniqid(rand()));
  107. $cid=substr($cid,0,15)."@unigenius.com";
  108. $passed.="<img ".$image_att."src=\"cid:".$cid."\"";
  109. $end_pos=@strpos($html_body,'>');
  110. $passed.=substr($html_body,0,$end_pos);
  111. $html_body=substr($html_body,$end_pos);
  112. // ????????????????
  113. $img_file_con=fopen($image_tag,"r");
  114. unset($image_data);
  115. while ($tem_buffer=AddSlashes(fread($img_file_con,16777216))) $image_data.=$tem_buffer;
  116. fclose($img_file_con);
  117. $image_exe_name=substr($image_tag,strrpos($image_tag,'.')+1,3);
  118. switch (strtolower($image_exe_name)) {
  119. case "jpg":
  120. case "jpeg":
  121. $content_type="image/jpeg";
  122. break;
  123. case "gif":
  124. $content_type="image/gif";
  125. break;
  126. case "png":
  127. $content_type="image/x-png";
  128. break;
  129. case "tif":
  130. $content_type="image/tif";
  131. break;
  132. default:
  133. $content_type="image/";
  134. break;
  135. }
  136. $this->temp_body_images[$image_count][name]=basename($image_tag);
  137. $this->temp_body_images[$image_count][type]=$content_type;
  138. $this->temp_body_images[$image_count][cid]=$cid;
  139. $this->temp_body_images[$image_count][data]=$image_data;
  140. $image_count++;
  141. }
  142. $this->temp_html_body=$passed.$html_body;
  143. return true;
  144. }
  145. function build_content($bound_level=0,$text_body,$html_body,$hava_att=false) {
  146. if ($html_body) {
  147. if (eregi("\<*img[[:space:]]+src[[:space:]]*=[[:space:]]*([^ ]+)",$html_body,$reg)) {
  148. $bound_level++;
  149. if ($text_body) {
  150. $this->body.="Content-Type: multipart/related; type=\"multipart/alternative\"; \tboundary=\"";
  151. $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
  152. } else {
  153. $this->body.="Content-Type: multipart/related; \tboundary=\"";
  154. $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
  155. }
  156. if (!$hava_att) $this->body.="This is a multi-part message in MIME format.\r\n\r\n";
  157. // ????????????????????? ???????
  158. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
  159. $this->parse_html_body($html_body);
  160. if ($text_body) {
  161. $this->body.="Content-Type: multipart/alternative; \tboundary=\"";
  162. $bound_level++;
  163. $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
  164. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
  165. $this->body.="Content-Type: text/plain;\r\n";
  166. $this->body.="\tcharset=\"$this->charset\"\r\n";
  167. $this->body.="Content-Transfer-Encoding: base64\r\n";
  168. $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
  169. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
  170. $this->body.="Content-Type: text/html;\r\n";
  171. $this->body.="\tcharset=\"$this->charset\"\r\n";
  172. $this->body.="Content-Transfer-Encoding: base64\r\n";
  173. $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
  174. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
  175. $bound_level--;
  176. } else {
  177. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
  178. $this->body.="Content-Type: text/html;\r\n";
  179. $this->body.="\tcharset=\"$this->charset\"\r\n";
  180. $this->body.="Content-Transfer-Encoding: base64\r\n";
  181. $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
  182. }//??????????text ???????????
  183. for ($i=0;$i<count($this->temp_body_images);$i++) {
  184. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
  185. $this->body.="Content-Type:".$this->temp_body_images[$i][type]."; name=\"";
  186. $this->body.=$this->temp_body_images[$i][name]."\"\r\n";
  187. $this->body.="Content-Transfer-Encoding: base64\r\n";
  188. $this->body.="Content-ID: <".$this->temp_body_images[$i][cid].">\r\n";
  189. $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_body_images[$i][data])))."\r\n";
  190. }
  191. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
  192. $bound_level--;
  193. } else {
  194. $this->temp_html_body=$html_body;
  195. if ($text_body) {
  196. $bound_level++;
  197. $this->body.="Content-Type: multipart/alternative; \tboundary=\"";
  198. $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
  199. if (!$hava_att) $this->body.="\r\nThis is a multi-part message in MIME format.\r\n\r\n";
  200. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
  201. $this->body.="Content-Type: text/plain;\r\n";
  202. $this->body.="\tcharset=\"$this->charset\"\r\n";
  203. $this->body.="Content-Transfer-Encoding: base64\r\n";
  204. $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
  205. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
  206. $this->body.="Content-Type: text/html;\r\n";
  207. $this->body.="\tcharset=\"$this->charset\"\r\n";
  208. $this->body.="Content-Transfer-Encoding: base64\r\n";
  209. $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
  210. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
  211. $bound_level--;
  212. } else {
  213. $this->body.="Content-Type: text/html;\r\n";
  214. $this->body.="\tcharset=\"$this->charset\"\r\n";
  215. $this->body.="Content-Transfer-Encoding: base64\r\n";
  216. $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($this->temp_html_body)))."\r\n";
  217. }//??????????text ???????????
  218. }// end else
  219. } else {
  220. $this->body.="Content-Type: text/plain; \tcharset=\"$this->charset\"\r\n";
  221. $this->body.="Content-Transfer-Encoding: base64\r\n";
  222. $this->body.="\r\n".chunk_split(base64_encode(StripSlashes($text_body)))."\r\n";
  223. }
  224. }
  225. Function Buildbody($text_body=null,$html_body=null,$att=null) {
  226. $this->body="MIME-Version: 1.0\r\n";
  227. if (null==$att or (@count($att)==0)) {
  228. $encode_level=0;
  229. $this->build_content($encode_level,$text_body,$html_body);
  230. } else {
  231. $bound_level=0;
  232. $this->body.="Content-Type: multipart/mixed; \tboundary=\"";
  233. $bound_level++;
  234. $this->body.=$this->bound_begin.$bound_level.$this->bound_end."\"\r\n\r\n";
  235. $this->body.="This is a multi-part message in MIME format.\r\n\r\n";
  236. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
  237. $this->build_content($bound_level,$text_body,$html_body,true);// ??????
  238. $num=count($att);
  239. for ($i=0;$i<$num;$i++) {
  240. $file_name=$att[$i][name];
  241. $file_source=$att[$i][source];
  242. $file_type=$att[$i][type];
  243. $file_size=$att[$i][size];
  244. if (file_exists($file_source)) {
  245. $file_data=addslashes(fread($fp=fopen($file_source,"r"), filesize($file_source)));
  246. $file_data=chunk_split(base64_encode(StripSlashes($file_data)));
  247. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."\r\n";
  248. $this->body.="Content-Type: $file_type;\r\n\tname=\"$file_name\"\r\nContent-Transfer-Encoding: base64\r\n";
  249. $this->body.="Content-Disposition: attachment;\r\n\tfilename=\"$file_name\"\r\n\r\n";
  250. $this->body.=$file_data."\r\n";
  251. }
  252. }
  253. $this->body.="--".$this->bound_begin.$bound_level.$this->bound_end."--\r\n\r\n";
  254. }
  255. if ($this->debug) echo nl2br(htmlspecialchars($this->body));
  256. return true;
  257. }
  258. function send($from_name,$to_name,$from_mail,$to_mail,$subject,$text_body=false,$html_body=false,$att=false) {
  259. if (empty($from_mail) or empty($to_mail)) {
  260. $this->err_str="???????????:????".$from_mail."????".$to_mail;
  261. return false;
  262. }
  263. if (gettype($to_mail)!="array") $to_mail=split(",",$to_mail);//???????????????????????;
  264. if (gettype($to_name)!="array") $to_name=split(",",$to_name);//???????????????????????;
  265. $this->Buildbody($text_body,$html_body,$att);
  266. // ???????????????????????????????????Head
  267. if (!$this->open()) return false;
  268. //if($this->auth)
  269. if (!$this->command("HELO $this->hostname",3,"250")) return false;
  270. // ????????
  271. if (!$this->open()) return false;
  272. if (!$this->command("HELO $this->hostname",3,"250")) return false;
  273. for ($i=0;$i<count($to_mail);$i++) {
  274. $this->Buildhead($from_name,$to_name[$i],$from_mail,$to_mail[$i],$subject);
  275. if (!$this->command("RSET",3,"250")) return false;
  276. if (!$this->command("MAIL FROM:".$from_mail,3,"250")) return false;
  277. if (!$this->command("RCPT TO:".$to_mail[$i],3,"250")) return false;
  278. if (!$this->command("DATA",3,"354")) return false;
  279. // ??????
  280. if ($this->debug) $this->outdebug("sending subject;");
  281. if (!fputs($this->connection,$this->subject)) {$this->err_str="?????????";return false;}
  282. if ($this->debug) $this->outdebug("sending body;");
  283. if (!fputs($this->connection,$this->body)) {$this->err_str="????????";return false;}
  284. if (!fputs($this->connection,".\r\n")) {$this->err_str="????????";return false;}//??????????
  285. $resp=fgets($this->connection,256);
  286. if($this->debug) $this->outdebug("$resp");
  287. if (substr($resp,0,1)!="2") {
  288. $this->err_str="??????????????";
  289. return false;
  290. }
  291. // ????
  292. }
  293. if (!$this->command("QUIT",3,"221")) return false;
  294. $this->close();
  295. return true;
  296. }
  297. }
  298. }
  299. ?>
  300. <?
  301. include("smtp.php");
  302. $mail = new smtp("localhost");
  303. $mail->debug = 0;
  304. $sender_name = "root";
  305. $sender_mail = "root@localhost";
  306. $to_name = "mm";
  307. $to_mail = "mm@your.com";
  308. $subject = "i miss u";
  309. $content = "i miss u much.";
  310. $att[0]["name"] = "miss.jpg";
  311. $att[0]["source"] = "/path/to/your/miss.jpg";
  312. $att[0]["type"] = "image/jpeg";
  313. $att[0]["size"] = "10kb";
  314. $extra_subject = "To:".$to_mail." ??:".$subject;
  315. if (!$mail->send($sender_name,$to_name,$sender_mail,$to_mail,$extra_subject,$content,false,$att))
  316. $err = 1;
  317. ?>