<?php
if( !defined('ISDVBBS') ){
	header('HTTP/1.0 404 Not Found');
	exit;
}
 function sendmail($mailto,$title,$body,$mailfrom){
	global $msetting;
	switch($msetting['mailmod']){
		case 0:
			return 0;
			break;
		case 1:
			if (@mail($mailto,$title,$body,$mailfrom)){
				return 1;
			} else{
				return 0;
			}
			break;
		case 2:
			$writed = 0;
			$getsbuf='';
            $db_charset="gb2312";
            $smtpserver=$msetting['server']==""?"smtp.".substr($msetting['username'],strpos($msetting['username'],"@")+1):$msetting['server'];
            if(!$fp=fsockopen($smtpserver,$msetting['port'],&$errno,&$errstr,30))return false;
            if(substr(fgets($fp,512),0,3)!="220") return false;
            if($msetting['auth']==1) {
               fwrite($fp,"HELO dvbbs.com\r\n");
               while($rt=strtolower(fgets($fp,512))){
                     if(strpos($rt,"-")!==3 || empty($rt)){
                        break;
                     }elseif(strpos($rt,"2")!==0){
                        return false;
                     }
                }
               fwrite($fp, "AUTH LOGIN \r\n");
               if(substr(fgets($fp,512),0,3) != 334) return false;
               fwrite($fp, base64_encode($msetting['username'])." \r\n");
               if(substr(fgets($fp,512),0,3) != 334) return false;
               fwrite($fp, base64_encode($msetting['password'])." \r\n");
               if(substr($rt=fgets($fp,512),0,3) != 235) return false;
             } else {
                  fwrite($fp, "HELO dvbbs.com\r\n");
             }
             $mailfrom=$msetting['username'];
             $mailfrom = preg_replace("/.*\<(.+?)\>.*/", "\\1", $mailfrom);
             fwrite($fp, "MAIL FROM: <$mailfrom>\r\n");
             if(substr(fgets($fp,512),0,3) != 250) return false;
             fwrite($fp, "RCPT TO: <$mailto>\r\n");
             if(substr(fgets($fp,512),0,3) != 250) return false;
             fwrite($fp, "DATA\r\n");
             if(substr(fgets($fp,512),0,3) != 354) return false;
             $title = str_replace("\n",' ',$title);
             $msg = "Date: ".Date("r")."\r\n";
             $msg .= "From: =?$db_charset?B?".base64_encode($msetting['username'])."?=<$mailfrom>\r\n";
             $msg .= "To: =?$db_charset?B?".base64_encode($mailto)."?=<$mailto>\r\n";
             $msg .= "Subject: =?$db_charset?B?".base64_encode($title)."?=\r\n";
             $msg .= "X-mailer: mail by dvbbs php\r\n";
             $msg .= "Mime-Version: 1.0\r\n";
             $msg .= "Content-Type: text/html;\r\n";
             $msg .= "\tcharset=\"$db_charset\"\r\n";
             $msg .= "Content-Transfer-Encoding: base64\r\n\r\n";
             $msg .= base64_encode($body)."\r\n.\r\n";
             fwrite($fp, $msg);
             fwrite($fp, "QUIT\r\n");
             fclose($fp);
             return true;
		case 3:
			ini_set('SMTP', $msetting['server']);
			ini_set('smtp_port', $msetting['port']);
			ini_set('sendmail_from', $mailfrom);
			if (@mail($mailto,$title,$body,"From: {$mailfrom}")){
				return 1;
			} else{
				return 0;
			}
			break;
		default:
			return 0;
			break;
	}
}
function replacehtml($str){
	$str = str_replace('&amp;','&',$str);
	$str = str_replace('&nbsp;',' ',$str);
	$str = str_replace('"','&quot;',$str);
	$str = str_replace("'",'&#39;',$str);
	$str = str_replace("<","&lt;",$str);
	$str = str_replace(">","&gt;",$str);
	$str = str_replace("\t"," &nbsp; &nbsp;",$str);
	$str = str_replace("\r","",$str);
	$str = str_replace("   "," &nbsp; ",$str);
	return $str;
}
?>