PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/require/sendemail.php

https://github.com/maddogfyg/eningjia
PHP | 346 lines | 331 code | 9 blank | 6 comment | 65 complexity | d1f9fca8758101e0d2d42cbc7cb66829 MD5 | raw file
  1. <?php
  2. !function_exists('readover') && exit('Forbidden');
  3. //set_time_limit(1000);
  4. @include_once(D_P.'data/bbscache/mail_config.php');
  5. $M_db= new Mailconfig(
  6. array(
  7. 'ifopen'=> $ml_mailifopen,
  8. 'method'=> $ml_mailmethod,
  9. 'host' => $ml_smtphost,
  10. 'port' => $ml_smtpport,
  11. 'auth' => $ml_smtpauth,
  12. 'from' => $ml_smtpfrom,
  13. 'user' => $ml_smtpuser,
  14. 'pass' => $ml_smtppass,
  15. 'smtphelo'=>$ml_smtphelo,
  16. 'smtpmxmailname' =>$ml_smtpmxmailname,
  17. 'mxdns'=>$ml_mxdns,
  18. 'mxdnsbak'=>$ml_mxdnsbak
  19. )
  20. );
  21. Class Mailconfig {
  22. var $S_method = 1;
  23. var $smtp;
  24. function Mailconfig($smtp=array()){
  25. $this->S_method = $smtp['method'];
  26. if(!$this->smtp['ifopen'] = $smtp['ifopen']) {
  27. Showmsg('mail_close');
  28. }
  29. if ($this->S_method == 1){
  30. //不用设置
  31. } elseif($this->S_method == 2){
  32. $this->smtp['host'] = $smtp['host'];
  33. $this->smtp['port'] = $smtp['port'];
  34. $this->smtp['auth'] = $smtp['auth'];
  35. $this->smtp['from'] = $smtp['from'];
  36. $this->smtp['user'] = $smtp['user'];
  37. $this->smtp['pass'] = $smtp['pass'];
  38. } elseif($this->S_method == 3){
  39. $this->smtp['port'] = $smtp['port'];
  40. $this->smtp['auth'] = $smtp['auth'];
  41. $this->smtp['from'] = $smtp['from'];
  42. $this->smtp['smtphelo']=$smtp['smtphelo'];
  43. $this->smtp['smtpmxmailname']=$smtp['smtpmxmailname'];
  44. $this->smtp['mxdns']=$smtp['mxdns'];
  45. $this->smtp['mxdnsbak']=$smtp['mxdnsbak'];
  46. //hacker
  47. } else{
  48. //hacker
  49. }
  50. }
  51. function mailmx($email,$retrys=3){
  52. global $timestamp;
  53. $domain=substr($email,strpos($email,'@')+1);
  54. @include(D_P.'data/bbscache/mx_config.php');
  55. if(!$_MX[$domain] || $timestamp - pwFilemtime(D_P.'data/bbscache/mx_config.php') > 3600*24*10){
  56. for($i=0;$i<$retrys;$i++){
  57. $result = $this->GetMax($domain);
  58. if($result !== false){
  59. $_MX[$domain]=$result;
  60. writeover(D_P.'data/bbscache/mx_config.php',"<?php\r\n\$_MX=".pw_var_export($_MX).";\r\n?>");
  61. $this->smtp['tomx']=$result;
  62. return true;
  63. }
  64. }
  65. return false;
  66. } else{
  67. $this->smtp['tomx']=$_MX[$domain];
  68. return true;
  69. }
  70. }
  71. function GetMax($maildomain){
  72. $header=pack("H*","000101000001000000000000");
  73. $end=pack("H*","00000f0001");
  74. $domain=explode(".",$maildomain);
  75. $ques='';
  76. foreach($domain as $value){
  77. $ques .=pack("Ca*",strlen($value),$value);
  78. }
  79. $fp = ($fp=fsockopen("udp://".$this->smtp['mxdns'], 53, $errno, $errstr , 30)) !== false ? $fp : fsockopen("udp://".$this->smtp['mxdnsbak'], 53, $errno, $errstr , 30);
  80. if(!$fp){
  81. return false;
  82. } else{
  83. fwrite($fp, $header.$ques.$end);
  84. $data=fread($fp,12);
  85. $q=unpack("n*",$data);
  86. if(array_shift($q) !=0x0001){
  87. return false;
  88. }
  89. if((array_shift($q) & 0x800F) !=0x8000){
  90. return false;
  91. }
  92. if(array_shift($q) !=0x0001){
  93. return false;
  94. }
  95. $anum=array_shift($q);
  96. if($anum < 0x0001){
  97. return false;
  98. }
  99. $aunum=array_shift($q);
  100. $aanum=array_shift($q);
  101. if(fread($fp,strlen($ques)+5) !== $ques.$end){
  102. return false;
  103. }
  104. $data .= $ques.$end;
  105. $rs=array();
  106. for($i=0;$i<$anum;$i++){
  107. $mxanwer=array();
  108. $tdata=fread($fp,1);
  109. $tmx=array();
  110. $tna=array();
  111. $tpre=65535;
  112. $compresspos=-1;
  113. for($j=0;$j<32;$j++){
  114. $tq=array_shift(unpack("C",$tdata));
  115. if(($tq & 192)==192){
  116. if($compresspos<0){
  117. $tdata .=fread($fp,1);
  118. $data .=$tdata;
  119. } else{
  120. $tdata=substr($data,$compresspos,2);
  121. }
  122. $tq=array_shift(unpack("n*",$tdata));
  123. $compresspos=($tq & 0x3fff);
  124. $tdata=substr($data,$compresspos,1);
  125. continue;
  126. }
  127. if($tq==0){
  128. break;
  129. } elseif($compresspos>0){
  130. $tna[]=array_shift(unpack("a*",substr($data,$compresspos+1,$tq)));
  131. $compresspos +=$tq+1;
  132. $tdata=substr($data,$compresspos,1);
  133. } else{
  134. $tdata=fread($fp,$tq);
  135. $data .=$tdata;
  136. $tna[]=unpack("a*",$tdata);
  137. $tdata=fread($fp,1);
  138. $data .=$tdata;
  139. }
  140. }
  141. $tdata=fread($fp,10);
  142. $data .=$tdata;
  143. $tq=unpack("n*",$tdata);
  144. $tdata=fread($fp,$tq[5]);
  145. $data .=$tdata;
  146. $tttl=array_shift(unpack("n*",$tq[4]));
  147. $tna=implode($tna,".");
  148. if($tq[1]===15 && $tq[2]===1){
  149. $tpref=array_shift(unpack("n*",substr($tdata,0,2)));
  150. $tdata=substr($tdata,2);
  151. $compresspos=-1;
  152. $tmdata=substr($tdata,0,1);
  153. $j=1;
  154. for($k=0;$k<32;$k++){
  155. $tq=array_shift(unpack("C",$tmdata));
  156. if(($tq & 192)==192){
  157. if($compresspos<0){
  158. $tmdata .=substr($tdata,$j,1);
  159. $j++;
  160. } else{
  161. $tmdata=substr($data,$compresspos,2);
  162. }
  163. $tq=array_shift(unpack("n*",$tmdata));
  164. $compresspos=($tq & 0x3fff);
  165. $tmdata=substr($data,$compresspos,1);
  166. continue;
  167. }
  168. if($tq==0){
  169. break;
  170. } elseif($compresspos>0){
  171. $tmx[]=array_shift(unpack("a*",substr($data,$compresspos+1,$tq)));
  172. $compresspos +=$tq+1;
  173. $tmdata=substr($data,$compresspos,1);
  174. } else{
  175. $tmdata=substr($tdata,$j,$tq);
  176. $j +=$tq;
  177. $tmx[]=array_shift(unpack("a*",$tmdata));
  178. $tmdata=substr($tdata,$j,1);
  179. $j++;
  180. }
  181. }
  182. $rs['mx'][$tttl][]=implode($tmx,".");
  183. }
  184. }
  185. arsort($rs,SORT_ASC);
  186. foreach($rs['mx'] as $key=>$values){
  187. arsort($values,SORT_ASC);
  188. foreach($values as $value){
  189. $mxs[]=$value;
  190. }
  191. }
  192. fclose($fp);
  193. return $mxs;
  194. }
  195. }
  196. }
  197. function sendemail($toemail,$subject,$message,$additional=null){
  198. global $M_db,$db_bbsname,$regname,$db_bbsurl,$windid,$winduid,$timestamp,$regpwd,$manager,$db_ceoemail,$fromemail,$pwd_user,$submit,$receiver,$old_title,$fid,$tid,$pwuser,$db_charset,$sendtoname,$db_registerfile;
  199. !$fromemail && $fromemail = $db_ceoemail;
  200. !$sendtoname && $sendtoname = $toemail;
  201. !$windid && $windid = $db_bbsname;
  202. $subject = stripslashes(getLangInfo('email',$subject));
  203. $message = stripslashes(getLangInfo('email',$message));
  204. $additional = getLangInfo('email',$additional);
  205. $send_subject = "=?$db_charset?B?".base64_encode(str_replace(array("\r","\n"), array('',' '),$subject)).'?=';
  206. $send_message = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $message)))))));
  207. $send_from = "=?$db_charset?B?".base64_encode($windid)."?= <$fromemail>";
  208. $send_to = "=?$db_charset?B?".base64_encode($sendtoname)."?= <$toemail>";
  209. !empty($additional) && $additional && substr(str_replace(array("\r","\n"),array('','<rn>'),$additional),-4) != '<rn>' && $additional .= "\r\n";
  210. $additional = "To: $send_to\r\nFrom: $send_from\r\nMIME-Version: 1.0\r\nContent-type: text/html; charset=$db_charset\r\n{$additional}Content-Transfer-Encoding: base64\r\n";
  211. if($M_db->S_method == 1){
  212. if(@mail($toemail,$send_subject,$send_message,$additional)){
  213. return true;
  214. } else{
  215. return false;
  216. }
  217. } elseif($M_db->S_method == 2){
  218. if(!$fp=fsockopen($M_db->smtp['host'],$M_db->smtp['port'],$errno,$errstr)){
  219. Showmsg('email_connect_failed');
  220. }
  221. if(strncmp(fgets($fp,512),'220',3)!=0){
  222. Showmsg('email_connect_failed');
  223. }
  224. if($M_db->smtp['auth']){
  225. fwrite($fp,"EHLO phpwind\r\n");
  226. while($rt=strtolower(fgets($fp,512))){
  227. if(strpos($rt,"-")!==3 || empty($rt)){
  228. break;
  229. } elseif(strpos($rt,"2")!==0){
  230. return false;
  231. }
  232. }
  233. fwrite($fp, "AUTH LOGIN\r\n");
  234. if(strncmp(fgets($fp,512),'334',3)!=0){
  235. return false;
  236. }
  237. fwrite($fp, base64_encode($M_db->smtp['user'])."\r\n");
  238. if(strncmp(fgets($fp,512),'334',3)!=0){
  239. return 'email_user_failed';
  240. }
  241. fwrite($fp, base64_encode($M_db->smtp['pass'])."\r\n");
  242. if(strncmp(fgets($fp,512),'235',3)!=0){
  243. return 'email_pass_failed';
  244. }
  245. } else{
  246. fwrite($fp, "HELO phpwind\r\n");
  247. }
  248. $from = $M_db->smtp['from'];
  249. $from = preg_replace("/.*\<(.+?)\>.*/", "\\1", $from);
  250. fwrite($fp, "MAIL FROM: <$from>\r\n");
  251. if(strncmp(fgets($fp,512),'250',3)!=0){
  252. return 'email_from_failed';
  253. }
  254. fwrite($fp, "RCPT TO: <$toemail>\r\n");
  255. if(strncmp(fgets($fp,512),'250',3)!=0){
  256. return 'email_toemail_failed';
  257. }
  258. fwrite($fp, "DATA\r\n");
  259. if(strncmp(fgets($fp,512),'354',3)!=0){
  260. return 'email_data_failed';
  261. }
  262. $msg = "Date: ".Date("r")."\r\n";
  263. $msg .= "Subject: $send_subject\r\n";
  264. $msg .= "$additional\r\n";
  265. $msg .= "$send_message\r\n.\r\n";
  266. fwrite($fp, $msg);
  267. $lastmessage = fgets($fp, 512);
  268. if(substr($lastmessage, 0, 3) != 250)
  269. {
  270. Showmsg('email_connect_failed');
  271. }
  272. fwrite($fp, "QUIT\r\n");
  273. fclose($fp);
  274. return true;
  275. } elseif($M_db->S_method == 3){
  276. if(!$M_db->mailmx($toemail)){
  277. return false;
  278. }
  279. foreach($M_db->smtp['tomx'] as $server){
  280. if(($fp=fsockopen($server,25,$errno,$errstr)) && strncmp(fgets($fp,512),'220',3)==0){
  281. break;
  282. }
  283. }
  284. fwrite($fp, "HELO ".$M_db->smtp['smtphelo']."\r\n");
  285. if(strncmp(fgets($fp,512),'250',3)!=0){
  286. fwrite($fp,"EHLO ".$M_db->smtp['smtphelo']."\r\n");
  287. while($rt=strtolower(fgets($fp,512))){
  288. if(strpos($rt,"-")!==3 || empty($rt)){
  289. break;
  290. } elseif(strpos($rt,"2")!==0){
  291. return false;
  292. }
  293. }
  294. fwrite($fp, "AUTH LOGIN\r\n");
  295. if(strncmp(fgets($fp,512),'334',3)!=0){
  296. return false;
  297. }
  298. fwrite($fp, base64_encode($M_db->smtp['user'])."\r\n");
  299. if(strncmp(fgets($fp,512),'334',3)!=0){
  300. return false;
  301. }
  302. fwrite($fp, base64_encode($M_db->smtp['pass'])."\r\n");
  303. if(strncmp(fgets($fp,512),'235',3)!=0){
  304. return false;
  305. }
  306. }
  307. $from = $M_db->smtp['smtpmxmailname'];
  308. $reply = $M_db->smtp['from'];
  309. fwrite($fp, "MAIL FROM: <$from>\r\n");
  310. if(strncmp(fgets($fp,512),'250',3)!=0){
  311. return false;
  312. }
  313. fwrite($fp, "RCPT TO: <$toemail>\r\n");
  314. if(strncmp(fgets($fp,512),'250',3)!=0){
  315. return false;
  316. }
  317. fwrite($fp, "DATA\r\n");
  318. if(strncmp(fgets($fp,512),'354',3)!=0){
  319. return false;
  320. }
  321. $msg = "Date: ".Date("r")."\r\n";
  322. $msg .= "Subject: $send_subject\r\n";
  323. $msg .= "$additional\r\n";
  324. $msg .= "$send_message\r\n.\r\n";
  325. fwrite($fp, $msg);
  326. if(strncmp(fgets($fp,512),'250',3)!=0){
  327. return false;
  328. }
  329. fwrite($fp, "QUIT\r\n");
  330. fclose($fp);
  331. return true;
  332. //hacker
  333. } else{
  334. //hacker
  335. }
  336. }
  337. ?>